|
Swadge ESP32-S2
APIs to develop for the Magfest Swadge
|
This component handles both push-buttons which are physical, tactile buttons.
The Swadge mode needs to call checkButtonQueueWrapper(), which calls checkButtonQueue() to receive queued button events. The reason for checkButtonQueueWrapper() is so that the main loop can monitor the button which can be held down to return to the main menu. The event contains which button caused the event, whether it was pressed or released, and the current state of all buttons. This way the Swadge mode is not responsible for high frequency button polling, and can still receive all button inputs.
The push-buttons are polled continuously at 1ms intervals in an interrupt, but these readings are not reported to the Swadge modes. The interrupt saves the prior DEBOUNCE_HIST_LEN polled button states and the last reported button state. When all DEBOUNCE_HIST_LEN button states are identical, the interrupt accepts the current state and checks if it different than the last reported state. If there is a difference, the button event is queued in the interrupt to be received by the Swadge mode.
The push-button GPIOs are all read at the same time using Dedicated GPIO.
Originally the push-buttons would trigger an interrupt, but we found that to have less reliable results with more glitches than polling.
Button events used to be delivered to the Swadge mode via a callback. This led to cases where multiple callbacks would occur between a single invocation of that mode's main function. Because the Swadge mode didn't have a separate queue for button events, this caused events to be dropped. Instead of forcing each mode to queue button events, now each mode must dequeue them rather than having a callback called.
You don't need to call initButtons() or deinitButtons(). The system does at the appropriate times.
You do need to call checkButtonQueueWrapper() and should do so in a while-loop to receive all events since the last check. This should be done in the Swadge mode's main function.
Go to the source code of this file.
Data Structures | |
| struct | buttonEvt_t |
| A button event containing the button that triggered the event, whether it was pressed or released, and the whole button state. More... | |
Enumerations | |
| enum | buttonBit_t { PB_UP = 0x0001 , PB_DOWN = 0x0002 , PB_LEFT = 0x0004 , PB_RIGHT = 0x0008 , PB_A = 0x0010 , PB_B = 0x0020 , PB_START = 0x0040 , PB_SELECT = 0x0080 } |
| Bitmask values for all the different buttons. More... | |
Functions | |
| void | initButtons (const gpio_num_t *pushButtons, uint8_t numPushButtons) |
| Initialize GPIO pushbuttons. | |
| void | deinitButtons (void) |
| Free memory used by the buttons. | |
| void | powerDownButtons (void) |
| Power down the GPIO pushbuttons. | |
| void | powerUpButtons (void) |
| Power up the GPIO pushbuttons. | |
| bool | checkButtonQueue (buttonEvt_t *) |
| Service the queue of button events that caused interrupts This only returns a single event, even if there are multiple in the queue This function may be called multiple times in a row to completely empty the queue. | |
| struct buttonEvt_t |
| Data Fields | ||
|---|---|---|
| uint16_t | state | A bitmask for the state of all buttons. |
| buttonBit_t | button | The button that caused this event. |
| bool | down | True if the button was pressed, false if it was released. |
| uint32_t | time | |
| enum buttonBit_t |
| void initButtons | ( | const gpio_num_t * | pushButtons, |
| uint8_t | numPushButtons ) |
Initialize GPIO pushbuttons.
| pushButtons | A list of GPIOs with pushbuttons to initialize. The list should be in the same order as buttonBit_t, starting at PB_UP |
| numPushButtons | The number of pushbuttons to initialize |
| void deinitButtons | ( | void | ) |
Free memory used by the buttons.
| void powerDownButtons | ( | void | ) |
Power down the GPIO pushbuttons.
| void powerUpButtons | ( | void | ) |
Power up the GPIO pushbuttons.
| bool checkButtonQueue | ( | buttonEvt_t * | evt | ) |
Service the queue of button events that caused interrupts This only returns a single event, even if there are multiple in the queue This function may be called multiple times in a row to completely empty the queue.
| evt | If an event occurred, return it through this argument |