Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
|
A Swadge mode is struct of configuration variables and function pointers, which is the closest thing in C to an object. These provide a common interface so that the system firmware can run each mode. The Swadge's system firmware will initialize peripherals required by the mode and call the mode's function pointers when appropriate.
If a mode does not need a particular function, for example it doesn't do audio handling, it is safe to set the function pointer to NULL
. The function won't be called. All fields must be initialized to something, since an uninitialized field may lead to undefined behavior.
The top level menu will maintain a list of all available modes and the user can pick the mode to run. This approach is similar to apps. Only one mode may run at a single time, and when it runs it will have full system resources.
Each mode must have a single swadgeMode_t. How the Swadge mode works is flexible and left up to the mode's author to determine. Maybe it uses a menu.h, maybe it has a custom UI. All Swadge mode source code should be in the /main/modes
folder. Each mode should have it's own folder to keep source organized.
To build the firmware, the mode's source files must be added to /main/CMakeLists.txt
. The emulator's makefile
automatically finds files to compile recursively, so they do not need to be explicitly listed.
It's best practice not to use 'generic' names for functions and variables, because they may collide with another mode's functions or variables. When possible, functions and variables should be prefixed with something unique, like demo
in the example below.
Adding a mode to the CMakeFile requires adding two separate lines in the idf_component_register section.
under the SRCS section and
under the INCLUDES section.
Function prototypes must be declared before using them to initialize function pointers:
Then functions can be used to initialize a swadgeMode_t:
The declared functions must actually exist somewhere:
The swadgeMode_t should be declared as extern
in a header file so that it can be referenced in the main menu
Add the mode to the menu initializer in mainMenuEnterMode():
Add the mode to the selector logic in mainMenuCb():
Go to the source code of this file.
Data Structures | |
struct | swadgeMode_t |
A struct of all the function pointers necessary for a swadge mode. If a mode does not need a particular function, for example it doesn't do audio handling, it is safe to set the pointer to NULL. It just won't be called. More... | |
Macros | |
#define | EXIT_TIME_US 1000000 |
#define | DEFAULT_FRAME_RATE_US (1000000 / 40) |
the default time between drawn frames, in microseconds (40FPS) | |
Functions | |
bool | checkButtonQueueWrapper (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. | |
void | switchToSwadgeMode (swadgeMode_t *mode) |
void | softSwitchToPendingSwadge (void) |
Switch to the pending Swadge mode without restarting the system. | |
void | deinitSystem (void) |
Deinitialize all components in the system. | |
void | openQuickSettings (void) |
void | setFrameRateUs (uint32_t newFrameRateUs) |
Set the framerate, in microseconds. | |
uint32_t | getFrameRateUs (void) |
Get the current framerate, in microseconds. | |
void | switchToSpeaker (void) |
Enable the speaker (and battery monitor) and disable the microphone. | |
void | switchToMicrophone (void) |
Enable the microphone and disable the speaker (and battery monitor) | |
#define EXIT_TIME_US 1000000 |
#define DEFAULT_FRAME_RATE_US (1000000 / 40) |
the default time between drawn frames, in microseconds (40FPS)
bool checkButtonQueueWrapper | ( | 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.
This is a wrapper for checkButtonQueue() which also monitors the button to return to the main menu
evt | If an event occurred, return it through this argument |
void switchToSwadgeMode | ( | swadgeMode_t * | mode | ) |
Set up variables to synchronously switch the swadge mode in the main loop
mode | A pointer to the mode to switch to |
void softSwitchToPendingSwadge | ( | void | ) |
Switch to the pending Swadge mode without restarting the system.
void deinitSystem | ( | void | ) |
Deinitialize all components in the system.
void openQuickSettings | ( | void | ) |
void setFrameRateUs | ( | uint32_t | newFrameRateUs | ) |
Set the framerate, in microseconds.
newFrameRateUs | The time between frame draws, in microseconds |
uint32_t getFrameRateUs | ( | void | ) |
Get the current framerate, in microseconds.
void switchToSpeaker | ( | void | ) |
Enable the speaker (and battery monitor) and disable the microphone.
void switchToMicrophone | ( | void | ) |
Enable the microphone and disable the speaker (and battery monitor)