Swadge ESP32-S2
APIs to develop for the Magfest Swadge
Loading...
Searching...
No Matches
hdw-btn.h File Reference

Detailed Description

Design Philosophy

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.

Pushbutton Design Philosophy

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.

Usage

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.

Example

// Check all queued button events
{
// Print the current event
printf("state: %04X, button: %d, down: %s\n",
evt.state, evt.button, evt.down ? "down" : "up");
}
buttonBit_t button
The button that caused this event.
Definition hdw-btn.h:85
bool down
True if the button was pressed, false if it was released.
Definition hdw-btn.h:86
uint16_t state
A bitmask for the state of all buttons.
Definition hdw-btn.h:84
A button event containing the button that triggered the event, whether it was pressed or released,...
Definition hdw-btn.h:83
bool checkButtonQueueWrapper(buttonEvt_t *evt)
Service the queue of button events that caused interrupts This only returns a single event,...
Definition swadge.c:894

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.

Data Structure Documentation

◆ buttonEvt_t

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

Enumeration Type Documentation

◆ buttonBit_t

Bitmask values for all the different buttons.

Enumerator
PB_UP 

The up button's bit.

PB_DOWN 

The down button's bit.

PB_LEFT 

The left button's bit.

PB_RIGHT 

The right button's bit.

PB_A 

The A button's bit.

PB_B 

The B button's bit.

PB_START 

The start button's bit.

PB_SELECT 

The select button's bit.

Function Documentation

◆ initButtons()

void initButtons ( const gpio_num_t * pushButtons,
uint8_t numPushButtons )

Initialize GPIO pushbuttons.

Parameters
pushButtonsA list of GPIOs with pushbuttons to initialize. The list should be in the same order as buttonBit_t, starting at PB_UP
numPushButtonsThe number of pushbuttons to initialize

◆ deinitButtons()

void deinitButtons ( void )

Free memory used by the buttons.

◆ powerDownButtons()

void powerDownButtons ( void )

Power down the GPIO pushbuttons.

◆ powerUpButtons()

void powerUpButtons ( void )

Power up the GPIO pushbuttons.

◆ checkButtonQueue()

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.

Parameters
evtIf an event occurred, return it through this argument
Returns
true if an event occurred, false if nothing happened