1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
use std::time::Duration; use serial_core::prelude::*; use thiserror::Error; use flipdot_core::{Frame, Message, SignBus}; /// Errors related to [`Odk`]s. #[derive(Debug, Error)] #[non_exhaustive] pub enum OdkError { /// The sign bus failed to process a message. #[error("Sign bus failed to process message")] Bus { /// The underlying bus error. #[from] source: Box<dyn std::error::Error + Send + Sync>, }, /// Failure reading/writing data. #[error("ODK communication error")] Communication { /// The underlying communication error. #[from] source: flipdot_core::FrameError, }, } /// Connects to a real ODK over the specified serial port and uses it to drive a [`SignBus`]. /// /// Typically this will be used to drive a [`VirtualSignBus`](crate::VirtualSignBus) in order to study the bus traffic /// or inspect the pages of pixel data sent by the ODK. /// /// # Examples /// /// ```no_run /// use flipdot_serial::SerialSignBus; /// use flipdot_testing::{Address, Odk, VirtualSign, VirtualSignBus}; /// /// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// # /// // Populate bus with signs from addresses 2 to 126 /// // (which seems to be the possible range for actual signs). /// let signs = (2..127).map(Address).map(VirtualSign::new); /// let bus = VirtualSignBus::new(signs); /// /// // Hook up ODK to virtual bus. /// let port = serial::open("/dev/ttyUSB0")?; /// let mut odk = Odk::try_new(port, bus)?; /// loop { /// // ODK communications are forwarded to/from the virtual bus. /// odk.process_message()?; /// } /// # /// # Ok(()) } /// ``` #[derive(Debug, PartialEq, Eq, Hash)] pub struct Odk<P: SerialPort, B: SignBus> { port: P, bus: B, } impl<P: SerialPort, B: SignBus> Odk<P, B> { /// Create a new `Odk` that connects the specified serial port and bus. /// /// # Errors /// /// Returns the underlying [`serial_core::Error`] if the serial port cannot be configured. /// /// # Examples /// /// ```no_run /// # use flipdot_serial::SerialSignBus; /// # use flipdot_testing::{Address, Odk, VirtualSign, VirtualSignBus}; /// # /// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// # /// let bus = VirtualSignBus::new(vec![VirtualSign::new(Address(3))]); /// let port = serial::open("COM3")?; /// let odk = Odk::try_new(port, bus)?; /// # /// # Ok(()) } /// ``` /// /// Note: You would typically use the `env_logger` crate and run with /// `RUST_LOG=debug` to watch the bus messages go by. pub fn try_new(mut port: P, bus: B) -> Result<Self, serial_core::Error> { flipdot_serial::configure_port(&mut port, Duration::from_secs(10))?; Ok(Odk { port, bus }) } /// Reads the next frame from the ODK over the serial port, forwards it /// to the attached bus, and sends the response, if any, back to the ODK. /// /// # Errors /// /// Returns: /// * [`OdkError::Communication`] if there was an error reading or writing the data. /// * [`OdkError::Bus`] if the bus failed to process the message. /// /// # Examples /// /// ```no_run /// # use flipdot_serial::SerialSignBus; /// # use flipdot_testing::{Address, Odk, VirtualSign, VirtualSignBus}; /// # /// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// # /// let bus = VirtualSignBus::new(vec![VirtualSign::new(Address(3))]); /// let port = serial::open("/dev/ttyUSB0")?; /// let mut odk = Odk::try_new(port, bus)?; /// loop { /// odk.process_message()?; /// } /// # /// # Ok(()) } /// ``` pub fn process_message(&mut self) -> Result<(), OdkError> { let response = { let frame = Frame::read(&mut self.port)?; let message = Message::from(frame); self.bus.process_message(message)? }; if let Some(message) = response { let frame = Frame::from(message); frame.write(&mut self.port)?; } Ok(()) } }