use crate::{WithInput, WithOutput}; use rp2040_hal::gpio::{ Function, FunctionSioInput, FunctionSioOutput, Pin, PinId, PullType, ValidFunction, }; /// A GPIO pin that can be configured as an input or output. pub struct IoPin where I: PinId + ValidFunction + ValidFunction + ValidFunction, F: Function, P: PullType, { pin: Option>, } impl IoPin where I: PinId + ValidFunction + ValidFunction + ValidFunction, F: Function, P: PullType, { /// Create a new instance of `IoPin` with a GPIO pin. pub fn new(pin: Pin) -> Self { Self { pin: Some(pin) } } } impl WithInput for IoPin where I: PinId + ValidFunction + ValidFunction + ValidFunction, F: Function, P: PullType, { type Input = Pin; fn with_input(&mut self, f: impl Fn(&mut Self::Input) -> R) -> R { let mut input = self.pin.take().unwrap().reconfigure(); let res = f(&mut input); self.pin.replace(input.reconfigure()); res } } impl WithOutput for IoPin where I: PinId + ValidFunction + ValidFunction + ValidFunction, F: Function, P: PullType, { type Output = Pin; fn with_output(&mut self, f: impl Fn(&mut Self::Output) -> R) -> R { let mut output = self.pin.take().unwrap().reconfigure(); let res = f(&mut output); self.pin.replace(output.reconfigure()); res } }