isPressed? wasPressed? What's the difference?



  • My impression was that isPressed reads the immediate state of the button, but wasPressedindicates that the button was pressed at some point. But trying it out, that doesn't seem to be the case:

    for count in range(10000):
      wait_ms(1)
    label0.setText(str(btnA.wasPressed()))
    

    Displays False, as does putting isPressed() in there, no matter how many times I press the button.

    So, what is the difference?



  • Ispressed is used to preform an action when a button is pressed.
    Waspressed is used when you want an action dependent on how long the button was held down for.

    Ispressed is a command block and requires other blocks and commands for use whereas,
    Waspressed is a loop function that is used to control other blocks/commands.



  • @dclaar isPressed() reflects current state of the button (is it pressed?), and returns a Boolean. wasPressed() is an event trigger you use to execute code when the button is pressed.

    Sample code below (generated from uiflow) to illustrate.

    from m5stack import *
    from m5ui import *
    from uiflow import *
    lcd.setRotation(1)
    
    setScreenColor(0x111111)
    
    isExit = None
    
    def buttonB_wasPressed():
      global isExit
      isExit = True
      pass
    btnB.wasPressed(buttonB_wasPressed)
    
    setScreenColor(0x000000)
    axp.setLcdBrightness(40)
    isExit = False
    while isExit == False:
      lcd.print((btnA.isPressed()), 3, 0, 0xffffff)