Reworked BLE according to v0.12 of the KTag Beacon Specification (#5)

This change to SystemK implements version 0.12 of the KTag Beacon Specification.

The spec. is here: [KTag Beacon Specification v0.12](https://ktag.clubk.club/Technology/BLE/KTag%20Beacon%20Specification%20v0.12.pdf)
This change also includes the 31AUG2025 changes to the State Machine (now documented [here](https://ktag.clubk.club/Technology/SystemK/SystemKStateMachine.drawio.png)), as well as changes to support automated testing.

**All projects should update to this version.**

Co-authored-by: Joe Kearney <joe@clubk.club>
Reviewed-on: #5
This commit is contained in:
Joe Kearney 2025-09-01 17:44:10 +00:00
parent 7aa827563a
commit 430aec54b8
22 changed files with 368 additions and 47 deletions

View file

@ -24,7 +24,7 @@
static TimerHandle_t BLEStatusTimer = NULL;
static StaticTimer_t xBLEStatusTimerBuffer;
static TickType_t xBLEStatusTimerPeriod = 3000 / portTICK_PERIOD_MS;
static TickType_t xBLEStatusTimerPeriod = 500 / portTICK_PERIOD_MS;
static TimerHandle_t GameDurationTimer = NULL;
static StaticTimer_t xGameDurationTimerBuffer;

View file

@ -406,6 +406,7 @@ static void Playing__Interacting_Do(StateMachineContext_T * context)
}
break;
case KEVENT_PLAY_PRESSED_ON_REMOTE:
case KEVENT_GAME_OVER:
{
AudioAction_T audio_action = {.ID = AUDIO_PLAY_GAME_OVER, .Play_To_Completion = true};
@ -413,6 +414,21 @@ static void Playing__Interacting_Do(StateMachineContext_T * context)
Transition_For_Event(context, STATE_WRAPPING_UP, &Event);
}
break;
case KEVENT_MENU_UP:
{
#ifdef LOG_INTERACTING_SUBSTATE
KLOG_INFO(KLOG_TAG, "Simulating a tag leading to being tagged out.");
#endif // LOG_INTERACTING_SUBSTATE
Reduce_Health(Get_Max_Health());
AudioAction_T audio_action = {.ID = AUDIO_PLAY_TAG_RECEIVED, .Data = (void *)0x00};
Perform_Audio_Action(&audio_action);
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TAG_RECEIVED, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)&ReceivedTagColor};
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
}
break;
default:
// All other events are ignored in this state.

View file

@ -185,6 +185,7 @@ static void Playing__Tagged_Out_Do(StateMachineContext_T * context)
}
break;
case KEVENT_PLAY_PRESSED_ON_REMOTE:
case KEVENT_GAME_OVER:
{
AudioAction_T audio_action = {.ID = AUDIO_PLAY_GAME_OVER, .Play_To_Completion = true};
@ -193,7 +194,7 @@ static void Playing__Tagged_Out_Do(StateMachineContext_T * context)
}
break;
case KEVENT_PLAY_PRESSED_ON_REMOTE:
case KEVENT_MENU_DOWN:
Set_Health(Get_Max_Health());
Reset_Bombs();
Transition_For_Event(context, STATE_PLAYING__INTERACTING, &Event);

View file

@ -50,6 +50,7 @@ static void Starting_Game__Counting_Down_Entry(StateMachineContext_T * context)
Reset_Shots_Fired();
Reset_Tags_Received();
Reset_Times_Tagged_Out();
Set_Health(Get_Max_Health());
uint8_t n_bombs;
if (SETTINGS_get_uint8_t(SYSTEMK_SETTING_N_SPECIAL_WEAPONS_ON_REENTRY, &n_bombs) == SYSTEMK_RESULT_SUCCESS)

View file

