xl9555-hal 0.3.0

embedded-hal driver for the XL9555 GPIO expander
Documentation
use embedded_hal::digital::{ErrorType, OutputPin, PinState, StatefulOutputPin};

use crate::{access::DriverHandler, types::PinId};

pub struct Output<'a, A: DriverHandler> {
    pub(crate) access: &'a A,
    pub(crate) pin: PinId,
}

impl<'a, A: DriverHandler> ErrorType for Output<'a, A> {
    type Error = A::Error;
}

impl<'a, A: DriverHandler> OutputPin for Output<'a, A> {
    fn set_low(&mut self) -> Result<(), Self::Error> {
        self.access.set_low(self.pin)
    }

    fn set_high(&mut self) -> Result<(), Self::Error> {
        self.access.set_high(self.pin)
    }
}

impl<'a, A: DriverHandler> StatefulOutputPin for Output<'a, A> {
    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
        Ok(self.access.get_set_level(self.pin)? == PinState::High)
    }

    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
        Ok(self.access.get_set_level(self.pin)? == PinState::Low)
    }
}

impl<'a, A: DriverHandler> Output<'a, A> {
    pub fn set_low(&mut self) -> Result<(), A::Error> {
        <Self as OutputPin>::set_low(self)
    }

    pub fn set_high(&mut self) -> Result<(), A::Error> {
        <Self as OutputPin>::set_high(self)
    }

    pub fn is_set_high(&mut self) -> Result<bool, A::Error> {
        <Self as StatefulOutputPin>::is_set_high(self)
    }

    pub fn is_set_low(&mut self) -> Result<bool, A::Error> {
        <Self as StatefulOutputPin>::is_set_low(self)
    }
}