From 2642c37a9ffd686465b5da4e06247ebb03a02cf0 Mon Sep 17 00:00:00 2001
From: Stephen D <webmaster@scd31.com>
Date: Thu, 15 Feb 2024 22:52:57 -0400
Subject: [PATCH] change device id to string, and add gain setting

---
 config.example.toml |  3 ++-
 src/config.rs       |  9 +++++++--
 src/decoder/mod.rs  |  2 +-
 src/decoder/rtl.rs  | 17 +++++++++++------
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/config.example.toml b/config.example.toml
index 4b1e964..e90621d 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -1,6 +1,7 @@
 # rtl_fm parameters
 frequency = 430_500_000 # center frequency, Hz
-device_id = 0 # ID of your RTL-SDR; 0 is default
+device_id = "0" # ID of your RTL-SDR; 0 is default
+# gain = 30.0 # dB. Uses AGC when not specified
 
 # if set to true:
 #  - expects 48k samples/second, 16-bit LE IQ.
diff --git a/src/config.rs b/src/config.rs
index 15d9372..fae5464 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -4,6 +4,10 @@ use anyhow::{bail, Context};
 use serde::Deserialize;
 use serde_with::{serde_as, DurationSeconds};
 
+fn default_device_id() -> String {
+    "0".to_string()
+}
+
 #[serde_as]
 #[derive(Deserialize, Clone)]
 pub struct BeaconConfig {
@@ -31,8 +35,9 @@ pub struct FelinetConfig {
 #[derive(Deserialize, Clone)]
 pub struct Config {
     pub frequency: u32,
-    #[serde(default)]
-    pub device_id: u32,
+    #[serde(default = "default_device_id")]
+    pub device_id: String,
+    pub gain: Option<f32>,
     #[serde(default)]
     pub use_stdin: bool,
     pub max_deviation: i32,
diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs
index 57ebe6c..3457fae 100644
--- a/src/decoder/mod.rs
+++ b/src/decoder/mod.rs
@@ -27,7 +27,7 @@ pub fn decode_forever(felinet_send: mpsc::Sender<SemiPacketIn>, config: &Config,
 
         Box::new(stdin.bytes().map(|x| x.expect("Could not read from stdin")))
     } else {
-        let rtl = RtlSdr::new(config.frequency, config.device_id).unwrap();
+        let rtl = RtlSdr::new(config.frequency, &config.device_id, config.gain).unwrap();
         Box::new(rtl)
     };
 
diff --git a/src/decoder/rtl.rs b/src/decoder/rtl.rs
index 2285b05..7e7bb07 100644
--- a/src/decoder/rtl.rs
+++ b/src/decoder/rtl.rs
@@ -11,14 +11,19 @@ pub struct RtlSdr {
 }
 
 impl RtlSdr {
-    pub fn new(freq: u32, device_id: u32) -> anyhow::Result<Self> {
+    pub fn new(freq: u32, device_id: &str, gain: Option<f32>) -> anyhow::Result<Self> {
         let freq = format!("{freq}");
-        let device_id = format!("{device_id}");
+        let mut args = vec![
+            "rtl_fm", "-f", &freq, "-M", "raw", "-F9", "-p", "0", "-d", device_id, "-s", "48000",
+        ];
+        let mut gain_s = String::new();
+        if let Some(g) = gain {
+            args.push("-g");
+            gain_s.push_str(&format!("{:.2}", g));
+            args.push(&gain_s);
+        }
         let mut process = Popen::create(
-            &[
-                "rtl_fm", "-f", &freq, "-M", "raw", "-F9", "-p", "0", "-d", &device_id, "-s",
-                "48000",
-            ],
+            &args,
             PopenConfig {
                 stdout: Redirection::Pipe,
                 stderr: Redirection::Pipe,
-- 
GitLab