xl9555-hal 0.3.0

embedded-hal driver for the XL9555 GPIO expander
Documentation
use embedded_hal::digital::PinState;

use crate::{access::DriverHandler, input::Input, output::Output, types::PinId};

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

impl<'a, A: DriverHandler> Pin<'a, A> {
    pub fn new(access: &'a A, pin: PinId) -> Self {
        Self { access, pin }
    }

    pub fn into_input(self) -> Result<Input<'a, A>, A::Error> {
        self.access
            .set_direction(self.pin, crate::types::Direction::Input)?;
        Ok(Input {
            access: self.access,
            pin: self.pin,
        })
    }

    pub fn into_output(self, level: PinState) -> Result<Output<'a, A>, A::Error> {
        self.access
            .set_direction(self.pin, crate::types::Direction::Output)?;
        self.access.set_level(self.pin, level)?;
        Ok(Output {
            access: self.access,
            pin: self.pin,
        })
    }
}