diff --git a/src/gpio.rs b/src/gpio.rs index 5557e38..49bfc67 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -17,12 +17,8 @@ pub enum GpioError { WrongPinModeError(i32, PinMode, PinMode), #[error("mode {0:?} is unsupported")] UnsupportedModeError(PinMode), -} - -#[derive(Error, Debug)] -pub enum PinRetrievalError { #[error("pin {0} has already been retrieved")] - AlreadyRetrieved(i32), + PinAlreadyRetrieved(i32), } #[derive(PartialEq, Eq, Debug, Clone, Copy)] @@ -120,7 +116,7 @@ pub struct Pin { } impl Pin { - pub fn new(pin_id: i32) -> Result { + pub fn new(pin_id: i32) -> Result { ensure_library_setup!(); let can_retrieve = !PINS_HANDED_OUT @@ -129,12 +125,13 @@ impl Pin { .contains(&pin_id); if can_retrieve { - Ok(Self { - pin_id, - pin_mode: PinMode::Uninitialized, - }) + PINS_HANDED_OUT.lock().unwrap().push(pin_id); + + let pin_mode = unsafe { ffi::getAlt(pin_id).try_into()? }; + + Ok(Self { pin_id, pin_mode }) } else { - Err(PinRetrievalError::AlreadyRetrieved(pin_id)) + Err(GpioError::PinAlreadyRetrieved(pin_id)) } } @@ -153,7 +150,9 @@ impl Pin { pub fn get_gpio_mode(&mut self) -> Result { ensure_library_setup!(); - unsafe { ffi::orangepi_get_gpio_mode(self.pin_id).try_into() } + self.pin_mode = unsafe { ffi::orangepi_get_gpio_mode(self.pin_id).try_into()? }; + + Ok(self.pin_mode) } pub fn set_gpio_mode(&mut self, mode: PinMode) -> Result<(), GpioError> {