@ -34,8 +34,7 @@ static TickType_t xBLEConfigurationResponseTimerPeriod = 3000 / portTICK_PERIOD_
static void BLEConfigurationResponseTimerCallback(TimerHandle_t xTimer)
{
// Don't send HELLO packets once configuration has started.
BLE_StopAdvertising();
BLE_UpdateHelloPacket();
}
#define MAX_MENU_DEPTH 10
@ -107,13 +106,6 @@ static void Configuring_Do(StateMachineContext_T *context)
{
portBASE_TYPE xStatus;
static KEvent_T Event;
// For the first hundred milliseconds, keep updating the Hello packet, since on some platforms (PSoC6), one call is not enough.
// TODO: Fix the Hello packet hack on PSoC6.
if ((xTaskGetTickCount() - context->Time_At_State_Entry_In_Ticks) < (100 / portTICK_PERIOD_MS))
{
BLE_UpdateHelloPacket();
}
xStatus = Receive_KEvent(&Event);
@ -238,7 +230,12 @@ static void Configuring_Do(StateMachineContext_T *context)
break;
case KEVENT_BLE_PACKET_RECEIVED:
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_PARAMETERS)
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_INSTIGATE_GAME)
{
Transition_For_Event(context, STATE_STARTING_GAME__RESPONDING, &Event);
// Don't free the packet buffer here; it will be freed in Starting_Game__Responding_Entry().
}
else if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_PARAMETERS)
{
HandleBLEConfigurationPacket((BLE_ParametersPacket_T *)Event.Data);
}
@ -408,6 +405,12 @@ void HandleBLEConfigurationPacket(const BLE_ParametersPacket_T *const packet)
BLE_RespondToConfigurationPacket(packet, BLE_ERROR_CHANGING_PARAMETERS);
}
// A parameters request addressed to this device or broadcast will end a BLE Quiet.
if (BLE_Unquiet() != SYSTEMK_RESULT_SUCCESS)
{
KLOG_ERROR(KLOG_TAG, "Couldn't Unquiet to respond to a Parameters request!");
}
if (xTimerStart(BLEConfigurationResponseTimer, 0) != pdPASS)
{
KLOG_ERROR(KLOG_TAG, "Couldn't start the BLEConfigurationResponseTimer!");
@ -415,11 +418,6 @@ void HandleBLEConfigurationPacket(const BLE_ParametersPacket_T *const packet)
}
}
}
else
{
// Once configuration has begun, stop advertising the HELLO packet to free up bandwidth.
BLE_StopAdvertising();
}
BLE_FreePacketBuffer((BLE_GenericPacketType_T *)packet);
}

View file

@ -68,7 +68,7 @@ static void Initializing_Do(StateMachineContext_T * context)
{
switch (Event.ID)
{
case KEVENT_TRIGGER_SWITCH_PRESSED:
case KEVENT_ACCESSORY_SWITCH_PRESSED:
Transition_For_Event(context, STATE_REPROGRAMMING, &Event);
break;

View file

@ -54,8 +54,6 @@ static void Ready_Entry(StateMachineContext_T * context)
}
AudioAction_T audio_action = {.ID = AUDIO_PLAY_GAME_ON, .Data = (void *)0x00};
Perform_Audio_Action(&audio_action);
Set_Health(Get_Max_Health());
}
//! Executes the Ready state.
@ -91,6 +89,7 @@ static void Ready_Do(StateMachineContext_T * context)
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_INSTIGATE_GAME)
{
Transition_For_Event(context, STATE_STARTING_GAME__RESPONDING, &Event);
// Don't free the packet buffer here; it will be freed in Starting_Game__Responding_Entry().
}
else if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_EVENT)
{

View file

@ -54,7 +54,7 @@ const StateActivity_T STATE_WRAPPING_UP_Activities =
static void Wrapping_Up_Entry(StateMachineContext_T * context)
{
LOG("Entering the Wrapping Up state.");
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_IDLE, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_WRAPPING_UP, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
BLE_UpdateStatusPacket(STATE_WRAPPING_UP);
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
@ -109,7 +109,12 @@ static void Wrapping_Up_Do(StateMachineContext_T * context)
break;
case KEVENT_BLE_PACKET_RECEIVED:
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_EVENT)
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_INSTIGATE_GAME)
{
Transition_For_Event(context, STATE_STARTING_GAME__RESPONDING, &Event);
// Don't free the packet buffer here; it will be freed in Starting_Game__Responding_Entry().
}
else if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_EVENT)
{
HandleBLEEventPacket((BLE_EventPacket_T *)Event.Data, context);
}
@ -118,6 +123,10 @@ static void Wrapping_Up_Do(StateMachineContext_T * context)
BLE_FreePacketBuffer(Event.Data);
}
break;
case KEVENT_PLAY_PRESSED_ON_REMOTE:
Transition_For_Event(context, STATE_CONFIGURING, &Event);
break;
default:
// All other events are ignored in this state.