The Accessory button now selects team in the Configuring state. (#1)

With the advent of the new State Machine, there is opportunity in the *Configuring* state for a player to select his team by pressing the *Accessory* button.  The Mobile App or IR remote can also be used, as before.

![SystemK State Machine.drawio.png](/attachments/0b93eee9-15cd-4dea-9961-e4777868495e)

I also consolidated some duplicated code into `Set_Team_With_Audio_Feedback()`.

Co-authored-by: Joe Kearney <joe@clubk.club>
Reviewed-on: #1
This commit is contained in:
Joe Kearney 2025-01-28 02:06:21 +00:00
parent 6f51f5b006
commit 4fe072f2d3
4 changed files with 189 additions and 141 deletions

View file

@ -29,18 +29,49 @@
//! Implemented according to the 2024-07-20 "KTag Teams Compatibility Matrix". //! Implemented according to the 2024-07-20 "KTag Teams Compatibility Matrix".
typedef enum typedef enum
{ {
TEAM_PURPLE = 0, TEAM_PURPLE = 0,
TEAM_RED = 1, BASIC_TEAMS_MINIMUM = 0,
TEAM_BLUE = 2, TEAM_RED = 1,
TEAM_WHITE = 3, TEAM_BLUE = 2,
COMMON_TEAM_ID_MASK = 3, BASIC_TEAMS_MAXIMUM = 2,
TEAM_ORANGE = 4, TEAM_WHITE = 3,
TEAM_GREEN = 5, COMMON_TEAM_ID_MASK = 3,
TEAM_YELLOW = 6, TEAM_ORANGE = 4,
TEAM_BLACK = 7, TEAM_GREEN = 5,
EXTENDED_TEAM_ID_MASK = 7 TEAM_YELLOW = 6,
TEAM_BLACK = 7,
EXTENDED_TEAM_ID_MASK = 7,
MAXIMUM_TEAM_ID = 7
} TeamID_t; } TeamID_t;
__attribute__((always_inline)) static inline SystemKResult_T Set_Team_With_Audio_Feedback(uint8_t team_ID)
{
static uint8_t Team_ID = 0; // This is static because AUDIO_PRONOUNCE_NUMBER_0_TO_100 needs a *pointer*.
SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
Team_ID = team_ID;
if (Team_ID > MAXIMUM_TEAM_ID)
{
result = SYSTEMK_RESULT_OVERFLOW;
}
else
{
result = SETTINGS_set_uint8_t(SYSTEMK_SETTING_TEAMID, Team_ID);
if (result == SYSTEMK_RESULT_SUCCESS)
{
AudioAction_T audio_action = {.ID = AUDIO_PLAY_TEAM_ID_PROMPT, .Play_To_Completion = true, .Data = (void *)0x00};
Perform_Audio_Action(&audio_action);
AudioAction_T volume_action = {.ID = AUDIO_PRONOUNCE_NUMBER_0_TO_100, .Play_To_Completion = true, .Data = (void *)&Team_ID};
Perform_Audio_Action(&volume_action);
}
}
return result;
}
__attribute__((always_inline)) inline TeamID_t Resolve_Common_Team_ID(uint8_t team_ID) __attribute__((always_inline)) inline TeamID_t Resolve_Common_Team_ID(uint8_t team_ID)
{ {
return (team_ID & COMMON_TEAM_ID_MASK); return (team_ID & COMMON_TEAM_ID_MASK);
@ -50,7 +81,7 @@ __attribute__((always_inline)) inline bool Team_Can_Tag_Me(uint8_t tagging_team_
{ {
bool can_tag = false; bool can_tag = false;
uint8_t team_ID; uint8_t team_ID;
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &team_ID); (void)SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &team_ID);
TeamID_t my_common_team_ID = Resolve_Common_Team_ID(team_ID); TeamID_t my_common_team_ID = Resolve_Common_Team_ID(team_ID);
if (tagging_team_ID == TEAM_PURPLE) if (tagging_team_ID == TEAM_PURPLE)
@ -94,7 +125,7 @@ __attribute__((always_inline)) inline void Set_Health(uint8_t health)
if (KTAG_Game_Data.My_Health == 0) if (KTAG_Game_Data.My_Health == 0)
{ {
KEvent_T tagged_out_event = { .ID = KEVENT_TAGGED_OUT, .Data = (void *)0x00 }; KEvent_T tagged_out_event = {.ID = KEVENT_TAGGED_OUT, .Data = (void *)0x00};
Post_KEvent(&tagged_out_event); Post_KEvent(&tagged_out_event);
} }
} }
@ -108,7 +139,7 @@ __attribute__((always_inline)) inline void Reduce_Health(uint8_t reduction)
else else
{ {
KTAG_Game_Data.My_Health = 0; KTAG_Game_Data.My_Health = 0;
KEvent_T tagged_out_event = { .ID = KEVENT_TAGGED_OUT, .Data = (void *)0x00 }; KEvent_T tagged_out_event = {.ID = KEVENT_TAGGED_OUT, .Data = (void *)0x00};
Post_KEvent(&tagged_out_event); Post_KEvent(&tagged_out_event);
} }
} }

