Many new settings to support the new state machine functionality.

This commit is contained in:
Joe Kearney 2025-03-01 06:12:58 -06:00
parent 583995097a
commit 998f93134a
5 changed files with 90 additions and 28 deletions

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;
@ -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;
}