I also tried some other methods since, none of them had the desired effect:
Method 2 - Pin buttons
- This method works only with mode High (pull_low=False), with or without pull
- It is incredibly slow to respond (about 1 second)
import os, sys, io
import M5
from M5 import *
from hardware import *
import time
Btn37 = None
Btn39 = None
Btn35 = None
def btn37_wasPressed_event(state):
global Btn37, Btn39, Btn35
print('Pressed A')
def btn39_wasPressed_event(state):
global Btn37, Btn39, Btn35
print('Pressed B')
def btn35_wasPressed_event(state):
global Btn37, Btn39, Btn35
print('Pressed PWR')
def btn35_wasReleased_event(state):
global Btn37, Btn39, Btn35
print('Released PWR')
def btn39_wasReleased_event(state):
global Btn37, Btn39, Btn35
print('Released B')
def btn37_wasReleased_event(state):
global Btn37, Btn39, Btn35
print('Released A')
def setup():
global Btn37, Btn39, Btn35
print('hello M5')
M5.begin()
# Pin 37 == button A
# Pin 39 == button B
# Pin 35 == button PWR (aka button C)
Btn37 = Button(37, active_low=False, pullup_active=False)
Btn37.setCallback(type=Btn37.CB_TYPE.WAS_PRESSED, cb=btn37_wasPressed_event)
Btn37.setCallback(type=Btn37.CB_TYPE.WAS_RELEASED, cb=btn37_wasReleased_event)
Btn39 = Button(39, active_low=False, pullup_active=False)
Btn39.setCallback(type=Btn39.CB_TYPE.WAS_PRESSED, cb=btn39_wasPressed_event)
Btn39.setCallback(type=Btn39.CB_TYPE.WAS_RELEASED, cb=btn39_wasReleased_event)
Btn35 = Button(35, active_low=False, pullup_active=False)
Btn35.setCallback(type=Btn35.CB_TYPE.WAS_PRESSED, cb=btn35_wasPressed_event)
Btn35.setCallback(type=Btn35.CB_TYPE.WAS_RELEASED, cb=btn35_wasReleased_event)
def loop():
global Btn37, Btn39, Btn35
M5.update()
Btn37.tick(None)
Btn39.tick(None)
Btn35.tick(None)
time.sleep_ms(50)
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")
Method 3 - machine.Pin (IRQ)
- It is fast, responsive
- It cannot report accurately if the button was pressed or released if multiple events are queued one after the other
import os, sys, io
import M5
from M5 import *
import time
from hardware import *
from machine import Pin
pin37 = None
pin39 = None
pin35 = None
p = None
# Describe this function...
def button_callback(p):
global pin37, pin39, pin35
i = int(str(p)[4:-1])
print(f"Button id: {i}, value: {p.value()}")
def setup():
global pin37, pin39, pin35
print('hello M5')
M5.begin()
# Pin 37 == button A
# Pin 39 == button B
# Pin 35 == button PWR (aka button C)
pin37 = Pin(37, mode=Pin.IN)
pin39 = Pin(39, mode=Pin.IN)
pin35 = Pin(35, mode=Pin.IN)
pin37.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=button_callback)
pin39.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=button_callback)
pin35.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=button_callback)
def loop():
global pin37, pin39, pin35
M5.update()
time.sleep_ms(50)
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")
Edit:
Oh, right, I almost forgot.
There is no such thing as the following in the machine.Pin method:
p.irq().flags()
I don't know if it is because of the micropython port you guys use or if it is the esp32-pico-v3-02's fault, but I think there is something missing there...