Trait flipdot_core::SignBus [−][src]
Abstraction over a bus containing devices that are able to send and receive Message
s.
Typically SerialSignBus
from flipdot
or VirtualSignBus
from flipdot-testing
are sufficient,
and you do not need to implement this yourself.
Examples
Using SignBus
as a trait object to allow choosing the type of bus at runtime:
use std::cell::RefCell; use std::rc::Rc; use flipdot::{Address, Sign, SignBus, SignType}; use flipdot::serial::SerialSignBus; use flipdot_testing::{VirtualSign, VirtualSignBus}; let bus: Rc<RefCell<SignBus>> = if use_serial() { let port = serial::open("/dev/ttyUSB0")?; Rc::new(RefCell::new(SerialSignBus::try_new(port)?)) } else { Rc::new(RefCell::new(VirtualSignBus::new(vec![VirtualSign::new(Address(3))]))) }; let sign = Sign::new(bus.clone(), Address(3), SignType::Max3000Side90x7); sign.configure()?;
Implementing a custom bus:
use flipdot_core::{Message, SignBus, State}; struct ExampleSignBus {} impl SignBus for ExampleSignBus { fn process_message<'a>(&mut self, message: Message) -> Result<Option<Message<'a>>, Box<dyn std::error::Error + Send + Sync>> { match message { Message::Hello(address) | Message::QueryState(address) => Ok(Some(Message::ReportState(address, State::Unconfigured))), _ => Ok(None), // Implement rest of protocol here... } } }
Required methods
fn process_message<'a>(
&mut self,
message: Message<'_>
) -> Result<Option<Message<'a>>, Box<dyn Error + Send + Sync>>
[src]
&mut self,
message: Message<'_>
) -> Result<Option<Message<'a>>, Box<dyn Error + Send + Sync>>
Sends a message to the bus and returns an optional response.
The caller is the “controller” (e.g. an ODK), and this method conceptually delivers the message to a certain sign on the bus and returns an optional response from it.
Examples
See the trait-level documentation.