You will find in my github (https://github.com/fstengel/m5LvglFiles) a package to manage an M5Stack’s buttons. It is really a WIP. The idea is:
- to use the buttons inside lvgl. For this I can simulate an encoder, navigation with a 3 key keypad or link the physical buttons to lvgl objects.
- to properly read the state of the physical buttons.
Until recently I firmly believed that to read the button state, one had to use an IRQ+ISR solution. However two things made this approach unuesable:
- The ESP32 has issues with IRQs. Sometimes they take time to actualy interrupt if they do, giving a very sluggish feeling to the interface…
- The M5Stack’s hardware (or worse, the underlying ESP32) has an issue. If the wifi is turned on, then pin 39, the gpio linked to button A, gets a very short pulse about every second. Perhaps more if wifi is being used rather than merely being active.
My solution, in following lvgl’s philosophy, is to poll the pins associatied with the physical buttons. For that I use the ESP32’s pulse counter hardware through the espidf module exposed by lvgl. I basically count both raising and falling pulses. An odd count means pressed, an even count means depressed. The spurious pulses caused by wifi, just add an even count and are so short that, unless one is incredibly unlucky, one only detects their passing by an increase of 2 of the count register.