134 lines
4.6 KiB
C
134 lines
4.6 KiB
C
/*
|
|
* This program source code file is part of SystemK, a library in the KTag project.
|
|
*
|
|
* 🛡️ <https://ktag.clubk.club> 🃞
|
|
*
|
|
* Copyright © 2016-2025 Joseph P. Kearney and the KTag developers.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
* later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
* details.
|
|
*
|
|
* There should be a copy of the GNU Affero General Public License in the LICENSE
|
|
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/* Include Files */
|
|
#include "SystemK.h"
|
|
|
|
static void Starting_Game__Responding_Entry(StateMachineContext_T * context);
|
|
static void Starting_Game__Responding_Do(StateMachineContext_T * context);
|
|
static void Starting_Game__Responding_Exit(StateMachineContext_T * context);
|
|
|
|
static TickType_t Period_Start = 0;
|
|
static uint_fast8_t N_Lights_Lit = 0;
|
|
|
|
static const char *KLOG_TAG = "STATE_STARTING_GAME__RESPONDING";
|
|
|
|
//! Activities for the **Responding** substate of the **Starting Game** state.
|
|
const StateActivity_T STATE_STARTING_GAME__RESPONDING_Activities =
|
|
{
|
|
.Entry = Starting_Game__Responding_Entry,
|
|
.Do = Starting_Game__Responding_Do,
|
|
.Exit = Starting_Game__Responding_Exit
|
|
};
|
|
|
|
//! Sets up the Responding substate.
|
|
/*!
|
|
* \param context Context in which this substate is being run.
|
|
*/
|
|
static void Starting_Game__Responding_Entry(StateMachineContext_T * context)
|
|
{
|
|
// First of all, enter the state containing this substate.
|
|
Starting_Game_Entry(context);
|
|
|
|
LOG("Entering the Responding substate of the Starting Game state.");
|
|
|
|
if (context->Cause_Of_Transition->ID == KEVENT_BLE_PACKET_RECEIVED)
|
|
{
|
|
if (((BLE_Packet_T *)context->Cause_Of_Transition->Data)->Generic.type == BLE_PACKET_TYPE_INSTIGATE_GAME)
|
|
{
|
|
uint32_t game_length_in_ms = ((BLE_Packet_T *)context->Cause_Of_Transition->Data)->Instigation.game_length_in_ms;
|
|
Set_Time_Remaining_in_Game(game_length_in_ms);
|
|
|
|
uint32_t time_remaining_until_countdown_in_ms = ((BLE_Packet_T *)context->Cause_Of_Transition->Data)->Instigation.time_remaining_until_countdown_in_ms;
|
|
Set_Time_Remaining_Until_Countdown(time_remaining_until_countdown_in_ms);
|
|
|
|
#ifdef LOG_STATE_MACHINE
|
|
KLOG_TRACE(KLOG_TAG, "Game length: %lu ms", game_length_in_ms);
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
KLOG_TRACE(KLOG_TAG, "Time until countdown: %lu ms", time_remaining_until_countdown_in_ms);
|
|
#endif // LOG_STATE_MACHINE
|
|
}
|
|
|
|
BLE_FreePacketBuffer(context->Cause_Of_Transition->Data);
|
|
}
|
|
}
|
|
|
|
//! Executes the Responding substate.
|
|
/*!
|
|
* \param context Context in which this substate is being run.
|
|
*/
|
|
static void Starting_Game__Responding_Do(StateMachineContext_T * context)
|
|
{
|
|
portBASE_TYPE xStatus;
|
|
static KEvent_T Event;
|
|
|
|
xStatus = Receive_KEvent(&Event);
|
|
|
|
if (xStatus == pdPASS)
|
|
{
|
|
switch (Event.ID)
|
|
{
|
|
default:
|
|
// All events are ignored in this state.
|
|
ProcessUnhandledEvent(&Event);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Wait for (T_start_game - T_instigation) before proceeding.
|
|
if (GetTimeInState_in_ms(context) > Get_Time_Remaining_Until_Countdown_in_ms())
|
|
{
|
|
static KEvent_T Event;
|
|
Event.ID = KEVENT_AUTOMATIC_TRANSITION;
|
|
Event.Data = NULL;
|
|
Transition_For_Event(context, STATE_STARTING_GAME__COUNTING_DOWN, &Event);
|
|
}
|
|
else
|
|
{
|
|
uint32_t period_time_in_ms = (xTaskGetTickCount() - Period_Start) * portTICK_PERIOD_MS;
|
|
|
|
if (period_time_in_ms > 150)
|
|
{
|
|
// Toggle the lights.
|
|
if (N_Lights_Lit == 0)
|
|
{
|
|
N_Lights_Lit = 5;
|
|
}
|
|
else
|
|
{
|
|
N_Lights_Lit = 0;
|
|
}
|
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_COUNTDOWN, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)&N_Lights_Lit};
|
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
|
|
|
// Start a new period.
|
|
Period_Start = xTaskGetTickCount();
|
|
}
|
|
}
|
|
}
|
|
|
|
//! Cleans up the Responding substate.
|
|
/*!
|
|
* \param context Context in which this substate is being run.
|
|
*/
|
|
static void Starting_Game__Responding_Exit(StateMachineContext_T * context)
|
|
{
|
|
}
|