SystemK/States/State_Wrapping_Up.c
2025-06-05 20:11:10 -05:00

140 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 const char *KLOG_TAG = "STATE_WRAPPING_UP";
static const uint16_t LONG_PRESS_FOR_CONFIGURING_in_ms = 5000;
static void Wrapping_Up_Entry(StateMachineContext_T * context);
static void Wrapping_Up_Do(StateMachineContext_T * context);
static void Wrapping_Up_Exit(StateMachineContext_T * context);
static TimerHandle_t BLEInfoRequestResponseTimer = NULL;
static StaticTimer_t xBLEInfoRequestResponseTimerBuffer;
static TickType_t xBLEInfoRequestResponseTimerPeriod = 3000 / portTICK_PERIOD_MS;
static void BLEInfoRequestResponseTimerCallback(TimerHandle_t xTimer)
{
BLE_UpdateStatusPacket(STATE_WRAPPING_UP);
}
//! Activities for the **Wrapping Up** state.
const StateActivity_T STATE_WRAPPING_UP_Activities =
{
.Entry = Wrapping_Up_Entry,
.Do = Wrapping_Up_Do,
.Exit = Wrapping_Up_Exit
};
//! Sets up the Wrapping Up state.
/*!
* \param context Context in which this state is being run.
*/
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};
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
BLE_UpdateStatusPacket(STATE_WRAPPING_UP);
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
{
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
}
if (BLEInfoRequestResponseTimer == NULL)
{
BLEInfoRequestResponseTimer = xTimerCreateStatic(
"BLEInfoRequestResponse",
xBLEInfoRequestResponseTimerPeriod,
pdFALSE,
(void *)0,
BLEInfoRequestResponseTimerCallback,
&xBLEInfoRequestResponseTimerBuffer);
}
if (BLEInfoRequestResponseTimer == NULL)
{
KLOG_ERROR(KLOG_TAG, "Couldn't create the BLEInfoRequestResponseTimer!");
}
}
//! Executes the Wrapping Up state.
/*!
* \param context Context in which this state is being run.
*/
static void Wrapping_Up_Do(StateMachineContext_T * context)
{
portBASE_TYPE xStatus;
static KEvent_T Event;
xStatus = Receive_KEvent(&Event);
if (xStatus == pdPASS)
{
switch (Event.ID)
{
case KEVENT_TRIGGER_SWITCH_PRESSED:
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_CONFIGURING_in_ms)
{
Transition_For_Event(context, STATE_CONFIGURING, &Event);
}
}
break;
case KEVENT_BLE_PACKET_RECEIVED:
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_EVENT)
{
HandleBLEEventPacket((BLE_EventPacket_T *)Event.Data, context);
}
else
{
BLE_FreePacketBuffer(Event.Data);
}
break;
default:
// All other events are ignored in this state.
ProcessUnhandledEvent(&Event);
break;
}
}
}
//! Cleans up the Wrapping Up state.
/*!
* \param context Context in which this state is being run.
*/
static void Wrapping_Up_Exit(StateMachineContext_T * context)
{
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_ALL_OFF, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
AudioAction_T audio_action = {.ID = AUDIO_SILENCE, .Data = (void *)0x00};
Perform_Audio_Action(&audio_action);
}