Pulled in the latest SystemK (with new BLE). (#2)

The new spec is here: [KTag Beacon Specification v0.11](https://ktag.clubk.club/Technology/BLE/KTag%20Beacon%20Specification%20v0.11.pdf)

Co-authored-by: Joe Kearney <joe@clubk.club>
Reviewed-on: #2
This commit is contained in:
Joe Kearney 2025-06-08 22:05:06 +00:00
parent af01bfed91
commit 7a7ce06d66
46 changed files with 2364 additions and 1531 deletions

View file

@ -11,7 +11,8 @@
static const char *TAG = "BLE";
static bool Host_And_Controller_Synced = false;
static bool Is_Scanning_And_Advertising = false;
static bool Is_Scanning = false;
static bool Is_Advertising = false;
static uint8_t Advertising_Data[BLE_KTAG_PACKET_TOTAL_SIZE] = {0x1E, 0xFF, 0xFF, 0xFF, 'K', 'T', 'a', 'g'};
@ -247,7 +248,7 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T *data)
memcpy(Advertising_Data, data, BLE_KTAG_PACKET_TOTAL_SIZE);
}
if (Is_Scanning_And_Advertising == true)
if (Is_Advertising == true)
{
// Restart advertising to put the new data into the advertisement.
ble_gap_adv_stop();
@ -263,11 +264,32 @@ SystemKResult_T BLE_ScanAndAdvertise(void)
if (Host_And_Controller_Synced == true)
{
if (Is_Scanning_And_Advertising == false)
if (Is_Scanning == false)
{
ble_scan();
Is_Scanning = true;
}
if (Is_Advertising == false)
{
ble_advertise();
Is_Scanning_And_Advertising = true;
Is_Advertising = true;
}
result = SYSTEMK_RESULT_SUCCESS;
}
return result;
}
SystemKResult_T BLE_StopAdvertising(void)
{
SystemKResult_T result = SYSTEMK_RESULT_NOT_READY;
if (Host_And_Controller_Synced == true)
{
if (Is_Advertising == true)
{
ble_gap_adv_stop();
Is_Advertising = false;
}
result = SYSTEMK_RESULT_SUCCESS;
}

View file

@ -1,3 +1,25 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-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 <SystemK.h>
#include <driver/gpio.h>
#include <driver/rmt_tx.h>
@ -53,6 +75,11 @@ static TimedPulseTrain_T Left_Rxd_RMT_Data;
static TimedPulseTrain_T Forward_Rxd_RMT_Data;
static TimedPulseTrain_T Right_Rxd_RMT_Data;
volatile TickType_t Last_Time_Checked_In_Ticks = 0;
TagSensorLocation_T Active_Tag_Sensor = TAG_SENSOR_NONE;
SemaphoreHandle_t Tag_Sensor_Mutex;
static const TickType_t LOCKOUT_TIME_IN_TICKS = pdMS_TO_TICKS(100);
#define IR_RX_STACK_SIZE 4096
static StaticTask_t xTaskBuffer;
static StackType_t xStack[IR_RX_STACK_SIZE];
@ -66,14 +93,45 @@ typedef struct
TagSensorLocation_T location;
} RxNotification_T;
// Prevent double-receiving (or triple-receiving!) IR packets by locking out the other sensors for a short time once one sensor becomes active.
// A sensor is allowed if it is the already-active sensor or the lockout period has expired.
static BaseType_t Is_Sensor_Allowed(TagSensorLocation_T sensor_location, BaseType_t *xHigherPriorityTaskWoken)
{
BaseType_t is_allowed = pdFALSE;
TickType_t current_time_in_ticks = xTaskGetTickCountFromISR();
if (xSemaphoreTakeFromISR(Tag_Sensor_Mutex, xHigherPriorityTaskWoken) == pdTRUE)
{
if ((Active_Tag_Sensor != TAG_SENSOR_NONE) && (Active_Tag_Sensor != sensor_location) &&
((current_time_in_ticks - Last_Time_Checked_In_Ticks) < LOCKOUT_TIME_IN_TICKS))
{
// We're in lockout period and this is not the active sensor--ignore this sensor.
is_allowed = pdFALSE;
}
else
{
// It's OK to allow this sensor.
Active_Tag_Sensor = sensor_location;
Last_Time_Checked_In_Ticks = current_time_in_ticks;
is_allowed = pdTRUE;
}
xSemaphoreGiveFromISR(Tag_Sensor_Mutex, xHigherPriorityTaskWoken);
}
return is_allowed;
}
static bool RMT_Rx_Left_Done_Callback(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *user_data)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
RxNotification_T notice;
notice.count = edata->num_symbols * 2;
notice.location = TAG_SENSOR_LEFT;
xQueueSendFromISR(Receive_Queue, &notice, &xHigherPriorityTaskWoken);
if (Is_Sensor_Allowed(TAG_SENSOR_LEFT, &xHigherPriorityTaskWoken) == pdTRUE)
{
RxNotification_T notice = {.count = edata->num_symbols * 2,
.location = TAG_SENSOR_LEFT};
xQueueSendFromISR(Receive_Queue, &notice, &xHigherPriorityTaskWoken);
}
return xHigherPriorityTaskWoken;
}
@ -81,11 +139,13 @@ static bool RMT_Rx_Left_Done_Callback(rmt_channel_handle_t channel, const rmt_rx
static bool RMT_Rx_Forward_Done_Callback(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *user_data)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
RxNotification_T notice;
notice.count = edata->num_symbols * 2;
notice.location = TAG_SENSOR_FORWARD;
xQueueSendFromISR(Receive_Queue, &notice, &xHigherPriorityTaskWoken);
if (Is_Sensor_Allowed(TAG_SENSOR_FORWARD, &xHigherPriorityTaskWoken) == pdTRUE)
{
RxNotification_T notice = {.count = edata->num_symbols * 2,
.location = TAG_SENSOR_FORWARD};
xQueueSendFromISR(Receive_Queue, &notice, &xHigherPriorityTaskWoken);
}
return xHigherPriorityTaskWoken;
}
@ -93,11 +153,13 @@ static bool RMT_Rx_Forward_Done_Callback(rmt_channel_handle_t channel, const rmt
static bool RMT_Rx_Right_Done_Callback(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *user_data)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
RxNotification_T notice;
notice.count = edata->num_symbols * 2;
notice.location = TAG_SENSOR_RIGHT;
xQueueSendFromISR(Receive_Queue, &notice, &xHigherPriorityTaskWoken);
if (Is_Sensor_Allowed(TAG_SENSOR_RIGHT, &xHigherPriorityTaskWoken) == pdTRUE)
{
RxNotification_T notice = {.count = edata->num_symbols * 2,
.location = TAG_SENSOR_RIGHT};
xQueueSendFromISR(Receive_Queue, &notice, &xHigherPriorityTaskWoken);
}
return xHigherPriorityTaskWoken;
}
@ -186,6 +248,13 @@ static void IR_Receive_Task(void *param)
static void Initialize_Receive_Task(void)
{
Tag_Sensor_Mutex = xSemaphoreCreateMutex();
if (Tag_Sensor_Mutex == NULL)
{
KLOG_ERROR(TAG, "Failed to create tag sensor mutex!");
}
IR_Rx_Task_Handle = xTaskCreateStaticPinnedToCore(
IR_Receive_Task, // Function that implements the task.
"IR Rx", // Text name for the task.

View file

@ -1,3 +1,25 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-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 "esp_err.h"
void Initialize_IR(SemaphoreHandle_t init_complete);

View file

@ -24,10 +24,7 @@
#include <string.h>
#include <errno.h>
#include <SystemK.h>
#define MAX_LINE_LENGTH 140
#define MAX_KEY_LENGTH 64
#define MAX_VALUE_LENGTH 64
#include "Key_Value.h"
static const char *TAG = "NVM KV";
static const char *TEMP_FILE = "/usb/esp/temp.txt";
@ -52,10 +49,10 @@ static int KV_Parse_Line(char *line, char *key, char *value)
if (delimiter == NULL)
return 0;
*delimiter = '\0';
strncpy(key, line, MAX_KEY_LENGTH - 1);
strncpy(value, delimiter + 1, MAX_VALUE_LENGTH - 1);
key[MAX_KEY_LENGTH - 1] = '\0';
value[MAX_VALUE_LENGTH - 1] = '\0';
strncpy(key, line, KV_MAX_KEY_LENGTH - 1);
strncpy(value, delimiter + 1, KV_MAX_VALUE_LENGTH - 1);
key[KV_MAX_KEY_LENGTH - 1] = '\0';
value[KV_MAX_VALUE_LENGTH - 1] = '\0';
KV_Trim(key);
KV_Trim(value);
return 1;
@ -70,8 +67,8 @@ SystemKResult_T KV_Get_Value_string(const char *filename, const char *search_key
return SYSTEMK_RESULT_FILE_NOT_FOUND;
}
char line[MAX_LINE_LENGTH];
char key[MAX_KEY_LENGTH];
char line[KV_MAX_LINE_LENGTH];
char key[KV_MAX_KEY_LENGTH];
while (fgets(line, sizeof(line), file))
{
@ -104,16 +101,16 @@ SystemKResult_T KV_Set_Value_string(const char *filename, const char *set_key, c
return SYSTEMK_RESULT_WRITE_FAILED;
}
char line[MAX_LINE_LENGTH];
char line_copy[MAX_LINE_LENGTH];
char key[MAX_KEY_LENGTH];
char value[MAX_VALUE_LENGTH];
char line[KV_MAX_LINE_LENGTH];
char line_copy[KV_MAX_LINE_LENGTH];
char key[KV_MAX_KEY_LENGTH];
char value[KV_MAX_VALUE_LENGTH];
int found = 0;
while (fgets(line, sizeof(line), file))
{
strncpy(line_copy, line, MAX_LINE_LENGTH);
line_copy[MAX_LINE_LENGTH - 1] = '\0'; // Ensure null-termination
strncpy(line_copy, line, KV_MAX_LINE_LENGTH);
line_copy[KV_MAX_LINE_LENGTH - 1] = '\0'; // Ensure null-termination
if (KV_Parse_Line(line, key, value) && strcmp(key, set_key) == 0)
{
@ -142,7 +139,7 @@ SystemKResult_T KV_Set_Value_string(const char *filename, const char *set_key, c
SystemKResult_T KV_Get_Value_uint32(const char *filename, const char *search_key, uint32_t *value)
{
char value_str[MAX_VALUE_LENGTH];
char value_str[KV_MAX_VALUE_LENGTH];
SystemKResult_T result = KV_Get_Value_string(filename, search_key, value_str);
if (result != SYSTEMK_RESULT_SUCCESS)
@ -179,10 +176,10 @@ SystemKResult_T KV_Get_Value_uint32(const char *filename, const char *search_key
SystemKResult_T KV_Set_Value_uint32(const char *filename, const char *set_key, uint32_t *set_value)
{
char value_str[MAX_VALUE_LENGTH];
int written = snprintf(value_str, MAX_VALUE_LENGTH, "%lu", *set_value);
char value_str[KV_MAX_VALUE_LENGTH];
int written = snprintf(value_str, KV_MAX_VALUE_LENGTH, "%lu", *set_value);
if (written < 0 || written >= MAX_VALUE_LENGTH)
if (written < 0 || written >= KV_MAX_VALUE_LENGTH)
{
KLOG_ERROR(TAG, "Error converting uint32_t to string for key %s!", set_key);
return SYSTEMK_RESULT_WRONG_DATATYPE;
@ -215,10 +212,10 @@ SystemKResult_T KV_Get_Value_uint8(const char *filename, const char *search_key,
SystemKResult_T KV_Set_Value_uint8(const char *filename, const char *set_key, uint8_t *set_value)
{
char value_str[MAX_VALUE_LENGTH];
int written = snprintf(value_str, MAX_VALUE_LENGTH, "%u", *set_value);
char value_str[KV_MAX_VALUE_LENGTH];
int written = snprintf(value_str, KV_MAX_VALUE_LENGTH, "%u", *set_value);
if (written < 0 || written >= MAX_VALUE_LENGTH)
if (written < 0 || written >= KV_MAX_VALUE_LENGTH)
{
KLOG_ERROR(TAG, "Error converting uint8_t to string for key %s!", set_key);
return SYSTEMK_RESULT_WRONG_DATATYPE;

View file

@ -19,6 +19,10 @@
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#define KV_MAX_LINE_LENGTH 140
#define KV_MAX_KEY_LENGTH 64
#define KV_MAX_VALUE_LENGTH 64
SystemKResult_T KV_Get_Value_string(const char *filename, const char *search_key, char *value);
SystemKResult_T KV_Set_Value_string(const char *filename, const char *set_key, const char *set_value);

View file

@ -19,6 +19,7 @@
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <SystemK.h>
#include <NVM.h>
@ -27,6 +28,8 @@ static const uint8_t UNINTIALIZED_UINT8 = UINT8_MAX;
static uint8_t Cached_Team_ID = UNINTIALIZED_UINT8;
static uint8_t Cached_Player_ID = UNINTIALIZED_UINT8;
static uint8_t Cached_Weapon_ID = UNINTIALIZED_UINT8;
static uint8_t Cached_Max_Health = UNINTIALIZED_UINT8;
static uint8_t Cached_N_Special_Weapons_On_Reentry = UNINTIALIZED_UINT8;
SystemKResult_T SETTINGS_get_uint8_t(SystemKSettingID_T id, uint8_t *value)
{
@ -55,7 +58,6 @@ SystemKResult_T SETTINGS_get_uint8_t(SystemKSettingID_T id, uint8_t *value)
key = "Team_ID";
cached_value = &Cached_Team_ID;
}
break;
case SYSTEMK_SETTING_PLAYERID:
@ -84,6 +86,32 @@ SystemKResult_T SETTINGS_get_uint8_t(SystemKSettingID_T id, uint8_t *value)
}
break;
case SYSTEMK_SETTING_MAX_HEALTH:
if (Cached_Max_Health != UNINTIALIZED_UINT8)
{
*value = Cached_Max_Health;
result = SYSTEMK_RESULT_SUCCESS;
}
else
{
key = "Max_Health";
cached_value = &Cached_Max_Health;
}
break;
case SYSTEMK_SETTING_N_SPECIAL_WEAPONS_ON_REENTRY:
if (Cached_N_Special_Weapons_On_Reentry != UNINTIALIZED_UINT8)
{
*value = Cached_N_Special_Weapons_On_Reentry;
result = SYSTEMK_RESULT_SUCCESS;
}
else
{
key = "N_Special_Weapons_On_Reentry";
cached_value = &Cached_N_Special_Weapons_On_Reentry;
}
break;
default:
result = SYSTEMK_RESULT_WRONG_DATATYPE;
break;
@ -142,6 +170,16 @@ SystemKResult_T SETTINGS_set_uint8_t(SystemKSettingID_T id, uint8_t value)
result = KV_Set_Value_uint8(CONFIG_FILE, "Weapon_ID", &value);
break;
case SYSTEMK_SETTING_MAX_HEALTH:
Cached_Max_Health = value;
result = KV_Set_Value_uint8(CONFIG_FILE, "Max_Health", &value);
break;
case SYSTEMK_SETTING_N_SPECIAL_WEAPONS_ON_REENTRY:
Cached_N_Special_Weapons_On_Reentry = value;
result = KV_Set_Value_uint8(CONFIG_FILE, "N_Special_Weapons_On_Reentry", &value);
break;
default:
result = SYSTEMK_RESULT_WRONG_DATATYPE;
break;
@ -157,10 +195,19 @@ SystemKResult_T SETTINGS_get_uint32_t(SystemKSettingID_T id, uint32_t *value)
switch (id)
{
case SYSTEMK_SETTING_DEVICE_TYPE:
*value = BLE_DEVICE_TYPE_32ESPECIAL;
return result;
break;
case SYSTEMK_SETTING_T_START_GAME_in_ms:
key = "T_Start_Game_in_ms";
break;
case SYSTEMK_SETTING_T_GAME_LENGTH_in_ms:
key = "T_Game_Length_in_ms";
break;
default:
result = SYSTEMK_RESULT_WRONG_DATATYPE;
break;
@ -194,6 +241,10 @@ SystemKResult_T SETTINGS_set_uint32_t(SystemKSettingID_T id, uint32_t value)
result = KV_Set_Value_uint32(CONFIG_FILE, "T_Start_Game_in_ms", &value);
break;
case SYSTEMK_SETTING_T_GAME_LENGTH_in_ms:
result = KV_Set_Value_uint32(CONFIG_FILE, "T_Game_Length_in_ms", &value);
break;
default:
result = SYSTEMK_RESULT_WRONG_DATATYPE;
break;
@ -202,6 +253,26 @@ SystemKResult_T SETTINGS_set_uint32_t(SystemKSettingID_T id, uint32_t value)
return result;
}
SystemKResult_T SETTINGS_get_device_name(char* name)
{
char buffer[KV_MAX_VALUE_LENGTH];
SystemKResult_T result = KV_Get_Value_string(CONFIG_FILE, "Device_Name", buffer);
if (result == SYSTEMK_RESULT_SUCCESS)
{
memcpy(name, buffer, SYSTEMK_MAX_CHARS_IN_DEVICE_NAME);
buffer[SYSTEMK_MAX_CHARS_IN_DEVICE_NAME] = 0x00;
}
return result;
}
SystemKResult_T SETTINGS_set_device_name(char* name)
{
SystemKResult_T result = KV_Set_Value_string(CONFIG_FILE, "Device_Name", name);
return result;
}
// Settings are saved on change in this implementation.
SystemKResult_T SETTINGS_Save(void)
{

@ -1 +1 @@
Subproject commit 4fe072f2d3280b19aa53e197bd22ec44b174ff88
Subproject commit bfcdf4c354ae6c96165e11e2fd6d52d0c8ab91eb