View file

@ -22,8 +22,10 @@
#include "SystemK.h" #include "SystemK.h"
#define MIN_TEAM_ID 0 #define MIN_TEAM_ID BASIC_TEAMS_MINIMUM
#define MAX_TEAM_ID 3 #define MAX_TEAM_ID BASIC_TEAMS_MAXIMUM
static const char * KLOG_TAG = "Team ID Menu";
static uint8_t Team_ID; static uint8_t Team_ID;
@ -73,13 +75,11 @@ static void OnIncrement()
{ {
Team_ID = MAX_TEAM_ID; Team_ID = MAX_TEAM_ID;
} }
(void) SETTINGS_set_uint8_t(SYSTEMK_SETTING_TEAMID, Team_ID);
AudioAction_T audio_action = {.ID = AUDIO_PLAY_TEAM_ID_PROMPT, .Play_To_Completion = true, .Data = (void *)0x00}; if (Set_Team_With_Audio_Feedback(Team_ID) != SYSTEMK_RESULT_SUCCESS)
Perform_Audio_Action(&audio_action); {
KLOG_WARN(KLOG_TAG, "Failed to increment team!");
AudioAction_T volume_action = {.ID = AUDIO_PRONOUNCE_NUMBER_0_TO_100, .Play_To_Completion = true, .Data = (void *)&Team_ID}; }
Perform_Audio_Action(&volume_action);
} }
static void OnDecrement() static void OnDecrement()
@ -94,11 +94,9 @@ static void OnDecrement()
{ {
Team_ID = MIN_TEAM_ID; Team_ID = MIN_TEAM_ID;
} }
(void) SETTINGS_set_uint8_t(SYSTEMK_SETTING_TEAMID, Team_ID);
AudioAction_T audio_action = {.ID = AUDIO_PLAY_TEAM_ID_PROMPT, .Play_To_Completion = true, .Data = (void *)0x00}; if (Set_Team_With_Audio_Feedback(Team_ID) != SYSTEMK_RESULT_SUCCESS)
Perform_Audio_Action(&audio_action); {
KLOG_WARN(KLOG_TAG, "Failed to decrement team!");
AudioAction_T volume_action = {.ID = AUDIO_PRONOUNCE_NUMBER_0_TO_100, .Play_To_Completion = true, .Data = (void *)&Team_ID}; }
Perform_Audio_Action(&volume_action);
} }

View file

@ -22,14 +22,14 @@
/* Include Files */ /* Include Files */
#include "SystemK.h" #include "SystemK.h"
static void Configuring_Entry(StateMachineContext_T * context); static void Configuring_Entry(StateMachineContext_T *context);
static void Configuring_Do(StateMachineContext_T * context); static void Configuring_Do(StateMachineContext_T *context);
static void Configuring_Exit(StateMachineContext_T * context); static void Configuring_Exit(StateMachineContext_T *context);
#define MAX_MENU_DEPTH 10 #define MAX_MENU_DEPTH 10
static MenuItem_T const * CurrentMenuItem; static MenuItem_T const *CurrentMenuItem;
static uint8_t MenuItemHistoryIndex = 0; static uint8_t MenuItemHistoryIndex = 0;
static MenuItem_T const * MenuItemHistory[MAX_MENU_DEPTH]; static MenuItem_T const *MenuItemHistory[MAX_MENU_DEPTH];
static const uint16_t LONG_PRESS_FOR_READY_in_ms = 5000; static const uint16_t LONG_PRESS_FOR_READY_in_ms = 5000;
@ -37,17 +37,16 @@ static const char *KLOG_TAG = "STATE_CONFIGURING";
//! Activities for the **Configuring** state. //! Activities for the **Configuring** state.
const StateActivity_T STATE_CONFIGURING_Activities = const StateActivity_T STATE_CONFIGURING_Activities =
{ {
.Entry = Configuring_Entry, .Entry = Configuring_Entry,
.Do = Configuring_Do, .Do = Configuring_Do,
.Exit = Configuring_Exit .Exit = Configuring_Exit};
};
//! Sets up the Configuring state. //! Sets up the Configuring state.
/*! /*!
* \param context Context in which this state is being run. * \param context Context in which this state is being run.
*/ */
static void Configuring_Entry(StateMachineContext_T * context) static void Configuring_Entry(StateMachineContext_T *context)
{ {
LOG("Entering the Configuring state."); LOG("Entering the Configuring state.");
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_MENU, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00}; NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_MENU, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
@ -68,7 +67,7 @@ static void Configuring_Entry(StateMachineContext_T * context)
/*! /*!
* \param context Context in which this state is being run. * \param context Context in which this state is being run.
*/ */
static void Configuring_Do(StateMachineContext_T * context) static void Configuring_Do(StateMachineContext_T *context)
{ {
portBASE_TYPE xStatus; portBASE_TYPE xStatus;
static KEvent_T Event; static KEvent_T Event;
@ -79,103 +78,123 @@ static void Configuring_Do(StateMachineContext_T * context)
{ {
switch (Event.ID) switch (Event.ID)
{ {
case KEVENT_MENU_ENTER: case KEVENT_MENU_ENTER:
if (CurrentMenuItem->OnFocus != NULL)
{
CurrentMenuItem->OnFocus(true);
}
break;
case KEVENT_MENU_SELECT:
{
if (CurrentMenuItem->OnSelect != NULL)
{
MenuItem_T const *menuItem = CurrentMenuItem->OnSelect();
// Check to see if we have entered a submenu.
if (menuItem != NULL)
{
// Save off the old menu item, so we can get back to it later.
MenuItemHistoryIndex++;
MenuItemHistory[MenuItemHistoryIndex] = menuItem;
CurrentMenuItem = menuItem;
if (CurrentMenuItem->OnFocus != NULL)
{
CurrentMenuItem->OnFocus(true);
}
}
}
}
break;
case KEVENT_MENU_BACK:
if (MenuItemHistoryIndex > 0)
{
// Go up a menu.
MenuItemHistoryIndex--;
CurrentMenuItem = MenuItemHistory[MenuItemHistoryIndex];
if (CurrentMenuItem->OnFocus != NULL) if (CurrentMenuItem->OnFocus != NULL)
{ {
CurrentMenuItem->OnFocus(true); CurrentMenuItem->OnFocus(true);
} }
break; }
break;
case KEVENT_MENU_SELECT: case KEVENT_MENU_UP:
{
if (CurrentMenuItem->OnIncrement != NULL)
{
CurrentMenuItem->OnIncrement();
}
}
break;
case KEVENT_MENU_DOWN:
{
if (CurrentMenuItem->OnDecrement != NULL)
{
CurrentMenuItem->OnDecrement();
}
}
break;
case KEVENT_MENU_EXIT:
while (MenuItemHistoryIndex > 0)
{
// Go up a menu.
MenuItemHistoryIndex--;
CurrentMenuItem = MenuItemHistory[MenuItemHistoryIndex];
if (CurrentMenuItem->OnFocus != NULL)
{ {
if (CurrentMenuItem->OnSelect != NULL) CurrentMenuItem->OnFocus(true);
{
MenuItem_T const * menuItem = CurrentMenuItem->OnSelect();
// Check to see if we have entered a submenu.
if (menuItem != NULL)
{
// Save off the old menu item, so we can get back to it later.
MenuItemHistoryIndex++;
MenuItemHistory[MenuItemHistoryIndex] = menuItem;
CurrentMenuItem = menuItem;
if (CurrentMenuItem->OnFocus != NULL)
{
CurrentMenuItem->OnFocus(true);
}
}
}
} }
break; }
break;
case KEVENT_MENU_BACK: case KEVENT_TRIGGER_SWITCH_RELEASED:
if (MenuItemHistoryIndex > 0) {
{ uint32_t duration_of_press_in_ms = (uint32_t)Event.Data;
// Go up a menu.
MenuItemHistoryIndex--;
CurrentMenuItem = MenuItemHistory[MenuItemHistoryIndex];
if (CurrentMenuItem->OnFocus != NULL)
{
CurrentMenuItem->OnFocus(true);
}
}
break;
case KEVENT_MENU_UP: if (duration_of_press_in_ms > LONG_PRESS_FOR_READY_in_ms)
{ {
if (CurrentMenuItem->OnIncrement != NULL)
{
CurrentMenuItem->OnIncrement();
}
}
break;
case KEVENT_MENU_DOWN:
{
if (CurrentMenuItem->OnDecrement != NULL)
{
CurrentMenuItem->OnDecrement();
}
}
break;
case KEVENT_MENU_EXIT:
while (MenuItemHistoryIndex > 0)
{
// Go up a menu.
MenuItemHistoryIndex--;
CurrentMenuItem = MenuItemHistory[MenuItemHistoryIndex];
if (CurrentMenuItem->OnFocus != NULL)
{
CurrentMenuItem->OnFocus(true);
}
}
break;
case KEVENT_TRIGGER_SWITCH_RELEASED:
{
uint32_t duration_of_press_in_ms = (uint32_t)Event.Data;
if (duration_of_press_in_ms > LONG_PRESS_FOR_READY_in_ms)
{
Transition_For_Event(context, STATE_READY, &Event);
}
}
break;
case KEVENT_PLAY_PRESSED_ON_REMOTE:
Transition_For_Event(context, STATE_READY, &Event); Transition_For_Event(context, STATE_READY, &Event);
break; }
}
break;
case KEVENT_REPROGRAM: case KEVENT_ACCESSORY_SWITCH_PRESSED:
Transition_For_Event(context, STATE_REPROGRAMMING, &Event); {
break; uint8_t Team_ID;
default: (void)SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &Team_ID);
// All other events are ignored in this state.
ProcessUnhandledEvent(&Event); Team_ID++;
break;
if (Team_ID > BASIC_TEAMS_MAXIMUM)
{
Team_ID = BASIC_TEAMS_MINIMUM;
}
if (Set_Team_With_Audio_Feedback(Team_ID) != SYSTEMK_RESULT_SUCCESS)
{
KLOG_WARN(KLOG_TAG, "Failed to increment team!");
}
}
break;
case KEVENT_PLAY_PRESSED_ON_REMOTE:
Transition_For_Event(context, STATE_READY, &Event);
break;
case KEVENT_REPROGRAM:
Transition_For_Event(context, STATE_REPROGRAMMING, &Event);
break;
default:
// All other events are ignored in this state.
ProcessUnhandledEvent(&Event);
break;
} }
} }
} }
@ -184,7 +203,7 @@ static void Configuring_Do(StateMachineContext_T * context)
/*! /*!
* \param context Context in which this state is being run. * \param context Context in which this state is being run.
*/ */
static void Configuring_Exit(StateMachineContext_T * context) static void Configuring_Exit(StateMachineContext_T *context)
{ {
// Save any changes that were made to NVM. // Save any changes that were made to NVM.
if (SETTINGS_Save() != SYSTEMK_RESULT_SUCCESS) if (SETTINGS_Save() != SYSTEMK_RESULT_SUCCESS)

View file

@ -93,8 +93,8 @@
#include "Logging/KLog.h" #include "Logging/KLog.h"
#include "Protocols/Protocols.h" #include "Protocols/Protocols.h"
#include "Settings/Settings_Interface.h" #include "Settings/Settings_Interface.h"
#include "Game/Game.h"
#include "Audio/Audio_HW_Interface.h" #include "Audio/Audio_HW_Interface.h"
#include "Game/Game.h"
#include "NeoPixels/NeoPixels.h" #include "NeoPixels/NeoPixels.h"
#include "BLE/BLE_Packets.h" #include "BLE/BLE_Packets.h"
#include "BLE/BLE_Packet_Tracker.h" #include "BLE/BLE_Packet_Tracker.h"