1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
mod exti;
mod fast_pin;
mod pin;
mod port;

pub use exti::*;
pub use fast_pin::*;
pub use pin::*;
pub use port::*;

pub use crate::ll_api::PinNum;
pub use crate::ll_api::PortNum;

/// Represents different modes for configuring a bidirectional GPIO pin.
pub enum BidirectionPinMode {
    /// Configures the pin as floating input.
    InFloating,
    /// Configures the pin as pull-up input.
    InPullUp,
    /// Configures the pin as pull-down input.
    InPullDown,
    /// Configures the pin as open-drain output.
    OutOD,
    /// Configures the pin as push-pull output.
    OutPP,
}

/// Trait for converting `BidirectionPinMode` into a GPIO initialization flag.
impl ToGpioInitFlag for BidirectionPinMode {
    fn to_flag(&self) -> u32 {
        match self {
            BidirectionPinMode::InFloating => crate::ll_api::GpioInitFlag::InFloating as u32,
            BidirectionPinMode::InPullUp => crate::ll_api::GpioInitFlag::InPU as u32,
            BidirectionPinMode::InPullDown => crate::ll_api::GpioInitFlag::InPD as u32,
            BidirectionPinMode::OutOD => crate::ll_api::GpioInitFlag::OutOD as u32,
            BidirectionPinMode::OutPP => crate::ll_api::GpioInitFlag::OutPP as u32,
        }
    }
}

/// Trait for controlling bidirectional GPIO pins.
pub trait BidirectionPin {
    /// Sets the mode of the GPIO pin.
    fn mode_ctrl(&self, mode: BidirectionPinMode);

    /// Sets the level of the GPIO pin.
    fn set(&self, level: bool);

    /// Gets the current level of the GPIO pin.
    fn get(&self) -> bool;
}

/// Trait for converting types into GPIO initialization flags.
pub trait ToGpioInitFlag {
    fn to_flag(&self) -> u32;
}