summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2021-04-16 17:23:10 +0300
committerYuval Adam <_@yuv.al>2021-04-17 10:33:57 +0300
commit01824ebaa3096d40a962b921ed239241f92e1076 (patch)
tree1b3d9f810b5086e52678fbed7f6e96e9e39d4cdf /src
parent199f7cfb5749a1810db7923780e5a65eb17b8e11 (diff)
Implement search_tuner()
Diffstat (limited to 'src')
-rw-r--r--src/devices/fc0013.rs12
-rw-r--r--src/devices/mod.rs4
-rw-r--r--src/devices/r820t.rs10
-rw-r--r--src/lib.rs57
4 files changed, 45 insertions, 38 deletions
diff --git a/src/devices/fc0013.rs b/src/devices/fc0013.rs
index 3a26218..7c64758 100644
--- a/src/devices/fc0013.rs
+++ b/src/devices/fc0013.rs
@@ -1,12 +1,12 @@
-use super::{Device, DeviceInfo};
+use super::{Tuner, TunerInfo};
#[allow(dead_code)]
pub struct FC0013 {
- pub device: DeviceInfo,
+ pub device: TunerInfo,
}
#[allow(dead_code)]
-pub const DEVICE_INFO: DeviceInfo = DeviceInfo {
+pub const TUNER_INFO: TunerInfo = TunerInfo {
id: "fc0013",
name: "Fitipower FC0013",
i2c_addr: 0xc6,
@@ -17,13 +17,11 @@ pub const DEVICE_INFO: DeviceInfo = DeviceInfo {
#[allow(dead_code)]
impl FC0013 {
pub fn new() -> FC0013 {
- FC0013 {
- device: DEVICE_INFO,
- }
+ FC0013 { device: TUNER_INFO }
}
}
-impl Device for FC0013 {
+impl Tuner for FC0013 {
fn init(&self) {
println!("Init {}", self.device.name);
}
diff --git a/src/devices/mod.rs b/src/devices/mod.rs
index a48531d..cbd7edf 100644
--- a/src/devices/mod.rs
+++ b/src/devices/mod.rs
@@ -1,7 +1,7 @@
pub mod fc0013;
pub mod r820t;
-pub struct DeviceInfo {
+pub struct TunerInfo {
pub id: &'static str,
pub name: &'static str,
pub i2c_addr: u8,
@@ -9,7 +9,7 @@ pub struct DeviceInfo {
pub check_val: u8,
}
-pub trait Device {
+pub trait Tuner {
fn init(&self);
fn exit(&self);
fn set_freq(&self, freq: u32);
diff --git a/src/devices/r820t.rs b/src/devices/r820t.rs
index a257b56..ad4720d 100644
--- a/src/devices/r820t.rs
+++ b/src/devices/r820t.rs
@@ -1,14 +1,14 @@
-use super::{Device, DeviceInfo};
+use super::{Tuner, TunerInfo};
use usb::RtlSdrDeviceHandle;
const R82XX_IF_FREQ: u32 = 3570000;
pub struct R820T<'a> {
- pub device: DeviceInfo,
+ pub device: TunerInfo,
pub handle: &'a RtlSdrDeviceHandle,
}
-pub const DEVICE_INFO: DeviceInfo = DeviceInfo {
+pub const TUNER_INFO: TunerInfo = TunerInfo {
id: "r820t",
name: "Rafael Micro R820T",
i2c_addr: 0x34,
@@ -19,7 +19,7 @@ pub const DEVICE_INFO: DeviceInfo = DeviceInfo {
impl<'a> R820T<'a> {
pub fn new(handle: &'a RtlSdrDeviceHandle) -> R820T<'a> {
R820T {
- device: DEVICE_INFO,
+ device: TUNER_INFO,
handle: handle,
}
}
@@ -29,7 +29,7 @@ impl<'a> Drop for R820T<'a> {
fn drop(&mut self) {}
}
-impl<'a> Device for R820T<'a> {
+impl<'a> Tuner for R820T<'a> {
fn init(&self) {
// disable Zero-IF mode
self.handle.demod_write_reg(1, 0xb1, 0x1a, 1);
diff --git a/src/lib.rs b/src/lib.rs
index 41aede9..b3c2a05 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,7 +13,7 @@ const KNOWN_DEVICES: [(u16, u16, &str); 2] = [
(0x0bda, 0x2838, "Generic RTL2832U OEM"),
];
-const KNOWN_TUNERS: [DeviceInfo; 2] = [r820t::DEVICE_INFO, fc0013::DEVICE_INFO];
+const KNOWN_TUNERS: [TunerInfo; 2] = [r820t::TUNER_INFO, fc0013::TUNER_INFO];
pub struct RtlSdr {
// ctx: rusb::Context,
@@ -29,8 +29,8 @@ impl RtlSdr {
}
}
- pub fn open(&mut self) -> bool {
- let mut found = false;
+ /// Open a new RTL-SDR device
+ pub fn open(&mut self) {
for dev in rusb::devices().unwrap().iter() {
let desc = dev.device_descriptor().unwrap();
let vid = desc.vendor_id();
@@ -51,28 +51,22 @@ impl RtlSdr {
handle.init_baseband();
handle.set_i2c_repeater(true);
- for tuner_info in KNOWN_TUNERS.iter() {
- match handle.i2c_read_reg(tuner_info.i2c_addr, tuner_info.check_addr) {
- Ok(val) => {
- if val == tuner_info.check_val {
- println!("Found {} tuner\n", tuner_info.name);
- found = true;
- break;
- } else {
- println!(
- "Mismatch in {} tuner expected {} found {}\n",
- tuner_info.name, tuner_info.check_val, val
- );
- }
+ let tuner = match self.search_tuner(&handle) {
+ Some(tuner) => match tuner {
+ "r820t" => Some(r820t::R820T::new(&handle)),
+ _ => {
+ println!("No valid tuner found");
+ return;
}
- Err(_) => {}
- };
- }
+ },
+ None => {
+ println!("No valid tuner found");
+ return;
+ }
+ };
- // {
- // let d = r820t::R820T::new(&handle);
- // d.init();
- // }
+ let tuner = tuner.unwrap();
+ tuner.init();
// handle.deinit_baseband();
@@ -84,7 +78,22 @@ impl RtlSdr {
}
}
}
- found
+ }
+
+ /// Probe all known tuners at their I2C addresses
+ /// and search for expected return values
+ fn search_tuner(&mut self, handle: &RtlSdrDeviceHandle) -> Option<&str> {
+ for tuner_info in KNOWN_TUNERS.iter() {
+ match handle.i2c_read_reg(tuner_info.i2c_addr, tuner_info.check_addr) {
+ Ok(val) => {
+ if val == tuner_info.check_val {
+ return Some(tuner_info.name);
+ }
+ }
+ Err(_) => {}
+ };
+ }
+ None
}
}