ESP32-S3 吊坠设备固件,集成火山引擎 RTC 语音助手、蓝牙配网、 VEML7700 环境光传感器驱动及石头同频匹配交友功能。 VEML7700 驱动: - 基于 ESP-IDF i2c_master API 实现,复用项目 I2cDevice 基类 - 支持 ALS + White 双通道、自动量程、Vishay 非线性校正 - 3 次采样取中位数过滤偶发异常 石头同频匹配算法(双维度): - 维度1:光谱比值 ALS/White(石头固有光学特征,不随光照强度变化) - 维度2:亮度等级(5级对数划分,排除极端环境差异) - 比值阈值 15%,实测同石头姿势变化波动 1.6%~9.6%,安全余量充足 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
100 lines
2.7 KiB
C++
100 lines
2.7 KiB
C++
#include "settings.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <nvs_flash.h>
|
|
|
|
#define TAG "Settings"
|
|
|
|
Settings::Settings(const std::string& ns, bool read_write) : ns_(ns), read_write_(read_write) {
|
|
nvs_open(ns.c_str(), read_write_ ? NVS_READWRITE : NVS_READONLY, &nvs_handle_);
|
|
}
|
|
|
|
Settings::~Settings() {
|
|
if (nvs_handle_ != 0) {
|
|
if (read_write_ && dirty_) {
|
|
ESP_ERROR_CHECK(nvs_commit(nvs_handle_));
|
|
}
|
|
nvs_close(nvs_handle_);
|
|
}
|
|
}
|
|
|
|
std::string Settings::GetString(const std::string& key, const std::string& default_value) {
|
|
if (nvs_handle_ == 0) {
|
|
return default_value;
|
|
}
|
|
|
|
size_t length = 0;
|
|
if (nvs_get_str(nvs_handle_, key.c_str(), nullptr, &length) != ESP_OK) {
|
|
return default_value;
|
|
}
|
|
|
|
std::string value;
|
|
value.resize(length);
|
|
ESP_ERROR_CHECK(nvs_get_str(nvs_handle_, key.c_str(), value.data(), &length));
|
|
while (!value.empty() && value.back() == '\0') {
|
|
value.pop_back();
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void Settings::SetString(const std::string& key, const std::string& value) {
|
|
if (read_write_) {
|
|
ESP_ERROR_CHECK(nvs_set_str(nvs_handle_, key.c_str(), value.c_str()));
|
|
dirty_ = true;
|
|
} else {
|
|
ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str());
|
|
}
|
|
}
|
|
|
|
int32_t Settings::GetInt(const std::string& key, int32_t default_value) {
|
|
if (nvs_handle_ == 0) {
|
|
return default_value;
|
|
}
|
|
|
|
int32_t value;
|
|
if (nvs_get_i32(nvs_handle_, key.c_str(), &value) != ESP_OK) {
|
|
return default_value;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void Settings::SetInt(const std::string& key, int32_t value) {
|
|
if (read_write_) {
|
|
ESP_ERROR_CHECK(nvs_set_i32(nvs_handle_, key.c_str(), value));
|
|
dirty_ = true;
|
|
} else {
|
|
ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str());
|
|
}
|
|
}
|
|
|
|
void Settings::EraseKey(const std::string& key) {
|
|
if (read_write_) {
|
|
auto ret = nvs_erase_key(nvs_handle_, key.c_str());
|
|
if (ret != ESP_ERR_NVS_NOT_FOUND) {
|
|
ESP_ERROR_CHECK(ret);
|
|
}
|
|
} else {
|
|
ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str());
|
|
}
|
|
}
|
|
|
|
void Settings::EraseAll() {
|
|
if (read_write_) {
|
|
ESP_ERROR_CHECK(nvs_erase_all(nvs_handle_));
|
|
} else {
|
|
ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str());
|
|
}
|
|
}
|
|
|
|
void Settings::Commit() {
|
|
if (read_write_ && nvs_handle_ != 0) {
|
|
esp_err_t ret = nvs_commit(nvs_handle_);
|
|
if (ret == ESP_OK) {
|
|
dirty_ = false;
|
|
ESP_LOGI(TAG, "Committed NVS namespace %s", ns_.c_str());
|
|
} else {
|
|
ESP_LOGE(TAG, "Commit failed for namespace %s (%s)", ns_.c_str(), esp_err_to_name(ret));
|
|
}
|
|
}
|
|
}
|