Buttons & Events¶
After this page you can make a button press change your app.
Buttons on the Cyber Fidget can ring your functions when something happens. You register a callback, and the framework calls it when the user presses, releases, or holds a button.
What is this?¶
Instead of checking "is the button down?" every frame, you tell the system: "When this button is pressed, call this function." That function is a callback. When the event happens, your code runs.
Like a doorbell
You don't stand at the door asking "Is someone there?" You install a doorbell and go about your day. When someone presses it, your bell function rings. Same idea: register a callback, and it gets called when the event happens.
Event types¶
The ButtonManager generates three event types:
| Event | When it fires |
|---|---|
| Pressed | Button transitions from not pressed -> pressed |
| Released | Button transitions from pressed -> not pressed |
| Held | Button has been held down longer than the hold threshold (default 1500 ms). You get one Held event per press. |
You choose which events to handle. Many apps use Pressed for "do the thing" and Released for "back". A release event also follows a hold; it does not distinguish a tap from a long press by itself.
If you would rather check buttons inside update() than register callbacks, buttonManager.wasPressed(button_RightIndex) is true in the one update() that follows a press, and wasReleased() is true in the one that follows a release. isPressed() stays true for as long as the button is held.
The 6 buttons: positions and names¶
The device has 6 physical buttons. You refer to them by index or semantic name:
| Index | Semantic name | Typical use |
|---|---|---|
| 0 | button_TopLeftIndex / button_UpIndex |
Up / primary action |
| 1 | button_TopRightIndex / button_DownIndex |
Down |
| 2 | button_MiddleLeftIndex / button_LeftIndex |
Left |
| 3 | button_MiddleRightIndex / button_RightIndex |
Right |
| 4 | button_BottomLeftIndex / button_SelectIndex |
Back (convention) |
| 5 | button_BottomRightIndex / button_EnterIndex |
Enter / confirm |
Back button convention
BottomLeft (button_BottomLeftIndex) is the standard "back" or "exit" button. Users expect it to return to the menu or previous screen. The studio starter already registers and cleans up this callback. Keep it: it calls the app's end() and MenuManager::instance().returnToMenu().
Code example: callback registration¶
Add these sections into the starter's .cpp file. The bottom-right button adds one to the score. The bottom-left button keeps its exit action. A free static function at the top of the file needs no declaration in the app's .h file.
Inside app methods, use buttonManager. In helpers outside the class, use HAL::buttonManager(). For continuous input you can also read buttonManager.isPressed(button_RightIndex) in update().
In const ButtonEvent& e, & passes the existing event by reference rather than making a copy. const means this function cannot change it.
// Insert at the top of the file, below the #include lines
#include <stdio.h>
static int score = 0;
static void onActionPressed(const ButtonEvent& e) {
if (e.eventType == ButtonEvent_Pressed) {
++score;
}
}
// Insert inside begin(), below setColorsOff();
score = 0;
buttonManager.registerCallback(button_BottomRightIndex, onActionPressed);
// Insert inside update(), just above display.display();
display.clear();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_CENTER);
char text[24];
snprintf(text, sizeof(text), "Score: %d", score);
display.drawString(64, 24, text);
// Insert inside end(), above unregisterButtonCallbacks();
buttonManager.unregisterCallback(button_BottomRightIndex);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
In the starter, nullptr means a pointer has no object. * in a declaration marks a pointer (an address), and before a pointer reads its object. & in a type refers to an existing object; before a value it gives its address. const means the code cannot change the value through that declaration.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
#include <stdio.h>
static int score = 0;
static void onActionPressed(const ButtonEvent& e) {
if (e.eventType == ButtonEvent_Pressed) {
++score;
}
}
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
score = 0;
buttonManager.registerCallback(button_BottomRightIndex, onActionPressed);
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.clear();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_CENTER);
char text[24];
snprintf(text, sizeof(text), "Score: %d", score);
display.drawString(64, 24, text);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
buttonManager.unregisterCallback(button_BottomRightIndex);
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
A ButtonEvent groups information about one button action:
buttonIndex-- Which button (0-5)eventType--ButtonEvent_Pressed,ButtonEvent_Released, orButtonEvent_Heldduration-- Time held on a hold or release event, in ms; zero on a press. Studio apps on the device estimate this from event delivery times.
Functions on this page¶
| Call | What it does |
|---|---|
ButtonManager& HAL::buttonManager()Example: HAL::buttonManager() |
The buttons, for code outside your app's functions. Inside begin(), update() and end(), the starter already calls it buttonManager. |
void buttonManager.registerCallback(int buttonIndex, ButtonCallback callback)Example: buttonManager.registerCallback(button_RightIndex, onRight); |
Call onRight for press, release, and hold events; replaces that button's callback. |
void buttonManager.unregisterCallback(int buttonIndex)Example: buttonManager.unregisterCallback(button_RightIndex); |
Remove the button callback when leaving the app. |
bool buttonManager.isPressed(int buttonIndex) constExample: buttonManager.isPressed(button_RightIndex); |
Read whether the button is down right now. |
bool buttonManager.wasPressed(int buttonIndex) constExample: buttonManager.wasPressed(button_RightIndex); |
Read whether the button was pressed since the previous update(); true for one update() only. |
bool buttonManager.wasReleased(int buttonIndex) constExample: buttonManager.wasReleased(button_RightIndex); |
Read whether the button was released since the previous update(); true for one update() only. |
bool buttonManager.hasCallback(int buttonIndex) constExample: buttonManager.hasCallback(button_RightIndex); |
Check whether a button has a registered callback. |
ButtonCallback buttonManager.getCallback(int buttonIndex) constExample: buttonManager.getCallback(button_RightIndex); |
Read the registered callback, or nullptr if none. |
const int button_BottomLeftIndex = 4Example: button_BottomLeftIndex |
BottomLeft button; aliases refer to the same physical button. |
const int button_BottomRightIndex = 5Example: button_BottomRightIndex |
BottomRight button; aliases refer to the same physical button. |
const int button_DownIndex = button_TopRightIndexExample: button_DownIndex |
Down button; aliases refer to the same physical button. |
const int button_EnterIndex = button_BottomRightIndexExample: button_EnterIndex |
Enter button; aliases refer to the same physical button. |
const int button_LeftIndex = button_MiddleLeftIndexExample: button_LeftIndex |
Left button; aliases refer to the same physical button. |
const int button_MiddleLeftIndex = 2Example: button_MiddleLeftIndex |
MiddleLeft button; aliases refer to the same physical button. |
const int button_MiddleRightIndex = 3Example: button_MiddleRightIndex |
MiddleRight button; aliases refer to the same physical button. |
const int button_RightIndex = button_MiddleRightIndexExample: button_RightIndex |
Right button; aliases refer to the same physical button. |
const int button_SelectIndex = button_BottomLeftIndexExample: button_SelectIndex |
Select button; aliases refer to the same physical button. |
const int button_TopLeftIndex = 0Example: button_TopLeftIndex |
TopLeft button; aliases refer to the same physical button. |
const int button_TopRightIndex = 1Example: button_TopRightIndex |
TopRight button; aliases refer to the same physical button. |
const int button_UpIndex = button_TopLeftIndexExample: button_UpIndex |
Up button; aliases refer to the same physical button. |
ButtonEventType ButtonEvent_NoneExample: ButtonEvent_None |
No button event. |
ButtonEventType ButtonEvent_PressedExample: ButtonEvent_Pressed |
Button went down. |
ButtonEventType ButtonEvent_ReleasedExample: ButtonEvent_Released |
Button went up; duration is time held in ms. |
ButtonEventType ButtonEvent_HeldExample: ButtonEvent_Held |
Button passed the 1500 ms hold threshold; duration is time held in ms. |
Framework detail: ButtonManager internals¶
Advanced: how the firmware handles button events
You do not need these details to build apps in the studio. They describe the firmware internals for readers writing an app by hand and building it into the firmware.
Ring buffer for events¶
Inside the firmware, ButtonManager stores up to 16 events in a ring buffer, a queue that reuses its space. When a press, release, or hold is detected, it pushes an event. The firmware drains the queue and delivers events to the active app. Studio apps run their registered callbacks before their next update().
Debouncing¶
Physical buttons "bounce" -- the electrical signal can flicker for a few milliseconds. The ButtonManager uses a configurable debounce time (default 20 ms). A state change is only registered after the reading has been stable for that duration. This prevents spurious double-presses.
Hold threshold¶
The hold threshold (default 1500 ms) determines when a Held event is generated. You get one Held event per press; it fires after the button has been down for that long.