saving and charge indication working

This commit is contained in:
2026-01-19 22:05:35 -06:00
parent 40bea065a7
commit d427859804
9 changed files with 215 additions and 39 deletions

View File

@@ -5,6 +5,7 @@
#include "esp_timer.h"
#include <string.h>
static SystemState_t _systemState;
static EventGroupHandle_t _systemEvent;
@@ -15,6 +16,7 @@ static const char* NVS_NAMESPACE = "bt_devices";
static const char* NVS_KEY_COUNT = "count";
static const char* NVS_NAMESPACE_SETTINGS = "settings";
static const char* NVS_KEY_VOLUME = "volume";
static const char* NVS_KEY_SWAP_LR = "swap_lr";
static esp_err_t nvs_load_devices_internal(paired_device_t *devices, size_t *count);
static esp_err_t nvs_save_devices_internal(const paired_device_t *devices, size_t count);
@@ -39,6 +41,9 @@ void system_init(void)
// Load saved volume from NVS
system_loadVolume();
// Load saved swap L/R setting from NVS
system_loadSwapLR();
}
int system_getPrimaryAxis(void)
@@ -138,6 +143,9 @@ void system_toggleSwapLR(void)
_systemState.swapLR = !_systemState.swapLR;
ESP_LOGI("system", "Swap L/R toggled: %s", _systemState.swapLR ? "ON" : "OFF");
xSemaphoreGive(_eventManager.mutex);
// Save to NVS
system_saveSwapLR();
}
void system_setZeroAngle(void)
@@ -354,7 +362,7 @@ esp_err_t system_loadVolume(void) {
ret = nvs_open(NVS_NAMESPACE_SETTINGS, NVS_READONLY, &nvs_handle);
if (ret != ESP_OK) {
ESP_LOGI("system", "No saved volume found, using default: %d", volume);
ESP_LOGI("system", "No saved volume found, using default: %ld", volume);
system_setVolume(volume);
return ESP_OK; // Not an error, just means no saved value
}
@@ -363,10 +371,10 @@ esp_err_t system_loadVolume(void) {
nvs_close(nvs_handle);
if (ret == ESP_OK) {
ESP_LOGI("system", "Loaded volume from NVS: %d", volume);
ESP_LOGI("system", "Loaded volume from NVS: %ld", volume);
system_setVolume(volume);
} else if (ret == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI("system", "No saved volume found, using default: %d", volume);
ESP_LOGI("system", "No saved volume found, using default: %ld", volume);
system_setVolume(volume);
ret = ESP_OK; // Not an error
} else {
@@ -376,6 +384,67 @@ esp_err_t system_loadVolume(void) {
return ret;
}
esp_err_t system_saveSwapLR(void) {
nvs_handle_t nvs_handle;
esp_err_t ret;
bool swapLR = system_getSwapLR();
ret = nvs_open(NVS_NAMESPACE_SETTINGS, NVS_READWRITE, &nvs_handle);
if (ret != ESP_OK) {
ESP_LOGE("system", "Failed to open NVS namespace for swap L/R write: %s", esp_err_to_name(ret));
return ret;
}
ret = nvs_set_u8(nvs_handle, NVS_KEY_SWAP_LR, swapLR ? 1 : 0);
if (ret != ESP_OK) {
ESP_LOGE("system", "Failed to write swap L/R to NVS: %s", esp_err_to_name(ret));
nvs_close(nvs_handle);
return ret;
}
ret = nvs_commit(nvs_handle);
nvs_close(nvs_handle);
if (ret == ESP_OK) {
ESP_LOGI("system", "Swap L/R %s saved to NVS", swapLR ? "ON" : "OFF");
} else {
ESP_LOGE("system", "Failed to commit swap L/R to NVS: %s", esp_err_to_name(ret));
}
return ret;
}
esp_err_t system_loadSwapLR(void) {
nvs_handle_t nvs_handle;
esp_err_t ret;
uint8_t swapLR_u8 = 0; // Default value (OFF)
ret = nvs_open(NVS_NAMESPACE_SETTINGS, NVS_READONLY, &nvs_handle);
if (ret != ESP_OK) {
ESP_LOGI("system", "No saved swap L/R found, using default: OFF");
system_setSwapLR(false);
return ESP_OK; // Not an error, just means no saved value
}
ret = nvs_get_u8(nvs_handle, NVS_KEY_SWAP_LR, &swapLR_u8);
nvs_close(nvs_handle);
if (ret == ESP_OK) {
bool swapLR = (swapLR_u8 != 0);
ESP_LOGI("system", "Loaded swap L/R from NVS: %s", swapLR ? "ON" : "OFF");
system_setSwapLR(swapLR);
} else if (ret == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI("system", "No saved swap L/R found, using default: OFF");
system_setSwapLR(false);
ret = ESP_OK; // Not an error
} else {
ESP_LOGE("system", "Failed to read swap L/R from NVS: %s", esp_err_to_name(ret));
}
return ret;
}
void system_initNvsService(void) {
_nvsRequestQueue = xQueueCreate(10, sizeof(nvs_request_t));
if (_nvsRequestQueue == NULL) {