Trait SignBus

Source
pub trait SignBus {
    // Required method
    fn process_message<'a>(
        &mut self,
        message: Message<'_>,
    ) -> Result<Option<Message<'a>>, Box<dyn Error + Send + Sync>>;
}
Expand description

Abstraction over a bus containing devices that are able to send and receive Messages.

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, PageFlipStyle, Sign, SignBus, SignType};
use flipdot::serial::SerialSignBus;
use flipdot_testing::{VirtualSign, VirtualSignBus};

let bus: Rc<RefCell<dyn 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), PageFlipStyle::Manual)])))
};

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§

Source

fn process_message<'a>( &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.

Trait Implementations§

Source§

impl Debug for dyn SignBus

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Implementors§