Pass pin as arg to LED and Switch

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ayuptom
Posts: 1
Joined: Mon Feb 10, 2020 11:38 am

Pass pin as arg to LED and Switch

Post by ayuptom » Mon Feb 10, 2020 3:27 pm

I am using an STM32F411RE with only one user LED and one user button. I'd like to add more to various pins but I'd like to make use of the pyb.LED and pyb.Switch rather .

Is there a way to do this? It would be great if I could declare a pyb.Pin and pass that in as an argument to LED or Switch for example.

User avatar
dhylands
Posts: 3457
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pass pin as arg to LED and Switch

Post by dhylands » Mon Feb 10, 2020 3:56 pm

The pyb.LED module supports upto 4 LEDs.

You need to configure them in your boards mpconfigboard.h file. For example, the PYBV11 board does this:
https://github.com/micropython/micropyt ... .h#L81-L89

The switch module only supports one button.

It would be fairly simple to create your own LED class which allowed arbitrary pins to be used. Implementing your own Switch class would be a bit more complicated if you needed to use the callback method and would be fairly simple if you just used the polled mode. In order to use the callback mode, you need to become familiar with the EXTI module on the STM32.
http://docs.micropython.org/en/latest/l ... xtInt.html

Post Reply