rp-hal icon indicating copy to clipboard operation
rp-hal copied to clipboard

Note on how to acquire Pin's number as an integer

Open elfmimi opened this issue 2 years ago • 1 comments

This is not an issue, but a sharing of knowledge that I wanted to have when I started.

It is about rp2040_hal::gpio::Pin .

at v0.9.0 it is like, https://github.com/rp-rs/rp-hal/blob/8f3e8d0f61415297f0f26db80b2ed84ccea64317/rp2040-hal/src/gpio/mod.rs#L161-L165

at v0.8.0 and v0.7.0 respectively, it was like, https://github.com/rp-rs/rp-hal/blob/f6a0d1b944b4fea3b09e8826dd9a8d8145c52442/rp2040-hal/src/gpio/pin.rs#L450-L457

https://github.com/rp-rs/rp-hal/blob/4c8e16aec9373fdeac0e5af2d4ec06fbc999b302/rp2040-hal/src/gpio/pin.rs#L450-L457

Suppose we had a trait like this.

pub trait PinIdNum {
    fn get_id_num(&self) -> u8;
}

for v0.9.0 you can implement it like this.

impl<I, F, P> PinIdNum for Pin<I, F, P> where I: PinId, F: Function, P: PullType {
    fn get_id_num(&self) -> u8 { self.id().num }
}

for v0.8.0 it can be like following two ways.

impl<I, M> PinIdNum for Pin<I, M> where I: PinId, M: PinMode + ValidPinMode<I> {
    fn get_id_num(&self) -> u8 { self.id().num }
}

impl<I, M> PinIdNum for Pin<I, M> where I: PinId, M: PinMode + ValidPinMode<I> {
    fn get_id_num(&self) -> u8 { I::DYN.num }
}

for v0.7.0 only the latter of above two is valid.

impl<I, M> PinIdNum for Pin<I, M> where I: PinId, M: PinMode + ValidPinMode<I> {
    fn get_id_num(&self) -> u8 { I::DYN.num }
}

elfmimi avatar Feb 14 '24 16:02 elfmimi

version correspondence rp-pico 0.6.0 => rp2040-hal 0.7.0 rp-pico 0.7.0 => rp2040-hal 0.8.0 rp-pico 0.8.0 => rp2040-hal 0.9.0

elfmimi avatar Feb 14 '24 16:02 elfmimi