feat: improve pin handout logic
This commit is contained in:
parent
c503a195b2
commit
33b4f2d218
1 changed files with 11 additions and 12 deletions
23
src/gpio.rs
23
src/gpio.rs
|
@ -17,12 +17,8 @@ pub enum GpioError {
|
||||||
WrongPinModeError(i32, PinMode, PinMode),
|
WrongPinModeError(i32, PinMode, PinMode),
|
||||||
#[error("mode {0:?} is unsupported")]
|
#[error("mode {0:?} is unsupported")]
|
||||||
UnsupportedModeError(PinMode),
|
UnsupportedModeError(PinMode),
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum PinRetrievalError {
|
|
||||||
#[error("pin {0} has already been retrieved")]
|
#[error("pin {0} has already been retrieved")]
|
||||||
AlreadyRetrieved(i32),
|
PinAlreadyRetrieved(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
|
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
|
||||||
|
@ -120,7 +116,7 @@ pub struct Pin {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pin {
|
impl Pin {
|
||||||
pub fn new(pin_id: i32) -> Result<Self, PinRetrievalError> {
|
pub fn new(pin_id: i32) -> Result<Self, GpioError> {
|
||||||
ensure_library_setup!();
|
ensure_library_setup!();
|
||||||
|
|
||||||
let can_retrieve = !PINS_HANDED_OUT
|
let can_retrieve = !PINS_HANDED_OUT
|
||||||
|
@ -129,12 +125,13 @@ impl Pin {
|
||||||
.contains(&pin_id);
|
.contains(&pin_id);
|
||||||
|
|
||||||
if can_retrieve {
|
if can_retrieve {
|
||||||
Ok(Self {
|
PINS_HANDED_OUT.lock().unwrap().push(pin_id);
|
||||||
pin_id,
|
|
||||||
pin_mode: PinMode::Uninitialized,
|
let pin_mode = unsafe { ffi::getAlt(pin_id).try_into()? };
|
||||||
})
|
|
||||||
|
Ok(Self { pin_id, pin_mode })
|
||||||
} else {
|
} 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<PinMode, GpioError> {
|
pub fn get_gpio_mode(&mut self) -> Result<PinMode, GpioError> {
|
||||||
ensure_library_setup!();
|
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> {
|
pub fn set_gpio_mode(&mut self, mode: PinMode) -> Result<(), GpioError> {
|
||||||
|
|
Loading…
Reference in a new issue