Swadge ESP32-S2/S3
APIs to develop for the Magfest Swadge
Loading...
Searching...
No Matches
highScores.c File Reference

Macros

#define NVS_KEY_USER_HIGH_SCORE   "user_high_score"
#define NVS_KEY_HIGH_SCORES   "high_scores"
#define HIGH_SCORE_COUNT(hs)

Functions

void initHighScores (highScores_t *hs, const char *nvsNamespace)
 Load high score data from NVS.
bool updateHighScores (highScores_t *hs, const char *nvsNamespace, score_t newScores[], uint8_t numNewScores)
 Update the high score table and user high score with new scores from the player or SwadgePass and persist to NVS. This function handles sorting and limiting the number of scores, so no preprocessing is necessary.
void saveHighScoresFromSwadgePass (highScores_t *hs, const char *nvsNamespace, list_t swadgePasses, const struct swadgeMode *mode, int32_t(*fnGetSwadgePassHighScore)(const swadgePassPacket_t *packet))
 Extract high score data received from SwadgePass and save to the high score table. This should be called from your mode's fnEnterMode function after calling initHighScores().
void clearHighScores (const char *nvsNamespace)
 Clear high score data from NVS.
void addHighScoreToSwadgePassPacket (const char *nvsNamespace, swadgePassPacket_t *packet, void(*fnSetSwadgePassHighScore)(swadgePassPacket_t *packet, int32_t highScore))
 Write high score data to SwadgePass packet for sending to other swadges. This should be called from your mode's fnAddToSwadgePassPacket function.
void initDisplayHighScores (highScores_t *hs, displayScore_t ds[])
 Load the usernames and user images for the high score table into memory. When you are finished with the images, call ::freeHighScoreUserImages() to free the memory. It is safe to call this function multiple times before calling ::freeHighScoreUserImages().
void freeDisplayHighScores (highScores_t *hs, displayScore_t ds[])
 Free memory used for user images.

Macro Definition Documentation

◆ NVS_KEY_USER_HIGH_SCORE

#define NVS_KEY_USER_HIGH_SCORE   "user_high_score"

◆ NVS_KEY_HIGH_SCORES

#define NVS_KEY_HIGH_SCORES   "high_scores"

◆ HIGH_SCORE_COUNT

#define HIGH_SCORE_COUNT ( hs)
Value:
(hs->highScoreCount == 0 ? MAX_HIGH_SCORE_COUNT : MIN(hs->highScoreCount, MAX_HIGH_SCORE_COUNT))
#define MAX_HIGH_SCORE_COUNT
Definition highScores.h:39
#define MIN(a, b)
Find the smaller of two numbers.
Definition macros.h:21

Function Documentation

◆ initHighScores()

void initHighScores ( highScores_t * hs,
const char * nvsNamespace )

Load high score data from NVS.

Parameters
hsThe struct that contains the high scores. highScores_t.highScoreCount should be set; other fields will be overwritten
nvsNamespaceThe NVS namespace to read saved high score data from

◆ updateHighScores()

bool updateHighScores ( highScores_t * hs,
const char * nvsNamespace,
score_t newScores[],
uint8_t numNewScores )

Update the high score table and user high score with new scores from the player or SwadgePass and persist to NVS. This function handles sorting and limiting the number of scores, so no preprocessing is necessary.

Parameters
hsThe struct that contains the high scores
nvsNamespaceThe NVS namespace to write data to
newScoresArray of scores to add to the table
numNewScoresCount of scores in the array
Returns
true if the high score table changed, false if it didn't

◆ saveHighScoresFromSwadgePass()

void saveHighScoresFromSwadgePass ( highScores_t * hs,
const char * nvsNamespace,
list_t swadgePasses,
const struct swadgeMode * mode,
int32_t(* fnGetSwadgePassHighScore )(const swadgePassPacket_t *packet) )

Extract high score data received from SwadgePass and save to the high score table. This should be called from your mode's fnEnterMode function after calling initHighScores().

Parameters
hsThe highScores_t struct that contains the high scores
nvsNamespaceThe NVS namespace to write data to
swadgePassesA list_t of SwadgePasses from getSwadgePasses()
modeThe swadgeMode that is consuming these SwadgePasses. They will be marked as used by this mode.
fnGetSwadgePassHighScorePointer to a function that returns your mode's high score from a SwadgePass packet

◆ clearHighScores()

void clearHighScores ( const char * nvsNamespace)

Clear high score data from NVS.

Parameters
nvsNamespaceThe NVS namespace to clear saved high score data from

◆ addHighScoreToSwadgePassPacket()

void addHighScoreToSwadgePassPacket ( const char * nvsNamespace,
swadgePassPacket_t * packet,
void(* fnSetSwadgePassHighScore )(swadgePassPacket_t *packet, int32_t highScore) )

Write high score data to SwadgePass packet for sending to other swadges. This should be called from your mode's fnAddToSwadgePassPacket function.

Parameters
nvsNamespaceThe NVS namespace to read data from
packetThe SwadgePass packet to modify
fnSetSwadgePassHighScorePointer to a function that sets your mode's high score field in a SwadgePass packet

◆ initDisplayHighScores()

void initDisplayHighScores ( highScores_t * hs,
displayScore_t ds[] )

Load the usernames and user images for the high score table into memory. When you are finished with the images, call ::freeHighScoreUserImages() to free the memory. It is safe to call this function multiple times before calling ::freeHighScoreUserImages().

Parameters
hsThe highScores_t struct that contains the high scores
dsArray of display structs to be populated. This array must be the same length as highScores_t.highScoreCount.

◆ freeDisplayHighScores()

void freeDisplayHighScores ( highScores_t * hs,
displayScore_t ds[] )

Free memory used for user images.

Parameters
hsThe highScores_t struct that contains the high scores
dsThe ::displayScores_t array that contains user images to be freed