SystemK/States/Starting_Game/State_Starting_Game__Instigating.c
Joe Kearney bfcdf4c354 Reworked BLE according to v0.11 of the KTag Beacon Specification (#2)
This was done to support the new KTag Konfigurator app, which Jack created for his Senior Design project.

Co-authored-by: Joe Kearney <joe@clubk.club>
Reviewed-on: #2
2025-06-08 21:52:29 +00:00

141 lines
4.9 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__Instigating_Entry(StateMachineContext_T * context);
static void Starting_Game__Instigating_Do(StateMachineContext_T * context);
static void Starting_Game__Instigating_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__INSTIGATING";
//! Activities for the **Instigating** substate of the **Starting Game** state.
const StateActivity_T STATE_STARTING_GAME__INSTIGATING_Activities =
{
.Entry = Starting_Game__Instigating_Entry,
.Do = Starting_Game__Instigating_Do,
.Exit = Starting_Game__Instigating_Exit
};
//! Sets up the Instigating substate.
/*!
* \param context Context in which this substate is being run.
*/
static void Starting_Game__Instigating_Entry(StateMachineContext_T * context)
{
// First of all, enter the state containing this substate.
Starting_Game_Entry(context);
LOG("Entering the Instigating substate of the Starting Game state.");
uint32_t game_length_in_ms;
(void) SETTINGS_get_uint32_t(SYSTEMK_SETTING_T_GAME_LENGTH_in_ms, &game_length_in_ms);
Set_Time_Remaining_Until_Countdown(game_length_in_ms);
Set_Time_Remaining_in_Game(UINT32_MAX);
uint32_t start_game_time_in_ms;
(void) SETTINGS_get_uint32_t(SYSTEMK_SETTING_T_START_GAME_in_ms, &start_game_time_in_ms);
Set_Time_Remaining_Until_Countdown(start_game_time_in_ms);
BLE_UpdateInstigationPacket(Get_Time_Remaining_in_Game_in_ms(), Get_Time_Remaining_Until_Countdown_in_ms());
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
{
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
}
Period_Start = xTaskGetTickCount();
}
//! Executes the Instigating substate.
/*!
* \param context Context in which this substate is being run.
*/
static void Starting_Game__Instigating_Do(StateMachineContext_T * context)
{
portBASE_TYPE xStatus;
static KEvent_T Event;
xStatus = Receive_KEvent(&Event);
if (xStatus == pdPASS)
{
switch (Event.ID)
{
case KEVENT_PLAY_PRESSED_ON_REMOTE:
Transition_For_Event(context, STATE_STARTING_GAME__COUNTING_DOWN, &Event);
break;
default:
// All other events are ignored in this state.
ProcessUnhandledEvent(&Event);
break;
}
}
// Wait for T_start_game before proceeding.
uint32_t start_game_time_in_ms;
(void) SETTINGS_get_uint32_t(SYSTEMK_SETTING_T_START_GAME_in_ms, &start_game_time_in_ms);
if (GetTimeInState_in_ms(context) > start_game_time_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;
uint32_t time_remaining_until_countdown_in_ms = start_game_time_in_ms - GetTimeInState_in_ms(context);
Set_Time_Remaining_Until_Countdown(time_remaining_until_countdown_in_ms);
if (period_time_in_ms > 150)
{
BLE_UpdateInstigationPacket(Get_Time_Remaining_in_Game_in_ms(), Get_Time_Remaining_Until_Countdown_in_ms());
// 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 Instigating substate.
/*!
* \param context Context in which this substate is being run.
*/
static void Starting_Game__Instigating_Exit(StateMachineContext_T * context)
{
// Nothing to do.
}