Compare commits
2 commits
4fe072f2d3
...
58f379dc18
Author | SHA1 | Date | |
---|---|---|---|
![]() |
58f379dc18 | ||
![]() |
a9212c5a3a |
17 changed files with 483 additions and 69 deletions
|
@ -49,11 +49,14 @@ static PacketTracker_T Tracker =
|
||||||
.head = 0,
|
.head = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// A packet is _new_ if the combination of BLE adress, type, and event number have not been seen recently.
|
// A packet is _new_ if the combination of BLE address, type, and event number have not been seen recently.
|
||||||
bool BLE_IsPacketNew(const uint8_t *sender_BD_ADDR,
|
bool BLE_IsPacketNew(const uint8_t *sender_BD_ADDR,
|
||||||
BLE_PacketType_T packet_type,
|
BLE_PacketType_T packet_type,
|
||||||
uint8_t event_number)
|
uint8_t event_number)
|
||||||
{
|
{
|
||||||
|
//esp_log_level_set("BLE", ESP_LOG_DEBUG);
|
||||||
|
//KLOG_DEBUG("BLE", "Packet from %s", BLE_ADDR_To_Str(sender_BD_ADDR));
|
||||||
|
|
||||||
// Check if the packet already exists in the tracker.
|
// Check if the packet already exists in the tracker.
|
||||||
for (int i = 0; i < Tracker.count; i++)
|
for (int i = 0; i < Tracker.count; i++)
|
||||||
{
|
{
|
||||||
|
|
|
@ -147,7 +147,7 @@ void BLE_UpdateInstigationPacket(uint32_t Game_Length_in_ms, uint32_t Time_Remai
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BLE_UpdateStatusPacket()
|
void BLE_UpdateStatusPacket(uint8_t current_state)
|
||||||
{
|
{
|
||||||
static uint8_t EventNumber = 0;
|
static uint8_t EventNumber = 0;
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ void BLE_UpdateStatusPacket()
|
||||||
Advertising_Data.data[23] = (Team_Color >> 8) & 0xFF; // Secondary Color
|
Advertising_Data.data[23] = (Team_Color >> 8) & 0xFF; // Secondary Color
|
||||||
Advertising_Data.data[24] = (Team_Color >> 16) & 0xFF; // Secondary Color
|
Advertising_Data.data[24] = (Team_Color >> 16) & 0xFF; // Secondary Color
|
||||||
Advertising_Data.data[25] = (Team_Color >> 24) & 0xFF; // Secondary Color
|
Advertising_Data.data[25] = (Team_Color >> 24) & 0xFF; // Secondary Color
|
||||||
Advertising_Data.data[26] = 0xFF;
|
Advertising_Data.data[26] = current_state;
|
||||||
Advertising_Data.data[27] = 0xFF;
|
Advertising_Data.data[27] = 0xFF;
|
||||||
Advertising_Data.data[28] = 0xFF;
|
Advertising_Data.data[28] = 0xFF;
|
||||||
Advertising_Data.data[29] = 0xFF;
|
Advertising_Data.data[29] = 0xFF;
|
||||||
|
@ -237,12 +237,12 @@ void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[B
|
||||||
Advertising_Data.data[17] = (color >> 8) & 0xFF;
|
Advertising_Data.data[17] = (color >> 8) & 0xFF;
|
||||||
Advertising_Data.data[18] = (color >> 16) & 0xFF;
|
Advertising_Data.data[18] = (color >> 16) & 0xFF;
|
||||||
Advertising_Data.data[19] = (color >> 24) & 0xFF;
|
Advertising_Data.data[19] = (color >> 24) & 0xFF;
|
||||||
Advertising_Data.data[20] = target_BD_ADDR[0],
|
Advertising_Data.data[20] = target_BD_ADDR[0];
|
||||||
Advertising_Data.data[21] = target_BD_ADDR[1],
|
Advertising_Data.data[21] = target_BD_ADDR[1];
|
||||||
Advertising_Data.data[22] = target_BD_ADDR[2],
|
Advertising_Data.data[22] = target_BD_ADDR[2];
|
||||||
Advertising_Data.data[23] = target_BD_ADDR[3],
|
Advertising_Data.data[23] = target_BD_ADDR[3];
|
||||||
Advertising_Data.data[24] = target_BD_ADDR[4],
|
Advertising_Data.data[24] = target_BD_ADDR[4];
|
||||||
Advertising_Data.data[25] = target_BD_ADDR[5],
|
Advertising_Data.data[25] = target_BD_ADDR[5];
|
||||||
Advertising_Data.data[26] = 0xFF;
|
Advertising_Data.data[26] = 0xFF;
|
||||||
Advertising_Data.data[27] = 0xFF;
|
Advertising_Data.data[27] = 0xFF;
|
||||||
Advertising_Data.data[28] = 0xFF;
|
Advertising_Data.data[28] = 0xFF;
|
||||||
|
@ -257,6 +257,121 @@ void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[B
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BLE_RespondToConfigurationPacket(const BLE_ConfigurationPacket_T *const packet, BLE_ConfigurationSubtype_T response)
|
||||||
|
{
|
||||||
|
static uint8_t EventNumber = 0;
|
||||||
|
|
||||||
|
Advertising_Data.length = BLE_KTAG_PACKET_TOTAL_SIZE;
|
||||||
|
|
||||||
|
// Manufacturer Specific Data
|
||||||
|
Advertising_Data.data[0] = 0x1E;
|
||||||
|
Advertising_Data.data[1] = 0xFF;
|
||||||
|
Advertising_Data.data[2] = 0xFF;
|
||||||
|
Advertising_Data.data[3] = 0xFF;
|
||||||
|
Advertising_Data.data[4] = 'K';
|
||||||
|
Advertising_Data.data[5] = 'T';
|
||||||
|
Advertising_Data.data[6] = 'a';
|
||||||
|
Advertising_Data.data[7] = 'g';
|
||||||
|
Advertising_Data.data[8] = BLE_PACKET_TYPE_CONFIGURATION;
|
||||||
|
Advertising_Data.data[9] = EventNumber++;
|
||||||
|
Advertising_Data.data[10] = packet->BD_ADDR[0];
|
||||||
|
Advertising_Data.data[11] = packet->BD_ADDR[1];
|
||||||
|
Advertising_Data.data[12] = packet->BD_ADDR[2];
|
||||||
|
Advertising_Data.data[13] = packet->BD_ADDR[3];
|
||||||
|
Advertising_Data.data[14] = packet->BD_ADDR[4];
|
||||||
|
Advertising_Data.data[15] = packet->BD_ADDR[5];
|
||||||
|
Advertising_Data.data[16] = (uint8_t)response;
|
||||||
|
Advertising_Data.data[17] = (uint8_t)((packet->key_one >> 0) & 0xFF);
|
||||||
|
Advertising_Data.data[18] = (uint8_t)((packet->key_one >> 8) & 0xFF);
|
||||||
|
Advertising_Data.data[19] = (uint8_t)((packet->value_one >> 0) & 0xFF);
|
||||||
|
Advertising_Data.data[20] = (uint8_t)((packet->value_one >> 8) & 0xFF);
|
||||||
|
Advertising_Data.data[21] = (uint8_t)((packet->value_one >> 16) & 0xFF);
|
||||||
|
Advertising_Data.data[22] = (uint8_t)((packet->value_one >> 24) & 0xFF);
|
||||||
|
Advertising_Data.data[23] = (uint8_t)((packet->key_two >> 0) & 0xFF);
|
||||||
|
Advertising_Data.data[24] = (uint8_t)((packet->key_two >> 8) & 0xFF);
|
||||||
|
Advertising_Data.data[25] = (uint8_t)((packet->value_two >> 0) & 0xFF);
|
||||||
|
Advertising_Data.data[26] = (uint8_t)((packet->value_two >> 8) & 0xFF);
|
||||||
|
Advertising_Data.data[27] = (uint8_t)((packet->value_two >> 16) & 0xFF);
|
||||||
|
Advertising_Data.data[28] = (uint8_t)((packet->value_two >> 24) & 0xFF);
|
||||||
|
Advertising_Data.data[29] = 0xFF;
|
||||||
|
Advertising_Data.data[30] = 0xFF;
|
||||||
|
|
||||||
|
SystemKResult_T result = BLE_SetAdvertisingData(&Advertising_Data);
|
||||||
|
|
||||||
|
if (result != SYSTEMK_RESULT_SUCCESS)
|
||||||
|
{
|
||||||
|
KLOG_ERROR(KLOG_TAG, "Error responding to configuration packet!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BLE_UpdateHelloPacket()
|
||||||
|
{
|
||||||
|
static uint8_t EventNumber = 0;
|
||||||
|
|
||||||
|
uint32_t device_type32;
|
||||||
|
uint16_t device_type = BLE_DEVICE_TYPE_UNKNOWN;
|
||||||
|
if (SETTINGS_get_uint32_t(SYSTEMK_SETTING_DEVICE_TYPE, &device_type32) == SYSTEMK_RESULT_SUCCESS)
|
||||||
|
{
|
||||||
|
device_type = (uint16_t)device_type32;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t team_ID;
|
||||||
|
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &team_ID);
|
||||||
|
|
||||||
|
char device_name[SYSTEMK_MAX_CHARS_IN_DEVICE_NAME + 1];
|
||||||
|
SystemKResult_T result = SETTINGS_get_device_name(&device_name[0]);
|
||||||
|
if (result != SYSTEMK_RESULT_SUCCESS)
|
||||||
|
{
|
||||||
|
strcpy(device_name, "Anonymous");
|
||||||
|
}
|
||||||
|
size_t len = strlen(device_name);
|
||||||
|
if (len < sizeof(device_name)) {
|
||||||
|
memset(device_name + len, 0x00, sizeof(device_name) - len);
|
||||||
|
}
|
||||||
|
|
||||||
|
Advertising_Data.length = BLE_KTAG_PACKET_TOTAL_SIZE;
|
||||||
|
|
||||||
|
// Manufacturer Specific Data
|
||||||
|
Advertising_Data.data[0] = 0x1E;
|
||||||
|
Advertising_Data.data[1] = 0xFF;
|
||||||
|
Advertising_Data.data[2] = 0xFF;
|
||||||
|
Advertising_Data.data[3] = 0xFF;
|
||||||
|
Advertising_Data.data[4] = 'K';
|
||||||
|
Advertising_Data.data[5] = 'T';
|
||||||
|
Advertising_Data.data[6] = 'a';
|
||||||
|
Advertising_Data.data[7] = 'g';
|
||||||
|
Advertising_Data.data[8] = BLE_PACKET_TYPE_HELLO;
|
||||||
|
Advertising_Data.data[9] = EventNumber++;
|
||||||
|
Advertising_Data.data[10] = SYSTEMK_MAJOR_VERSION;
|
||||||
|
Advertising_Data.data[11] = SYSTEMK_MINOR_VERSION;
|
||||||
|
Advertising_Data.data[12] = (device_type >> 0) & 0xFF;
|
||||||
|
Advertising_Data.data[13] = (device_type >> 8) & 0xFF;
|
||||||
|
Advertising_Data.data[14] = team_ID;
|
||||||
|
Advertising_Data.data[15] = device_name[0];
|
||||||
|
Advertising_Data.data[16] = device_name[1];
|
||||||
|
Advertising_Data.data[17] = device_name[2];
|
||||||
|
Advertising_Data.data[18] = device_name[3];
|
||||||
|
Advertising_Data.data[19] = device_name[4];
|
||||||
|
Advertising_Data.data[20] = device_name[5];
|
||||||
|
Advertising_Data.data[21] = device_name[6];
|
||||||
|
Advertising_Data.data[22] = device_name[7];
|
||||||
|
Advertising_Data.data[23] = device_name[8];
|
||||||
|
Advertising_Data.data[24] = device_name[9];
|
||||||
|
Advertising_Data.data[25] = device_name[10];
|
||||||
|
Advertising_Data.data[26] = device_name[11];
|
||||||
|
Advertising_Data.data[27] = device_name[12];
|
||||||
|
Advertising_Data.data[28] = device_name[13];
|
||||||
|
Advertising_Data.data[29] = device_name[14];
|
||||||
|
Advertising_Data.data[30] = device_name[15];
|
||||||
|
|
||||||
|
result = BLE_SetAdvertisingData(&Advertising_Data);
|
||||||
|
|
||||||
|
if (result != SYSTEMK_RESULT_SUCCESS)
|
||||||
|
{
|
||||||
|
KLOG_ERROR(KLOG_TAG, "Error updating hello packet!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[6])
|
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[6])
|
||||||
{
|
{
|
||||||
bool for_me = false;
|
bool for_me = false;
|
||||||
|
|
|
@ -29,7 +29,8 @@
|
||||||
#define BLE_PACKETS_H
|
#define BLE_PACKETS_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C"
|
||||||
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Preprocessor and Type Definitions */
|
/* Preprocessor and Type Definitions */
|
||||||
|
@ -59,11 +60,13 @@ typedef enum
|
||||||
BLE_PACKET_TYPE_BUFFER_FREE = 0,
|
BLE_PACKET_TYPE_BUFFER_FREE = 0,
|
||||||
BLE_PACKET_TYPE_INSTIGATE_GAME = 1,
|
BLE_PACKET_TYPE_INSTIGATE_GAME = 1,
|
||||||
BLE_FIRST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_INSTIGATE_GAME,
|
BLE_FIRST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_INSTIGATE_GAME,
|
||||||
BLE_PACKET_TYPE_JOIN_NOW = 2,
|
BLE_PACKET_TYPE_EVENT = 2,
|
||||||
BLE_PACKET_TYPE_TAG = 3,
|
BLE_PACKET_TYPE_TAG = 3,
|
||||||
BLE_PACKET_TYPE_CONSOLE = 4,
|
BLE_PACKET_TYPE_CONSOLE = 4,
|
||||||
BLE_PACKET_TYPE_STATUS = 5,
|
BLE_PACKET_TYPE_STATUS = 5,
|
||||||
BLE_LAST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_STATUS,
|
BLE_PACKET_TYPE_CONFIGURATION = 6,
|
||||||
|
BLE_PACKET_TYPE_HELLO = 7,
|
||||||
|
BLE_LAST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_HELLO,
|
||||||
BLE_PACKET_TYPE_UNKNOWN
|
BLE_PACKET_TYPE_UNKNOWN
|
||||||
} BLE_PacketType_T;
|
} BLE_PacketType_T;
|
||||||
|
|
||||||
|
@ -87,11 +90,10 @@ typedef struct
|
||||||
|
|
||||||
uint32_t game_length_in_ms;
|
uint32_t game_length_in_ms;
|
||||||
uint32_t time_remaining_until_countdown_in_ms;
|
uint32_t time_remaining_until_countdown_in_ms;
|
||||||
uint8_t random_time_after_countdown_in_ms_x100;
|
uint8_t unused[13];
|
||||||
uint8_t unused[12];
|
|
||||||
} __attribute__((packed, aligned(1))) BLE_InstigationPacket_T;
|
} __attribute__((packed, aligned(1))) BLE_InstigationPacket_T;
|
||||||
|
|
||||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_JOIN_NOW.
|
//! Contents of the BLE packet #BLE_PACKET_TYPE_EVENT.
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
BLE_PacketType_T type;
|
BLE_PacketType_T type;
|
||||||
|
@ -99,10 +101,11 @@ typedef struct
|
||||||
int8_t RSSI;
|
int8_t RSSI;
|
||||||
uint8_t event_number;
|
uint8_t event_number;
|
||||||
|
|
||||||
uint32_t game_length_in_ms;
|
uint8_t target_BD_ADDR[BD_ADDR_SIZE];
|
||||||
uint32_t time_remaining_in_game_in_ms;
|
uint32_t event_ID;
|
||||||
uint8_t unused[13];
|
uint32_t event_data;
|
||||||
} __attribute__((packed, aligned(1))) BLE_JoinNowPacket_T;
|
uint8_t unused[7];
|
||||||
|
} __attribute__((packed, aligned(1))) BLE_EventPacket_T;
|
||||||
|
|
||||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_TAG.
|
//! Contents of the BLE packet #BLE_PACKET_TYPE_TAG.
|
||||||
typedef struct
|
typedef struct
|
||||||
|
@ -149,17 +152,93 @@ typedef struct
|
||||||
uint16_t maximum_health;
|
uint16_t maximum_health;
|
||||||
color_t primary_color;
|
color_t primary_color;
|
||||||
color_t secondary_color;
|
color_t secondary_color;
|
||||||
uint8_t unused[5];
|
uint8_t SystemK_top_level_state; // StateID_T
|
||||||
|
uint8_t unused[4];
|
||||||
} __attribute__((packed, aligned(1))) BLE_StatusPacket_T;
|
} __attribute__((packed, aligned(1))) BLE_StatusPacket_T;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
BLE_REQUEST_CURRENT_PARAMETER_INFORMATION = 0x00,
|
||||||
|
BLE_CURRENT_PARAMETER_INFORMATION = 0x01,
|
||||||
|
BLE_REQUEST_PARAMETER_CHANGE = 0x02,
|
||||||
|
BLE_ACKNOWLEDGE_PARAMETER_CHANGE = 0x03,
|
||||||
|
BLE_ERROR_CHANGING_PARAMETERS = 0x04,
|
||||||
|
BLE_ERROR_RESPONDING_TO_REQUEST = 0xFF
|
||||||
|
} BLE_ConfigurationSubtype_T;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
BLE_CONFIGURATION_KEY_NONE = 0x0000,
|
||||||
|
BLE_CONFIGURATION_KEY_TEAM_ID = 0x0001,
|
||||||
|
BLE_FIRST_VALID_CONFIGURATION_KEY = BLE_CONFIGURATION_KEY_TEAM_ID,
|
||||||
|
BLE_LAST_VALID_CONFIGURATION_KEY = BLE_CONFIGURATION_KEY_TEAM_ID,
|
||||||
|
BLE_DEVICE_TYPE_UNUSED = 0xFFFF
|
||||||
|
} BLE_ConfigurationKey_T;
|
||||||
|
|
||||||
|
inline BLE_ConfigurationKey_T BLE_GetValidConfigKey(uint16_t key)
|
||||||
|
{
|
||||||
|
BLE_ConfigurationKey_T result = BLE_CONFIGURATION_KEY_NONE;
|
||||||
|
|
||||||
|
if ((key >= BLE_FIRST_VALID_CONFIGURATION_KEY) &&
|
||||||
|
(key <= BLE_LAST_VALID_CONFIGURATION_KEY))
|
||||||
|
{
|
||||||
|
result = (BLE_ConfigurationKey_T)key;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Contents of the BLE packet #BLE_PACKET_TYPE_CONFIGURATION.
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
BLE_PacketType_T type;
|
||||||
|
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||||
|
int8_t RSSI;
|
||||||
|
uint8_t event_number;
|
||||||
|
|
||||||
|
uint8_t target_BD_ADDR[BD_ADDR_SIZE];
|
||||||
|
uint8_t subtype;
|
||||||
|
uint16_t key_one;
|
||||||
|
uint32_t value_one;
|
||||||
|
uint16_t key_two;
|
||||||
|
uint32_t value_two;
|
||||||
|
uint8_t unused[2];
|
||||||
|
} __attribute__((packed, aligned(1))) BLE_ConfigurationPacket_T;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
BLE_DEVICE_TYPE_LITTLE_BOY_BLUE = 0x0000,
|
||||||
|
BLE_DEVICE_TYPE_2020TPC = 0x0001,
|
||||||
|
BLE_DEVICE_TYPE_MOBILE_APP = 0x0002,
|
||||||
|
BLE_DEVICE_TYPE_32ESPECIAL = 0x0003,
|
||||||
|
BLE_DEVICE_TYPE_UNKNOWN = 0xFFFF
|
||||||
|
} BLE_DeviceType_T;
|
||||||
|
|
||||||
|
//! Contents of the BLE packet #BLE_PACKET_TYPE_HELLO.
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
BLE_PacketType_T type;
|
||||||
|
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||||
|
int8_t RSSI;
|
||||||
|
uint8_t event_number;
|
||||||
|
|
||||||
|
uint8_t SystemK_major_version;
|
||||||
|
uint8_t SystemK_minor_version;
|
||||||
|
uint16_t device_type;
|
||||||
|
uint8_t team_ID;
|
||||||
|
uint8_t device_name[SYSTEMK_MAX_CHARS_IN_DEVICE_NAME];
|
||||||
|
} __attribute__((packed, aligned(1))) BLE_HelloPacket_T;
|
||||||
|
|
||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
BLE_GenericPacketType_T Generic;
|
BLE_GenericPacketType_T Generic;
|
||||||
BLE_InstigationPacket_T Instigation;
|
BLE_InstigationPacket_T Instigation;
|
||||||
BLE_JoinNowPacket_T JoinNow;
|
BLE_EventPacket_T Event;
|
||||||
BLE_TagPacket_T Tag;
|
BLE_TagPacket_T Tag;
|
||||||
BLE_ConsolePacket_T Console;
|
BLE_ConsolePacket_T Console;
|
||||||
BLE_StatusPacket_T Status;
|
BLE_StatusPacket_T Status;
|
||||||
|
BLE_ConfigurationPacket_T Configuration;
|
||||||
|
BLE_HelloPacket_T Hello;
|
||||||
} BLE_Packet_T;
|
} BLE_Packet_T;
|
||||||
|
|
||||||
/* Include Files */
|
/* Include Files */
|
||||||
|
@ -168,11 +247,11 @@ typedef union
|
||||||
|
|
||||||
/* Public Functions */
|
/* Public Functions */
|
||||||
|
|
||||||
inline void BLE_FreePacketBuffer(void * buffer)
|
inline void BLE_FreePacketBuffer(BLE_GenericPacketType_T *buffer)
|
||||||
{
|
{
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
((BLE_GenericPacketType_T *)buffer)->type = BLE_PACKET_TYPE_BUFFER_FREE;
|
buffer->type = BLE_PACKET_TYPE_BUFFER_FREE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,8 +259,10 @@ void BLE_InitPacketBuffers(void);
|
||||||
|
|
||||||
BLE_Packet_T *BLE_DecodeKTagPacket(const uint8_t *received_data, uint8_t received_data_length, uint8_t peer_BD_ADDR[BD_ADDR_SIZE], int8_t rssi_in_dBm);
|
BLE_Packet_T *BLE_DecodeKTagPacket(const uint8_t *received_data, uint8_t received_data_length, uint8_t peer_BD_ADDR[BD_ADDR_SIZE], int8_t rssi_in_dBm);
|
||||||
void BLE_UpdateInstigationPacket(uint32_t Game_Length_in_ms, uint32_t Time_Remaining_Until_Countdown_in_ms);
|
void BLE_UpdateInstigationPacket(uint32_t Game_Length_in_ms, uint32_t Time_Remaining_Until_Countdown_in_ms);
|
||||||
void BLE_UpdateStatusPacket();
|
void BLE_UpdateStatusPacket(uint8_t current_state);
|
||||||
void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[BD_ADDR_SIZE]);
|
void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[BD_ADDR_SIZE]);
|
||||||
|
void BLE_RespondToConfigurationPacket(const BLE_ConfigurationPacket_T *const packet, BLE_ConfigurationSubtype_T response);
|
||||||
|
void BLE_UpdateHelloPacket();
|
||||||
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[BD_ADDR_SIZE]);
|
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[BD_ADDR_SIZE]);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
52
BLE/BLE_Utils.c
Normal file
52
BLE/BLE_Utils.c
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* This program source code file is part of SystemK, a library in the KTag project.
|
||||||
|
*
|
||||||
|
* 🛡️ <https://ktag.clubk.club> 🃞
|
||||||
|
*
|
||||||
|
* Copyright © 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 <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
//! Converts a 6-byte Bluetooth Low Energy address to a human-readable string (for logging).
|
||||||
|
/*!
|
||||||
|
* \param bd_addr The Bluetooth address as a 6-byte array
|
||||||
|
* \return Pointer to a static string containing the formatted address
|
||||||
|
*
|
||||||
|
* Output format: "XX:XX:XX:XX:XX:XX" where XX are uppercase hexadecimal values
|
||||||
|
* \note This function is not reentrant as it uses a static buffer!
|
||||||
|
*/
|
||||||
|
const char *BLE_ADDR_To_Str(const uint8_t bd_addr[6])
|
||||||
|
{
|
||||||
|
static char str_addr[18]; // 17 characters + null terminator
|
||||||
|
|
||||||
|
// Convert each byte and add separating colons.
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
snprintf(&str_addr[i * 3], sizeof(str_addr) - (i * 3), "%02X", bd_addr[i]);
|
||||||
|
|
||||||
|
// Add colon separator except after the last byte.
|
||||||
|
if (i < 5)
|
||||||
|
{
|
||||||
|
str_addr[(i * 3) + 2] = ':';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
str_addr[17] = '\0'; // Ensure null termination.
|
||||||
|
return str_addr;
|
||||||
|
}
|
22
BLE/BLE_Utils.h
Normal file
22
BLE/BLE_Utils.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* This program source code file is part of SystemK, a library in the KTag project.
|
||||||
|
*
|
||||||
|
* 🛡️ <https://ktag.clubk.club> 🃞
|
||||||
|
*
|
||||||
|
* Copyright © 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char* BLE_ADDR_To_Str(const uint8_t bd_addr[6]);
|
|
@ -2,6 +2,7 @@ idf_component_register(
|
||||||
SRCS
|
SRCS
|
||||||
"BLE/BLE_Packets.c"
|
"BLE/BLE_Packets.c"
|
||||||
"BLE/BLE_Packet_Tracker.c"
|
"BLE/BLE_Packet_Tracker.c"
|
||||||
|
"BLE/BLE_Utils.c"
|
||||||
"Events/KEvents.c"
|
"Events/KEvents.c"
|
||||||
"Game/Game.c"
|
"Game/Game.c"
|
||||||
"Game/Weapons.c"
|
"Game/Weapons.c"
|
||||||
|
|
12
Game/Game.h
12
Game/Game.h
|
@ -44,6 +44,18 @@ typedef enum
|
||||||
MAXIMUM_TEAM_ID = 7
|
MAXIMUM_TEAM_ID = 7
|
||||||
} TeamID_t;
|
} TeamID_t;
|
||||||
|
|
||||||
|
inline bool Is_Valid_Team_ID(uint8_t team_ID)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
if (team_ID <= MAXIMUM_TEAM_ID)
|
||||||
|
{
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
__attribute__((always_inline)) static inline SystemKResult_T Set_Team_With_Audio_Feedback(uint8_t team_ID)
|
__attribute__((always_inline)) static inline SystemKResult_T Set_Team_With_Audio_Feedback(uint8_t team_ID)
|
||||||
{
|
{
|
||||||
static uint8_t Team_ID = 0; // This is static because AUDIO_PRONOUNCE_NUMBER_0_TO_100 needs a *pointer*.
|
static uint8_t Team_ID = 0; // This is static because AUDIO_PRONOUNCE_NUMBER_0_TO_100 needs a *pointer*.
|
||||||
|
|
|
@ -22,6 +22,8 @@
|
||||||
#ifndef SETTINGS_INTERFACE_H
|
#ifndef SETTINGS_INTERFACE_H
|
||||||
#define SETTINGS_INTERFACE_H
|
#define SETTINGS_INTERFACE_H
|
||||||
|
|
||||||
|
#define SYSTEMK_MAX_CHARS_IN_DEVICE_NAME 15
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
SYSTEMK_SETTING_IS_RIGHT_HANDED,
|
SYSTEMK_SETTING_IS_RIGHT_HANDED,
|
||||||
|
@ -29,13 +31,16 @@ typedef enum
|
||||||
SYSTEMK_SETTING_TEAMID,
|
SYSTEMK_SETTING_TEAMID,
|
||||||
SYSTEMK_SETTING_PLAYERID,
|
SYSTEMK_SETTING_PLAYERID,
|
||||||
SYSTEMK_SETTING_WEAPONID,
|
SYSTEMK_SETTING_WEAPONID,
|
||||||
SYSTEMK_SETTING_T_START_GAME_in_ms
|
SYSTEMK_SETTING_T_START_GAME_in_ms,
|
||||||
|
SYSTEMK_SETTING_DEVICE_TYPE,
|
||||||
} SystemKSettingID_T;
|
} SystemKSettingID_T;
|
||||||
|
|
||||||
SystemKResult_T SETTINGS_get_uint8_t(SystemKSettingID_T id, uint8_t * value);
|
SystemKResult_T SETTINGS_get_uint8_t(SystemKSettingID_T id, uint8_t * value);
|
||||||
SystemKResult_T SETTINGS_set_uint8_t(SystemKSettingID_T id, uint8_t value);
|
SystemKResult_T SETTINGS_set_uint8_t(SystemKSettingID_T id, uint8_t value);
|
||||||
SystemKResult_T SETTINGS_get_uint32_t(SystemKSettingID_T id, uint32_t * value);
|
SystemKResult_T SETTINGS_get_uint32_t(SystemKSettingID_T id, uint32_t * value);
|
||||||
SystemKResult_T SETTINGS_set_uint32_t(SystemKSettingID_T id, uint32_t value);
|
SystemKResult_T SETTINGS_set_uint32_t(SystemKSettingID_T id, uint32_t value);
|
||||||
|
SystemKResult_T SETTINGS_get_device_name(char* name);
|
||||||
|
SystemKResult_T SETTINGS_set_device_name(char* name);
|
||||||
SystemKResult_T SETTINGS_Save(void);
|
SystemKResult_T SETTINGS_Save(void);
|
||||||
|
|
||||||
#endif // SETTINGS_INTERFACE_H
|
#endif // SETTINGS_INTERFACE_H
|
||||||
|
|
|
@ -37,7 +37,8 @@ static const int_fast8_t TAG_RSSI_THRESHOLD = -75;
|
||||||
|
|
||||||
static void BLEStatusTimerCallback(TimerHandle_t xTimer)
|
static void BLEStatusTimerCallback(TimerHandle_t xTimer)
|
||||||
{
|
{
|
||||||
BLE_UpdateStatusPacket();
|
StateMachineContext_T* context = GetContext();
|
||||||
|
BLE_UpdateStatusPacket(context->States.Current_State);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GameDurationTimerCallback(TimerHandle_t xTimer)
|
static void GameDurationTimerCallback(TimerHandle_t xTimer)
|
||||||
|
@ -60,7 +61,7 @@ void Playing_Entry(StateMachineContext_T *context)
|
||||||
BLE_RXd_data.neighbors[slot].TimeToLive_in_ms = 0;
|
BLE_RXd_data.neighbors[slot].TimeToLive_in_ms = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BLE_UpdateStatusPacket();
|
BLE_UpdateStatusPacket(context->States.Current_State);
|
||||||
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
||||||
{
|
{
|
||||||
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
||||||
|
@ -92,7 +93,8 @@ void Playing_Entry(StateMachineContext_T *context)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The timer is only needed for timed games.
|
// The timer is only needed for timed games.
|
||||||
if (Get_Time_Remaining_in_Game_in_ms() != UINT32_MAX)
|
if ((Get_Time_Remaining_in_Game_in_ms() != UINT32_MAX) &&
|
||||||
|
(Get_Time_Remaining_in_Game_in_ms() != 0))
|
||||||
{
|
{
|
||||||
if (GameDurationTimer == NULL)
|
if (GameDurationTimer == NULL)
|
||||||
{
|
{
|
||||||
|
@ -129,7 +131,7 @@ void Playing_Exit(StateMachineContext_T *context)
|
||||||
{
|
{
|
||||||
LOG("Exiting the Playing state.");
|
LOG("Exiting the Playing state.");
|
||||||
xTimerStop(BLEStatusTimer, portMAX_DELAY);
|
xTimerStop(BLEStatusTimer, portMAX_DELAY);
|
||||||
BLE_UpdateStatusPacket();
|
BLE_UpdateStatusPacket(context->States.Next_State);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleBLEStatusPacket(const BLE_StatusPacket_T *const packet)
|
void HandleBLEStatusPacket(const BLE_StatusPacket_T *const packet)
|
||||||
|
@ -179,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};
|
// NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_BLE_NEARBY, .Data = (void *)&BLE_RXd_data, .Prominence = NEOPIXELS_BACKGROUND};
|
||||||
// xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
// xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
|
|
||||||
|
BLE_FreePacketBuffer((BLE_GenericPacketType_T *)packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
|
void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
|
||||||
|
@ -202,7 +206,11 @@ void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
|
||||||
{
|
{
|
||||||
if (BLE_IsPacketNew(packet->BD_ADDR, BLE_PACKET_TYPE_TAG, packet->event_number))
|
if (BLE_IsPacketNew(packet->BD_ADDR, BLE_PACKET_TYPE_TAG, packet->event_number))
|
||||||
{
|
{
|
||||||
|
Health_In_Percent = (int16_t)Get_Health();
|
||||||
|
|
||||||
if (packet->damage > 0)
|
if (packet->damage > 0)
|
||||||
|
{
|
||||||
|
if (Health_In_Percent > 0)
|
||||||
{
|
{
|
||||||
Reduce_Health(packet->damage);
|
Reduce_Health(packet->damage);
|
||||||
|
|
||||||
|
@ -214,9 +222,9 @@ void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TAG_RECEIVED, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)&ReceivedTagColor};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TAG_RECEIVED, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)&ReceivedTagColor};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (packet->damage < 0)
|
else if (packet->damage < 0)
|
||||||
{
|
{
|
||||||
Health_In_Percent = (uint8_t)Get_Health();
|
|
||||||
Health_In_Percent -= packet->damage;
|
Health_In_Percent -= packet->damage;
|
||||||
if (Health_In_Percent > MAX_HEALTH)
|
if (Health_In_Percent > MAX_HEALTH)
|
||||||
{
|
{
|
||||||
|
@ -236,4 +244,6 @@ void HandleBLETagPacket(const BLE_TagPacket_T *const packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BLE_FreePacketBuffer((BLE_GenericPacketType_T *)packet);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ static void Playing__Tagged_Out_Entry(StateMachineContext_T * context)
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TAGGED_OUT, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TAGGED_OUT, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
|
|
||||||
BLE_UpdateStatusPacket();
|
BLE_UpdateStatusPacket(STATE_PLAYING__TAGGED_OUT);
|
||||||
|
|
||||||
Increment_Times_Tagged_Out();
|
Increment_Times_Tagged_Out();
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,18 @@ static void Configuring_Entry(StateMachineContext_T *context);
|
||||||
static void Configuring_Do(StateMachineContext_T *context);
|
static void Configuring_Do(StateMachineContext_T *context);
|
||||||
static void Configuring_Exit(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
|
#define MAX_MENU_DEPTH 10
|
||||||
static MenuItem_T const *CurrentMenuItem;
|
static MenuItem_T const *CurrentMenuItem;
|
||||||
static uint8_t MenuItemHistoryIndex = 0;
|
static uint8_t MenuItemHistoryIndex = 0;
|
||||||
|
@ -52,12 +64,29 @@ static void Configuring_Entry(StateMachineContext_T *context)
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_MENU, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_MENU, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
|
|
||||||
BLE_UpdateStatusPacket();
|
BLE_UpdateHelloPacket();
|
||||||
|
|
||||||
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
||||||
{
|
{
|
||||||
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
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.
|
// Reset the menu.
|
||||||
CurrentMenuItem = RootMenu;
|
CurrentMenuItem = RootMenu;
|
||||||
MenuItemHistory[MenuItemHistoryIndex] = CurrentMenuItem;
|
MenuItemHistory[MenuItemHistoryIndex] = CurrentMenuItem;
|
||||||
|
@ -150,6 +179,7 @@ static void Configuring_Do(StateMachineContext_T *context)
|
||||||
CurrentMenuItem->OnFocus(true);
|
CurrentMenuItem->OnFocus(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BLE_UpdateHelloPacket();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEVENT_TRIGGER_SWITCH_RELEASED:
|
case KEVENT_TRIGGER_SWITCH_RELEASED:
|
||||||
|
@ -180,6 +210,8 @@ static void Configuring_Do(StateMachineContext_T *context)
|
||||||
{
|
{
|
||||||
KLOG_WARN(KLOG_TAG, "Failed to increment team!");
|
KLOG_WARN(KLOG_TAG, "Failed to increment team!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BLE_UpdateHelloPacket();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -191,6 +223,19 @@ static void Configuring_Do(StateMachineContext_T *context)
|
||||||
Transition_For_Event(context, STATE_REPROGRAMMING, &Event);
|
Transition_For_Event(context, STATE_REPROGRAMMING, &Event);
|
||||||
break;
|
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:
|
default:
|
||||||
// All other events are ignored in this state.
|
// All other events are ignored in this state.
|
||||||
ProcessUnhandledEvent(&Event);
|
ProcessUnhandledEvent(&Event);
|
||||||
|
@ -214,3 +259,67 @@ static void Configuring_Exit(StateMachineContext_T *context)
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_ALL_OFF, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_ALL_OFF, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
|
@ -44,7 +44,8 @@ const StateActivity_T STATE_INITIALIZING_Activities =
|
||||||
*/
|
*/
|
||||||
static void Initializing_Entry(StateMachineContext_T * context)
|
static void Initializing_Entry(StateMachineContext_T * context)
|
||||||
{
|
{
|
||||||
LOG("Entering the Initializing state.");
|
KLOG_INFO("SystemK", "Using SystemK version %s.", SYSTEMK_VERSION_STRING);
|
||||||
|
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TEST_PATTERN, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_TEST_PATTERN, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
AudioAction_T audio_action = {.ID = AUDIO_PLAY_STARTUP_SOUND, .Play_To_Completion = true, .Data = (void *)0x00};
|
AudioAction_T audio_action = {.ID = AUDIO_PLAY_STARTUP_SOUND, .Play_To_Completion = true, .Data = (void *)0x00};
|
||||||
|
|
|
@ -22,13 +22,6 @@
|
||||||
/* Include Files */
|
/* Include Files */
|
||||||
#include "SystemK.h"
|
#include "SystemK.h"
|
||||||
|
|
||||||
const StateActivity_T STATE_DEFAULT_Activities =
|
|
||||||
{
|
|
||||||
.Entry = NULL,
|
|
||||||
.Do = NULL,
|
|
||||||
.Exit = NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
//! Activities for the top-level state machine.
|
//! Activities for the top-level state machine.
|
||||||
/*!
|
/*!
|
||||||
* This array is indexed by #StateID_T (except there is no entry for
|
* This array is indexed by #StateID_T (except there is no entry for
|
||||||
|
@ -37,7 +30,6 @@ const StateActivity_T STATE_DEFAULT_Activities =
|
||||||
*/
|
*/
|
||||||
static const StateActivity_T * Activities[] =
|
static const StateActivity_T * Activities[] =
|
||||||
{
|
{
|
||||||
&STATE_DEFAULT_Activities,
|
|
||||||
&STATE_INITIALIZING_Activities,
|
&STATE_INITIALIZING_Activities,
|
||||||
&STATE_REPROGRAMMING_Activities,
|
&STATE_REPROGRAMMING_Activities,
|
||||||
&STATE_CONFIGURING_Activities,
|
&STATE_CONFIGURING_Activities,
|
||||||
|
@ -62,6 +54,11 @@ static StateMachineContext_T Context =
|
||||||
.Time_At_State_Entry_In_Ticks = 0,
|
.Time_At_State_Entry_In_Ticks = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
StateMachineContext_T* GetContext()
|
||||||
|
{
|
||||||
|
return &Context;
|
||||||
|
}
|
||||||
|
|
||||||
TaskHandle_t State_Machine_Task_Handle;
|
TaskHandle_t State_Machine_Task_Handle;
|
||||||
|
|
||||||
void State_Machine_Task(void * pvParameters)
|
void State_Machine_Task(void * pvParameters)
|
||||||
|
|
|
@ -36,22 +36,21 @@
|
||||||
*/
|
*/
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
STATE_DEFAULT = 0,
|
STATE_INITIALIZING = 0,
|
||||||
STATE_INITIALIZING = 1,
|
STATE_REPROGRAMMING = 1,
|
||||||
STATE_REPROGRAMMING = 2,
|
STATE_CONFIGURING = 2,
|
||||||
STATE_CONFIGURING = 3,
|
STATE_READY = 3,
|
||||||
STATE_READY = 4,
|
|
||||||
|
|
||||||
// Substates of STATE_STARTING_GAME
|
// Substates of STATE_STARTING_GAME
|
||||||
STATE_STARTING_GAME__INSTIGATING = 5,
|
STATE_STARTING_GAME__INSTIGATING = 4,
|
||||||
STATE_STARTING_GAME__RESPONDING = 6,
|
STATE_STARTING_GAME__RESPONDING = 5,
|
||||||
STATE_STARTING_GAME__COUNTING_DOWN = 7,
|
STATE_STARTING_GAME__COUNTING_DOWN = 6,
|
||||||
|
|
||||||
// Substates of STATE_PLAYING
|
// Substates of STATE_PLAYING
|
||||||
STATE_PLAYING__INTERACTING = 8,
|
STATE_PLAYING__INTERACTING = 7,
|
||||||
STATE_PLAYING__TAGGED_OUT = 9,
|
STATE_PLAYING__TAGGED_OUT = 8,
|
||||||
|
|
||||||
STATE_WRAPPING_UP = 10,
|
STATE_WRAPPING_UP = 9,
|
||||||
|
|
||||||
// STATE_IS_OUT_OF_RANGE is one more than the last valid state.
|
// STATE_IS_OUT_OF_RANGE is one more than the last valid state.
|
||||||
STATE_IS_OUT_OF_RANGE,
|
STATE_IS_OUT_OF_RANGE,
|
||||||
|
@ -120,6 +119,8 @@ inline uint32_t GetTimeInState_in_ms(StateMachineContext_T * context)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StateMachineContext_T* GetContext();
|
||||||
|
|
||||||
#include "State_Configuring.h"
|
#include "State_Configuring.h"
|
||||||
#include "State_Ready.h"
|
#include "State_Ready.h"
|
||||||
#include "State_Initializing.h"
|
#include "State_Initializing.h"
|
||||||
|
|
|
@ -47,7 +47,7 @@ static void Ready_Entry(StateMachineContext_T * context)
|
||||||
LOG("Entering the Ready state.");
|
LOG("Entering the Ready state.");
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_IDLE, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_IDLE, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
BLE_UpdateStatusPacket();
|
BLE_UpdateStatusPacket(STATE_READY);
|
||||||
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
||||||
{
|
{
|
||||||
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
||||||
|
|
|
@ -47,7 +47,7 @@ static void Wrapping_Up_Entry(StateMachineContext_T * context)
|
||||||
LOG("Entering the Wrapping Up state.");
|
LOG("Entering the Wrapping Up state.");
|
||||||
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_IDLE, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
NeoPixelsAction_T neopixels_action = {.ID = NEOPIXELS_IDLE, .Prominence = NEOPIXELS_FOREGROUND, .Data = (void *)0x00};
|
||||||
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
|
||||||
BLE_UpdateStatusPacket();
|
BLE_UpdateStatusPacket(STATE_WRAPPING_UP);
|
||||||
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
if (BLE_ScanAndAdvertise() != SYSTEMK_RESULT_SUCCESS)
|
||||||
{
|
{
|
||||||
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
KLOG_ERROR(KLOG_TAG, "Couldn't start BLE!");
|
||||||
|
|
|
@ -70,6 +70,10 @@
|
||||||
#ifndef SYSTEMK_H
|
#ifndef SYSTEMK_H
|
||||||
#define SYSTEMK_H
|
#define SYSTEMK_H
|
||||||
|
|
||||||
|
#define SYSTEMK_MAJOR_VERSION 0
|
||||||
|
#define SYSTEMK_MINOR_VERSION 99
|
||||||
|
#define SYSTEMK_VERSION_STRING "00.99"
|
||||||
|
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
@ -99,6 +103,7 @@
|
||||||
#include "BLE/BLE_Packets.h"
|
#include "BLE/BLE_Packets.h"
|
||||||
#include "BLE/BLE_Packet_Tracker.h"
|
#include "BLE/BLE_Packet_Tracker.h"
|
||||||
#include "BLE/BLE_HW_Interface.h"
|
#include "BLE/BLE_HW_Interface.h"
|
||||||
|
#include "BLE/BLE_Utils.h"
|
||||||
#include "IR/IR_HW_Interface.h"
|
#include "IR/IR_HW_Interface.h"
|
||||||
#include "States/State_Machine.h"
|
#include "States/State_Machine.h"
|
||||||
#include "Menu/Menu.h"
|
#include "Menu/Menu.h"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue