WIP: More new BLE

This commit is contained in:
Joe Kearney 2025-02-15 16:13:58 -06:00
parent a9212c5a3a
commit 58f379dc18
10 changed files with 301 additions and 22 deletions

View file

@ -181,6 +181,8 @@ void HandleBLEStatusPacket(const BLE_StatusPacket_T *const packet)
// NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_BLE_NEARBY, .Data = (void *)&BLE_RXd_data, .Prominence = NEOPIXELS_BACKGROUND};
// xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
BLE_FreePacketBuffer((BLE_GenericPacketType_T *)packet);
}
void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
@ -243,5 +245,5 @@ void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
}
}
BLE_FreePacketBuffer(packet);
BLE_FreePacketBuffer((BLE_GenericPacketType_T *)packet);
}

View file

@ -26,6 +26,18 @@ static void Configuring_Entry(StateMachineContext_T *context);
static void Configuring_Do(StateMachineContext_T *context);
static void Configuring_Exit(StateMachineContext_T *context);
static void HandleBLEConfigurationPacket(const BLE_ConfigurationPacket_T *const packet);
static void HandleBLEEventPacket(const BLE_EventPacket_T *const packet);
static TimerHandle_t BLEConfigurationResponseTimer = NULL;
static StaticTimer_t xBLEConfigurationResponseTimerBuffer;
static TickType_t xBLEConfigurationResponseTimerPeriod = 3000 / portTICK_PERIOD_MS;
static void BLEConfigurationResponseTimerCallback(TimerHandle_t xTimer)
{
BLE_UpdateHelloPacket();
}
#define MAX_MENU_DEPTH 10
static MenuItem_T const *CurrentMenuItem;
static uint8_t MenuItemHistoryIndex = 0;
@ -59,6 +71,22 @@ static void Configuring_Entry(StateMachineContext_T *context)
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
}
if (BLEConfigurationResponseTimer == NULL)
{
BLEConfigurationResponseTimer = xTimerCreateStatic(
"BLEConfigResponse",
xBLEConfigurationResponseTimerPeriod,
pdTRUE,
(void *)0,
BLEConfigurationResponseTimerCallback,
&xBLEConfigurationResponseTimerBuffer);
}
if (BLEConfigurationResponseTimer == NULL)
{
KLOG_ERROR(KLOG_TAG, "Couldn't create the BLEConfigurationResponseTimer!");
}
// Reset the menu.
CurrentMenuItem = RootMenu;
MenuItemHistory[MenuItemHistoryIndex] = CurrentMenuItem;
@ -195,6 +223,19 @@ static void Configuring_Do(StateMachineContext_T *context)
Transition_For_Event(context, STATE_REPROGRAMMING, &Event);
break;
case KEVENT_BLE_PACKET_RECEIVED:
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_CONFIGURATION)
{
HandleBLEConfigurationPacket((BLE_ConfigurationPacket_T *)Event.Data);
}
else if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_EVENT)
{
HandleBLEEventPacket((BLE_EventPacket_T *)Event.Data);
}
BLE_FreePacketBuffer(Event.Data);
break;
default:
// All other events are ignored in this state.
ProcessUnhandledEvent(&Event);
@ -218,3 +259,67 @@ static void Configuring_Exit(StateMachineContext_T *context)
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_ALL_OFF, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
}
static SystemKResult_T HandleParameterChangeRequest(BLE_ConfigurationKey_T key, uint32_t value)
{
SystemKResult_T result = SYSTEMK_RESULT_UNSPECIFIED_FAILURE;
if (key == BLE_CONFIGURATION_KEY_TEAM_ID)
{
uint8_t team_ID = (uint8_t)value;
if (Is_Valid_Team_ID(team_ID))
{
result = Set_Team_With_Audio_Feedback(team_ID);
}
}
return result;
}
void HandleBLEConfigurationPacket(const BLE_ConfigurationPacket_T *const packet)
{
if (BLE_IsBLEPacketForMe(packet->target_BD_ADDR))
{
if (BLE_IsPacketNew(packet->BD_ADDR, BLE_PACKET_TYPE_CONFIGURATION, packet->event_number))
{
if (packet->subtype == BLE_REQUEST_PARAMETER_CHANGE)
{
SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
BLE_ConfigurationKey_T key_one = BLE_GetValidConfigKey(packet->key_one);
if (key_one != BLE_CONFIGURATION_KEY_NONE)
{
result = HandleParameterChangeRequest(key_one, packet->value_one);
}
if (result == SYSTEMK_RESULT_SUCCESS)
{
BLE_ConfigurationKey_T key_two = BLE_GetValidConfigKey(packet->key_two);
if (key_two != BLE_CONFIGURATION_KEY_NONE)
{
result = HandleParameterChangeRequest(key_two, packet->value_two);
}
}
if (result == SYSTEMK_RESULT_SUCCESS)
{
BLE_RespondToConfigurationPacket(packet, BLE_ACKNOWLEDGE_PARAMETER_CHANGE);
}
else
{
KLOG_ERROR(KLOG_TAG, "Error changing parameter(s)!");
BLE_RespondToConfigurationPacket(packet, BLE_ERROR_CHANGING_PARAMETERS);
}
if (xTimerStart(BLEConfigurationResponseTimer, 0) != pdPASS)
{
KLOG_ERROR(KLOG_TAG, "Couldn't start the BLEConfigurationResponseTimer!");
}
}
}
}
}
static void HandleBLEEventPacket(const BLE_EventPacket_T *const packet)
{
}