Skip to content

How an App Works

After this page you can set up, update and clean up a Cyber Fidget app.

Every Cyber Fidget app follows the same lifecycle pattern: begin -> update (repeated) -> end. Understanding this pattern is the key to writing apps that feel at home on the device.


What is this?

When you launch an app, the firmware does three things in order:

  1. begin() -- Runs once when the app starts. Set up your state, register button handlers, load resources.
  2. update() -- Runs over and over, about 50 times per second. This is where your app "lives": read inputs, update logic, draw to the screen.
  3. end() -- Runs once when the user exits. Clean up: unregister buttons, free resources, save state if needed.

Think of it like a board game

  • begin() = Setting up the board, dealing cards, placing pieces.
  • update() = Playing turns -- each call is like one turn.
  • end() = Putting everything back in the box when you're done.

The lifecycle in practice

  • begin() runs once at start -- use it for one-time setup.
  • update() runs ~50 times per second -- use it for everything that happens during the app.
  • end() runs once on exit -- use it to leave things tidy for the next app.

Going to sleep is not an exit

end() runs when someone leaves your app, not when the Fidget falls asleep. Waking from sleep starts the Fidget fresh: your app's variables are reset, and the next time your app opens, begin() runs again from the top.

Why ~50 times per second?

The firmware schedules app updates every 20 ms (TASK_20MS), which gives roughly 50 updates per second. See The Update Loop for details.


Code example: reset and draw a score

Start with Code from Blank Template in the studio. Add each section into the .cpp file where its comment says. "At the top" means below the #include lines and above the app object, not inside a method. Keep the starter's button setup and cleanup, including setColorsOff() in begin() and end(); its bottom-left button returns to the menu.

// Insert at the top of the file, below the #include lines
#include <stdio.h>
static int score = 0;

// Insert inside begin(), below setColorsOff();
score = 0;

// 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);
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.

MyApp.cpp
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"

#include <stdio.h>
static int score = 0;
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;
}

// 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() {
    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);
}

snprintf() writes the score into a fixed-size char buffer, an array that holds the text. The screen draws that text. Change score in your app logic to change what appears. To make a button add to the score, see Buttons & Events.

The studio adds your app to the menu using the Menu Category you choose. You do not edit a manifest for a studio app.


Functions on this page

Call What it does
MenuManager& MenuManager::instance().instance()
Example: MenuManager& menu = MenuManager::instance();
Get the shared menu controller.
void MenuManager::instance().returnToMenu()
Example: MenuManager::instance().returnToMenu();
Go back to the menu. Your app closes once the function that called this (for example a button callback) finishes.
unsigned long millis_APP_LASTINTERACTION
Example: millis_APP_LASTINTERACTION = millis();
Set this to millis() to count as activity, so the device does not go to sleep while your app runs.

Framework details: how it all connects

Advanced: how the firmware runs your app

You do not need these details to build apps in the studio. The studio and firmware handle app registration and scheduling for you; this section is for writing an app by hand and building it into the firmware.

Firmware-source excerpt: built-in apps are registered in AppManifest.h with APP_ENTRY. This excerpt assumes the app class, global myApp instance and app identifier are defined in the firmware; it is not code to add into a studio app.

APP_ENTRY(APP_MY_APP, "My App", "Games",
    [](){ myApp.begin(); },
    [](){ myApp.end(); },
    [](){ myApp.update(); }
)

AppManager calls your functions

Inside the firmware, AppManager owns the app lifecycle. A singleton is an object shared through one instance. When the user selects your app from the menu:

  1. It calls appDefs[appActive].endFunc() for the previous app.
  2. It sets appActive to your app.
  3. It calls appDefs[appActive].beginFunc() for your app.

When the 20 ms update interval has elapsed, it calls appDefs[appActive].runFunc() -- which is your update().

The APP_ENTRY macro

A macro is a named piece of code expanded by the C++ preprocessor before compilation. These details apply to built-in firmware apps.

APP_ENTRY expands into an entry in the appDefs[] array. The macro takes:

  • Enum name -- e.g. APP_MY_APP
  • Menu label -- What appears in the app menu
  • Category path -- e.g. "Games" or "Tools/WiFi"
  • beginFunc -- Function to call when the app starts
  • endFunc -- Function to call when the app exits
  • runFunc -- Function to call every ~20 ms (your update loop)

Static instance pattern

Built-in apps commonly use a single global instance (e.g. MyApp myApp) and pass lambdas, short unnamed functions, that call its methods. This keeps the manifest clean and lets each app own its state.

Reopening a built-in app after sleep

A built-in app can reopen when the Fidget wakes from idle sleep, and in Dev mode after a firmware update. Counter and Home Remote do; studio apps don't. This needs firmware 1.7.0 or later.

  • Opt in. Add the app's enum name to resumeEligibleApps in lib/AppDefs/AppDefs.h. The firmware records the open app just before it sleeps, and opens it again on the next button wake.
  • Save your own state while the app runs. Deep sleep clears memory, and end() doesn't run before sleep, so anything the app should remember must already be saved. Counter saves its counts to flash a couple of seconds after the last press, so a burst of taps is one write and the counts are saved long before idle sleep.
  • Load it in begin(). A reopened app starts like any other: begin() runs from the top, so read the saved state there.
  • Nothing else changes. The press that wakes the Fidget never reaches the app, and Back returns to the menu folder the app was opened from. An ordinary start (after switching off with Power Manager, a restart, a low battery, or a firmware update outside Dev mode) lands on the menu, not in the reopened app. A restart the firmware asks for on purpose, such as opening the Web Portal or the Music Player, still opens the app it was asked to open.