switch callback on press & release
switch callback on press & release
Is it possible to have a callback on a switch release as well as the initial press? If so, how do you configure it?
Re: switch callback on press & release
You can by using ExtInt: http://docs.micropython.org/en/latest/l ... xtInt.html
The switch module is built internal using the ExtInt module,
I have an example that uses ExtInt for decoding a rotary encoder on rising and falling edges: https://github.com/dhylands/upy-example ... /rotary.py
You could also modify the mpconfigboard.h file. For the PYBV11, for example, this is what determines which edges cause the switch callback to be invoked:
https://github.com/micropython/micropyt ... oard.h#L78
You would use GPIO_MODE_IT_RISING_FALLING to have the callback called on both edges.
Note that you'll want to have some debounce circuitry or logic since you may get multple presses/releases per button press otherwise.
The switch module is built internal using the ExtInt module,
I have an example that uses ExtInt for decoding a rotary encoder on rising and falling edges: https://github.com/dhylands/upy-example ... /rotary.py
You could also modify the mpconfigboard.h file. For the PYBV11, for example, this is what determines which edges cause the switch callback to be invoked:
https://github.com/micropython/micropyt ... oard.h#L78
You would use GPIO_MODE_IT_RISING_FALLING to have the callback called on both edges.
Note that you'll want to have some debounce circuitry or logic since you may get multple presses/releases per button press otherwise.
Re: switch callback on press & release
Thanks. Another gem in the documentation I missed. Usually I use MAX681x switch debouncers. Pricey little hummers but they are worth it in my book. Steve
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: switch callback on press & release
Another approach is to use asynchronous programming with uasyncio. This repo contains various resources including a tutorial on uasyncio. It also offers classes for debounced Switch and Pushbutton objects. The former supports open and close callbacks, the latter extends it to include long press and double-click callbacks.
And, as a software solution, it's freePricey little hummers
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.