]> code.octet-stream.net Git - m17rt/blob - m17app/src/serial.rs
d3eec1936eab13cd8725b0b67f43de07fb5e5dab
[m17rt] / m17app / src / serial.rs
1 use serialport::SerialPort;
2
3 use crate::soundmodem::Ptt;
4
5 /// The pin on the serial port which is driving PTT
6 pub enum PttPin {
7 // Ready To Send (RTS)
8 Rts,
9 // Data Terminal ready (DTR)
10 Dtr,
11 }
12
13 pub struct SerialPtt {
14 port: Box<dyn SerialPort>,
15 pin: PttPin,
16 }
17
18 impl SerialPtt {
19 pub fn available_ports() -> impl Iterator<Item = String> {
20 serialport::available_ports()
21 .unwrap_or_else(|_| vec![])
22 .into_iter()
23 .map(|i| i.port_name)
24 }
25
26 pub fn new(port_name: &str, pin: PttPin) -> Self {
27 // TODO: error handling
28 let port = serialport::new(port_name, 9600).open().unwrap();
29 Self { port, pin }
30 }
31 }
32
33 impl Ptt for SerialPtt {
34 fn ptt_on(&mut self) {
35 let _ = match self.pin {
36 PttPin::Rts => self.port.write_request_to_send(true),
37 PttPin::Dtr => self.port.write_data_terminal_ready(true),
38 };
39 }
40
41 fn ptt_off(&mut self) {
42 let _ = match self.pin {
43 PttPin::Rts => self.port.write_request_to_send(false),
44 PttPin::Dtr => self.port.write_data_terminal_ready(false),
45 };
46 }
47 }