/* * This program source code file is part of SystemK, a library in the KTag project. * * 🛡️ 🃞 * * 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 . */ /* 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."); 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. }