arduino-tnc

git clone https://git.bradleygannon.com/arduino-tnc.git
Log | Files | Refs

rx.rs (1687B)


      1 use arduino_hal::{
      2     hal::port::PC0,
      3     port::{
      4         mode::{Analog, Floating, Input},
      5         Pin,
      6     },
      7     Adc,
      8 };
      9 use tnc_core::{constants::SAMPLES_PER_SYMBOL, demodulator::Bell202Demodulator};
     10 
     11 pub(crate) struct Receiver {
     12     adc: Option<Adc>,
     13     input: Option<Pin<Analog, PC0>>,
     14     demod: Bell202Demodulator,
     15     sample_count: u8,
     16 }
     17 
     18 impl Receiver {
     19     pub(crate) const fn default() -> Receiver {
     20         Receiver {
     21             adc: None,
     22             input: None,
     23             demod: Bell202Demodulator::new(),
     24             sample_count: 0,
     25         }
     26     }
     27     pub(crate) fn initialize(&mut self, mut adc: Adc, input: Pin<Input<Floating>, PC0>) {
     28         self.input = Some(input.into_analog_input(&mut adc));
     29         self.adc = Some(adc);
     30     }
     31     pub(crate) fn get_output(&self) -> Option<u8> {
     32         self.demod.get_output()
     33     }
     34     /// Accumulate a sample and check for symbol boundary
     35     ///
     36     /// Returns `true` when 32 samples have been accumulated and a symbol decision has been made
     37     /// (filters reset). This is used by the TIMER0 ISR to trigger clock-recovery adjustments on
     38     /// TIMER1.
     39     pub(crate) fn sample_update(&mut self) -> bool {
     40         let mut at_boundary = false;
     41         if let (Some(ref input), Some(ref mut adc)) = (&self.input, &mut self.adc) {
     42             let new_sample = input.analog_read(adc) as f32 / 512. - 0.5;
     43             self.demod.sample_update(new_sample);
     44             self.sample_count += 1;
     45             if self.sample_count >= SAMPLES_PER_SYMBOL {
     46                 self.sample_count = 0;
     47                 self.demod.symbol_update();
     48                 at_boundary = true;
     49             }
     50         }
     51         at_boundary
     52     }
     53 }