From 998f93134ab0dd565459e2ed79ebf9e876b100d0 Mon Sep 17 00:00:00 2001 From: Joe Kearney Date: Sat, 1 Mar 2025 06:12:58 -0600 Subject: [PATCH] Many new settings to support the new state machine functionality. --- components/NVM/Key_Value.c | 43 +++++++++++------------ components/NVM/Key_Value.h | 4 +++ components/NVM/Settings.c | 60 +++++++++++++++++++++++++++++++-- components/SystemK | 2 +- spiffs_image/default_config.txt | 9 ++++- 5 files changed, 90 insertions(+), 28 deletions(-) diff --git a/components/NVM/Key_Value.c b/components/NVM/Key_Value.c index 2384b9e..2872f96 100644 --- a/components/NVM/Key_Value.c +++ b/components/NVM/Key_Value.c @@ -24,10 +24,7 @@ #include #include #include - -#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; diff --git a/components/NVM/Key_Value.h b/components/NVM/Key_Value.h index 9331f1e..bbc00d6 100644 --- a/components/NVM/Key_Value.h +++ b/components/NVM/Key_Value.h @@ -19,6 +19,10 @@ * file in the root of this repository. If not, see . */ +#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); diff --git a/components/NVM/Settings.c b/components/NVM/Settings.c index aa6c230..59234b5 100644 --- a/components/NVM/Settings.c +++ b/components/NVM/Settings.c @@ -19,6 +19,7 @@ * file in the root of this repository. If not, see . */ +#include #include #include @@ -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; @@ -166,6 +204,10 @@ SystemKResult_T SETTINGS_get_uint32_t(SystemKSettingID_T id, uint32_t *value) 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; @@ -199,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; @@ -209,13 +255,21 @@ SystemKResult_T SETTINGS_set_uint32_t(SystemKSettingID_T id, uint32_t value) SystemKResult_T SETTINGS_get_device_name(char* name) { - SystemKResult_T result = SYSTEMK_RESULT_NOT_IMPLEMENTED; + 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 = SYSTEMK_RESULT_NOT_IMPLEMENTED; + SystemKResult_T result = KV_Set_Value_string(CONFIG_FILE, "Device_Name", name); return result; } diff --git a/components/SystemK b/components/SystemK index 82ec410..47822bb 160000 --- a/components/SystemK +++ b/components/SystemK @@ -1 +1 @@ -Subproject commit 82ec410264cd43e5dbd1778a1dc51ebf36f2f0b5 +Subproject commit 47822bbdec20eda1a4c21871c7b4fe17c36bb731 diff --git a/spiffs_image/default_config.txt b/spiffs_image/default_config.txt index 03a2c0a..993d5f9 100644 --- a/spiffs_image/default_config.txt +++ b/spiffs_image/default_config.txt @@ -6,15 +6,22 @@ Config_Version=1 Model=2024A +; Device-Specific +Device_Name=Another 32ESP'al + ; Game Settings Player_ID=1 Team_ID=1 Weapon_ID=10 +Max_Health=100 +N_Special_Weapons_On_Reentry=1 +T_Start_Game_in_ms=30000 +T_Game_Length_in_ms=600000 +Secondary_Color=0 ; Hardware Settings Is_Right_Handed=1 Audio_Volume=30 -T_Start_Game_in_ms=30000 ; WiFi ; SSID and password of the network this device should connect to when it is in range.