vfo-sync

Bidirectional VFO synchronization for IC-7300 and SDR++
git clone https://git.bradleygannon.com/vfo-sync.git
Log | Files | Refs

main.rs (2146B)


      1 use std::{sync::mpsc, thread};
      2 
      3 use ic7300::ic7300_handler;
      4 use log::{debug, error, trace};
      5 use rspduo::rspduo_handler;
      6 
      7 mod ic7300;
      8 mod rspduo;
      9 
     10 type Freq = u32;
     11 type Ptt = bool;
     12 
     13 #[derive(Debug, Clone, Copy, PartialEq)]
     14 struct State {
     15     freq: Freq,
     16     ptt: Ptt,
     17 }
     18 
     19 impl Default for State {
     20     fn default() -> Self {
     21         State {
     22             freq: 0,
     23             ptt: false,
     24         }
     25     }
     26 }
     27 
     28 fn main() {
     29     env_logger::init();
     30 
     31     // By convention, foo uses foo_tx_bar to transmit to bar and uses foo_rx_bar to receive from bar
     32     let (main_tx_ic7300, ic7300_rx_main) = mpsc::channel();
     33     let (main_tx_rspduo, rspduo_rx_main) = mpsc::channel();
     34     let (ic7300_tx_main, main_rx_ic7300) = mpsc::channel();
     35     let (rspduo_tx_main, main_rx_rspduo) = mpsc::channel();
     36     debug!("Set up thread channels");
     37 
     38     let mut current_state = State {
     39         freq: 0,
     40         ptt: false,
     41     };
     42 
     43     let ic7300 = thread::spawn(|| ic7300_handler(ic7300_tx_main, ic7300_rx_main));
     44     debug!("Spawned thread for IC-7300");
     45     let rspduo = thread::spawn(|| rspduo_handler(rspduo_tx_main, rspduo_rx_main));
     46     debug!("Spawned thread for RSPduo");
     47 
     48     loop {
     49         if ic7300.is_finished() || rspduo.is_finished() {
     50             error!(
     51                 "A thread exited, closing... (IC-7300: {}, RSPduo: {})",
     52                 ic7300.is_finished(),
     53                 rspduo.is_finished()
     54             );
     55             break;
     56         }
     57         if let Some(new_state) = main_rx_ic7300.try_iter().last() {
     58             trace!("Got new state from IC-7300: {:?}", new_state);
     59             if new_state != current_state {
     60                 main_tx_rspduo.send(new_state).unwrap();
     61                 current_state = new_state;
     62                 debug!("Sent new state to RSPduo");
     63             }
     64         } else if let Some(new_state) = main_rx_rspduo.try_iter().last() {
     65             trace!("Got new state from RSPduo: {:?}", new_state);
     66             if new_state != current_state {
     67                 main_tx_ic7300.send(new_state).unwrap();
     68                 current_state = new_state;
     69                 debug!("Sent new state to IC-7300");
     70             }
     71         }
     72     }
     73 }