Documentation Improvement: Pushbutton Suppress argument
See original GitHub issueHi, This library is awesome. I couldn’t believe how simple it was to create tasks and to use buttons. I was able to do in hours what took me days/weeks in Arduino. Also, kudos that there is documentation on Github!
However, it took me a few tries to figure out how to implement multi-function push buttons (single, long, and double). I think as micropython grows in popularity, a lot more students, non-native speakers, and people who are not computer scientists will use this library. I wonder if the documentation could be expanded to offer more “goal-oriented how to’s”. The trouble I had was that I initially set up a press_func, and then wanted to add more functionality from there. I’m sure many people will take a similar approach. After creating more tasks which used different methods, I began testing. Then I saw my console log fill up with undesired behavior. I’m sure many others will also get frustrated to see that when they try to set up double or long presses, also get short presses. It took me a few several reads, and a lot of experimenting, to figure out how to modify my code to use the suppress function correctly. For me, the concept of “suppress” didn’t really enter my mind. https://github.com/peterhinch/micropython-async/blob/master/v3/docs/DRIVERS.md#411-the-suppress-constructor-argument
This would probably make for a great tutorial in a book, an article on a blog, or a youtube video, but why not expand this technical documentation here to make it more accessible to new users? Now, I’m not sure if there is a style guide that this project adheres to, or if the maintainers are against including animated gifs of both the console and a fritzing-style breadboard pressing a button. But here’s my suggestion for a code snippet:
(4.1.1.1) Multi-Function Push Buttons Implementation
This code snippet demonstrates how to use which code to interpret push button single (release), double (presses), and long presses together without undesired events getting triggered. Setting the “suppress=True” argument is critical to making these events cooperate.
from machine import Pin
import uasyncio
import Pushbutton
btn = Pin(<your chosen pin>, Pin.IN, Pin.PULL_UP, suppress=True) # Assumes a Raspberry Pi Pico
pb = Pushbutton(btn)
async def short_press():
    print("SHORT")
    await uasyncio.sleep_ms(1000)
    
async def double_press():
    print("DOUBLE")
    await uasyncio.sleep_ms(1000)
async def long_press():
    print("LONG")
    await uasyncio.sleep_ms(1000)
async def main():
    q = queue.Queue()
    short_press = pb.release_func(short_press)
    double_press = pb.double_func(double_press)
    long_press = pb.long_func(long_press)
    while True:
        await uasyncio.sleep_ms(1000)```
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)

 Top Related Medium Post
Top Related Medium Post Top Related StackOverflow Question
Top Related StackOverflow Question
OK, I’ll go with that. I’ve pushed an update, with a few very minor tweaks to the wording and formatting.
Thank you for the contribution.
I have added the above script here. Thanks for the suggestion.