diff options
| author | Yuval Adam <_@yuv.al> | 2021-04-25 09:28:58 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2021-04-25 09:28:58 +0300 |
| commit | 47934e3c7ca64bfb82fbcede47491084a9e9f266 (patch) | |
| tree | cb2c905375ea35d30654ffb32f19710e4be46410 /src | |
| parent | 0a6bfe601e51f4c9475bec03d077e72ed7c46823 (diff) | |
Restore dynamic trait polymorphism with handles in functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 11 | ||||
| -rw-r--r-- | src/tuners/r820t.rs | 94 | ||||
| -rw-r--r-- | src/usb.rs | 19 |
3 files changed, 63 insertions, 61 deletions
@@ -16,17 +16,17 @@ const KNOWN_DEVICES: [(u16, u16, &str); 2] = [ ]; pub struct RtlSdr { - // handle: RtlSdrDeviceHandle, + handle: RtlSdrDeviceHandle, } impl RtlSdr { pub fn new() -> RtlSdr { pretty_env_logger::init(); - let handle = Self::open_device(); - RtlSdr {} + let handle = Self::open_device().unwrap(); + RtlSdr { handle } } - pub fn open_device() { + pub fn open_device() -> Option<RtlSdrDeviceHandle> { for dev in rusb::devices().unwrap().iter() { let desc = dev.device_descriptor().unwrap(); let vid = desc.vendor_id(); @@ -50,9 +50,12 @@ impl RtlSdr { handle.set_i2c_repeater(true); handle.init_tuner(); + + return Some(handle); } } } + None } // pub fn set_sample_rate(&self, samp_rate: u32) { diff --git a/src/tuners/r820t.rs b/src/tuners/r820t.rs index ae80e25..444caa9 100644 --- a/src/tuners/r820t.rs +++ b/src/tuners/r820t.rs @@ -1,4 +1,4 @@ -use super::TunerInfo; +use super::{Tuner, TunerInfo}; use log::info; use usb::RtlSdrDeviceHandle; @@ -56,7 +56,6 @@ pub struct Config { } pub struct R820T { - pub handle: RtlSdrDeviceHandle, pub device: TunerInfo, regs: [u8; NUM_REGS], buf: [u8; NUM_REGS + 1], @@ -90,7 +89,7 @@ const INITIAL_REGS: [u8; NUM_REGS - 3] = [ 0x00, 0xc0, 0x30, 0x48, 0xcc, 0x60, 0x00, 0x54, 0xae, 0x4a, 0xc0, ]; -const FREQ_RANGES: [(u32, u8, u8, u8, u8, u8, u8); 21] = [ +const _FREQ_RANGES: [(u32, u8, u8, u8, u8, u8, u8); 21] = [ (000, 0x08, 0x02, 0xdf, 0x02, 0x01, 0x00), (050, 0x08, 0x02, 0xbe, 0x02, 0x01, 0x00), (055, 0x08, 0x02, 0x8b, 0x02, 0x01, 0x00), @@ -161,7 +160,7 @@ fn get_freq_range(freq: u32) -> FreqRange { } } -const XTAL_CAPS: [(u8, XtalCapValue); 5] = [ +const _XTAL_CAPS: [(u8, XtalCapValue); 5] = [ (0x0b, XtalCapValue::XtalLowCap30P), (0x02, XtalCapValue::XtalLowCap20P), (0x01, XtalCapValue::XtalLowCap10P), @@ -170,9 +169,8 @@ const XTAL_CAPS: [(u8, XtalCapValue); 5] = [ ]; impl R820T { - pub fn new(handle: RtlSdrDeviceHandle) -> R820T { + pub fn new(handle: &RtlSdrDeviceHandle) -> R820T { let tuner = R820T { - handle: handle, device: TUNER_INFO, regs: [0; NUM_REGS], buf: [0; NUM_REGS + 1], @@ -187,23 +185,23 @@ impl R820T { // tuner_type: TunerType, // bw: u32, // MHz }; - tuner.init(); + tuner.init(handle); tuner } - fn init(&self) { + fn init(&self, handle: &RtlSdrDeviceHandle) { // disable Zero-IF mode - self.handle.demod_write_reg(1, 0xb1, 0x1a, 1); + handle.demod_write_reg(1, 0xb1, 0x1a, 1); // only enable In-phase ADC input - self.handle.demod_write_reg(0, 0x08, 0x4d, 1); + handle.demod_write_reg(0, 0x08, 0x4d, 1); // the R82XX use 3.57 MHz IF for the DVB-T 6 MHz mode, and // 4.57 MHz for the 8 MHz mode - self.handle.set_if_freq(IF_FREQ); + handle.set_if_freq(IF_FREQ); // enable spectrum inversion - self.handle.demod_write_reg(1, 0x15, 0x01, 1); + handle.demod_write_reg(1, 0x15, 0x01, 1); } fn shadow_store(&self, reg: u8, _val: u8, len: usize) { @@ -257,50 +255,50 @@ impl Drop for R820T { } } -// impl Tuner for R820T { -// fn init(&self, handle: &RtlSdrDeviceHandle) { -// // disable Zero-IF mode -// handle.demod_write_reg(1, 0xb1, 0x1a, 1); +impl Tuner for R820T { + fn init(&self, handle: &RtlSdrDeviceHandle) { + // disable Zero-IF mode + handle.demod_write_reg(1, 0xb1, 0x1a, 1); -// // only enable In-phase ADC input -// handle.demod_write_reg(0, 0x08, 0x4d, 1); + // only enable In-phase ADC input + handle.demod_write_reg(0, 0x08, 0x4d, 1); -// // the R82XX use 3.57 MHz IF for the DVB-T 6 MHz mode, and -// // 4.57 MHz for the 8 MHz mode -// handle.set_if_freq(IF_FREQ); + // the R82XX use 3.57 MHz IF for the DVB-T 6 MHz mode, and + // 4.57 MHz for the 8 MHz mode + handle.set_if_freq(IF_FREQ); -// // enable spectrum inversion -// handle.demod_write_reg(1, 0x15, 0x01, 1); -// } + // enable spectrum inversion + handle.demod_write_reg(1, 0x15, 0x01, 1); + } -// fn exit(&self) {} + fn exit(&self) {} -// fn set_freq(&self, _freq: u32) { -// unimplemented!() -// } + fn set_freq(&self, _freq: u32) { + unimplemented!() + } -// fn set_bandwidth(&self, bw: u16, rate: u32, handle: &RtlSdrDeviceHandle) { -// let mut rc: u16; -// let mut real_bw: u16 = 0; -// let mut reg_0a: u8; -// let mut reg_0b: u8; + fn set_bandwidth(&self, _bw: u16, _rate: u32, _handle: &RtlSdrDeviceHandle) { + let mut _rc: u16; + let mut _real_bw: u16 = 0; + let mut _reg_0a: u8; + let mut _reg_0b: u8; -// if bw > 7000000 {} -// } + // if bw > 7000000 {} + } -// fn set_gain(&self, _gain: u32) { -// unimplemented!() -// } + fn set_gain(&self, _gain: u32) { + unimplemented!() + } -// fn set_if_gain(&self, _if_gain: u32) { -// unimplemented!() -// } + fn set_if_gain(&self, _if_gain: u32) { + unimplemented!() + } -// fn set_gain_mode(&self, _mode: bool) { -// unimplemented!() -// } + fn set_gain_mode(&self, _mode: bool) { + unimplemented!() + } -// fn display(&self) -> &str { -// self.device.name -// } -// } + fn display(&self) -> &str { + self.device.name + } +} @@ -40,7 +40,7 @@ const CTRL_TIMEOUT: Duration = Duration::from_millis(300); pub struct RtlSdrDeviceHandle { handle: DeviceHandle<GlobalContext>, - // tuner: Option<r820t::R820T>, // when we go abstract: Option<Box<dyn Tuner>>, + tuner: Option<Box<dyn Tuner>>, iface_id: u8, kernel_driver_active: bool, } @@ -51,7 +51,7 @@ impl RtlSdrDeviceHandle { pub fn new(handle: DeviceHandle<GlobalContext>, iface_id: u8) -> RtlSdrDeviceHandle { let mut handle = RtlSdrDeviceHandle { handle, - // tuner: None, + tuner: None, iface_id, kernel_driver_active: false, }; @@ -60,7 +60,7 @@ impl RtlSdrDeviceHandle { handle } - pub fn init_tuner(self) { + pub fn init_tuner(&mut self) { let tuner_id: &str = match self.search_tuner() { Some(tid) => { trace!("Found tuner ID {}", tid); @@ -69,16 +69,16 @@ impl RtlSdrDeviceHandle { None => "", }; - let tuner: Option<r820t::R820T> = match tuner_id { - r820t::TUNER_ID => Some(r820t::R820T::new(self)), - // fc0013::TUNER_ID => Some(Box::new(fc0013::FC0013::new(&self))), + let tuner: Option<Box<dyn Tuner>> = match tuner_id { + r820t::TUNER_ID => Some(Box::new(r820t::R820T::new(&self))), + fc0013::TUNER_ID => Some(Box::new(fc0013::FC0013::new(&self))), _ => { error!("Could not find any valid tuner."); None } }; - // self.tuner = tuner; + self.tuner = tuner; info!("Found tuner r820t"); } @@ -226,12 +226,13 @@ impl RtlSdrDeviceHandle { } pub fn i2c_write(&self, i2c_addr: u8, buf: &[u8]) { - self.write_array(BLOCK_IICB, i2c_addr as u16, buf); + self.write_array(BLOCK_IICB, i2c_addr as u16, buf).unwrap(); } pub fn i2c_write_reg(&self, i2c_addr: u8, reg: u8, val: u8) { let data: [u8; 2] = [reg, val]; - self.write_array(BLOCK_IICB, i2c_addr as u16, &data); + self.write_array(BLOCK_IICB, i2c_addr as u16, &data) + .unwrap(); } pub fn set_i2c_repeater(&self, on: bool) { |
