Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access the Pins by Index #382

Closed
TheFrozenDuck opened this issue Jul 10, 2022 · 7 comments
Closed

Access the Pins by Index #382

TheFrozenDuck opened this issue Jul 10, 2022 · 7 comments

Comments

@TheFrozenDuck
Copy link

The documentation give a rather good overview of how to access pins using a pin's name. What I could not find though was how to access a pin by index. I would like to avoid matching like this:

match pin {
    0=>pins.gpio0.into_mode(),
    1=>pins.gpio1.into_mode(),
    2=>pins.gpio2.into_mode(),
    ...

Would that be possible to do?

@jannic
Copy link
Member

jannic commented Jul 10, 2022

No, I don't think there is an easy way.
The current API makes sure that you can't get two handles to the same pin, at compile time. That would be difficult to achieve if you could somehow get the pin for a given pin number.
The closest thing I could imagine would be something like a function on rp2040_hal::gpio::pin::bank0::Pins:

pub fn to_dynpin_array(self) -> [DynPin; 30]

But currently no such thing exitsts, and I'm not sure how useful it would be.

@TheFrozenDuck
Copy link
Author

Thank you for the technological background. I agree with you that adding such thing is probably not utterly useful.

I only need it to do some multiplexing and that is problem not the most common use-case for this.

@koriwi
Copy link

koriwi commented Apr 13, 2023

Hope necromancing to prevent duplicate issues is ok.
In my usecase (freedeck-firmware) the user can set some pins which are stored in a config file on an sdcard. The only hard coded pins are the spi spins for the sdcard. the rest should be possible to be dynamic so people don't have to recompile everything just to change some pins. This is currently blocking me from porting this project to rust.

Maybe you have a solution for me or i gave you a good idea why it could be needed.

I'm currently doing it like this:

...
let mut pin_array: [Option<DynPin>; 29] = [
        Some(pins.gpio0.into()),
        Some(pins.gpio1.into()),
        Some(pins.gpio2.into()),
        Some(pins.gpio3.into()),
        Some(pins.gpio4.into()),
        Some(pins.gpio5.into()),
        Some(pins.gpio6.into()),
        Some(pins.gpio7.into()),
        Some(pins.gpio8.into()),
        Some(pins.gpio9.into()),
        Some(pins.gpio10.into()),
        Some(pins.gpio11.into()),
        Some(pins.gpio12.into()),
        Some(pins.gpio13.into()),
        Some(pins.gpio14.into()),
        Some(pins.gpio15.into()),
        Some(pins.gpio16.into()),
        Some(pins.gpio17.into()),
        Some(pins.gpio18.into()),
        Some(pins.gpio19.into()),
        Some(pins.gpio20.into()),
        Some(pins.gpio21.into()),
        Some(pins.gpio22.into()),
        None,
        None,
        None,
        Some(pins.gpio26.into()),
        Some(pins.gpio27.into()),
        Some(pins.gpio28.into()),
    ];
 ...
    

@jannic
Copy link
Member

jannic commented Apr 13, 2023

IMHO adding comments to old tickets is ok. However they are not well visible, so it might easily happen that you don't get an answer.
About your question: I think your code, using pin_array, is fine. It has the big advantage of being safe, so you don't risk to accidentally use the same pin twice, concurrently.
If you really want to construct a pin type from its number, you'd need something like #482. @9names, this PR has been in draft state for several months. Do you think it still needs work?

@koriwi
Copy link

koriwi commented Apr 13, 2023

Hi Jannic. I hoped somebody would see it. If not i would have done something else :)
Yes. my code is safe which is what i ultimately want, so i will probably keep it that way. I saw the issue where this pr comes from. I don't really do interop stuff and i also do not like unsafe. Sooooo I'm probably not the guy to ask regardings this draft :P

@jannic
Copy link
Member

jannic commented Apr 13, 2023

Ultimately, any safe function providing this functionality probably needs to contain a data structure similar to your pin_array. It might hold a compressed form (a bitmap of all available pins) in a single u32 to reduce the memory footprint. But that would complicate the code, so it's a tradeoff.
There's room for a crate providing such tools, but personally I don't think it's a good fit for the HAL. It's a higher level concept.

@koriwi
Copy link

koriwi commented Apr 13, 2023

I'm fine with that.
Maybe we could put a notice somewhere for noobies like me that come from Arduino or similar and are used to dynamically indexing pins. Was scratching my head a while before i came up with this :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants