use hal::digital::v2::InputPin;
use hal::digital::v2::OutputPin;
pub trait ExpanderIO {
type Error;
fn write_port(&self, port: u8, bit: bool) -> Result<(), Self::Error>;
fn read_port(&self, port: u8) -> Result<bool, Self::Error>;
}
pub struct PortPin<'io, IO: ExpanderIO> {
io: &'io IO,
port: u8,
}
impl<'io, IO: ExpanderIO> PortPin<'io, IO> {
pub(crate) fn new(io: &'io IO, port: u8) -> Self {
Self { io, port }
}
}
impl<'io, IO: ExpanderIO> OutputPin for PortPin<'io, IO> {
type Error = IO::Error;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.io.write_port(self.port, true)
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.io.write_port(self.port, false)
}
}
impl<'io, IO: ExpanderIO> InputPin for PortPin<'io, IO> {
type Error = IO::Error;
fn is_high(&self) -> Result<bool, Self::Error> {
self.io.read_port(self.port)
}
fn is_low(&self) -> Result<bool, Self::Error> {
self.io.read_port(self.port).map(|hi| !hi)
}
}