summaryrefslogtreecommitdiff
path: root/src/drivers/nvs.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-09-09 15:15:00 +1000
committerjacqueline <me@jacqueline.id.au>2024-09-09 15:15:00 +1000
commit2b1a01705d62d08cefd6816ba108c5cae48a50ac (patch)
tree20ba16a6259ffc335dbcded84fa6bcbe327e9d84 /src/drivers/nvs.cpp
parent9475d10d1000c7e21a7ea311b0c8ee6a72ef46c4 (diff)
parentacdc9789c90ed6f083d054cd07930e020123457f (diff)
downloadtangara-fw-2b1a01705d62d08cefd6816ba108c5cae48a50ac.tar.gz
Merge branch 'main' into jqln/tts
Diffstat (limited to 'src/drivers/nvs.cpp')
-rw-r--r--src/drivers/nvs.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp
index 5c7d2218..d004201b 100644
--- a/src/drivers/nvs.cpp
+++ b/src/drivers/nvs.cpp
@@ -26,8 +26,10 @@ static constexpr uint8_t kSchemaVersion = 1;
static constexpr char kKeyVersion[] = "ver";
static constexpr char kKeyBluetoothPreferred[] = "bt_dev";
static constexpr char kKeyBluetoothVolumes[] = "bt_vols";
+static constexpr char kKeyBluetoothNames[] = "bt_names";
static constexpr char kKeyOutput[] = "out";
static constexpr char kKeyBrightness[] = "bright";
+static constexpr char kKeyInterfaceTheme[] = "ui_theme";
static constexpr char kKeyAmpMaxVolume[] = "hp_vol_max";
static constexpr char kKeyAmpCurrentVolume[] = "hp_vol";
static constexpr char kKeyAmpLeftBias[] = "hp_bias";
@@ -39,6 +41,7 @@ static constexpr char kKeyDisplayRows[] = "disprows";
static constexpr char kKeyHapticMotorType[] = "hapticmtype";
static constexpr char kKeyLraCalibration[] = "lra_cali";
static constexpr char kKeyDbAutoIndex[] = "dbautoindex";
+static constexpr char kKeyFastCharge[] = "fastchg";
static auto nvs_get_string(nvs_handle_t nvs, const char* key)
-> std::optional<std::string> {
@@ -130,6 +133,69 @@ auto Setting<bluetooth::MacAndName>::store(nvs_handle_t nvs,
}
template <>
+auto Setting<std::vector<bluetooth::MacAndName>>::load(nvs_handle_t nvs)
+ -> std::optional<std::vector<bluetooth::MacAndName>> {
+ auto raw = nvs_get_string(nvs, name_);
+ if (!raw) {
+ return {};
+ }
+ auto [parsed, unused, err] = cppbor::parseWithViews(
+ reinterpret_cast<const uint8_t*>(raw->data()), raw->size());
+ if (parsed->type() != cppbor::MAP) {
+ return {};
+ }
+ std::vector<bluetooth::MacAndName> res;
+ for (const auto& i : *parsed->asMap()) {
+ auto mac = i.first->asViewBstr()->view();
+ auto name = i.second->asViewTstr()->view();
+ bluetooth::MacAndName entry{
+ .mac = {},
+ .name = {name.begin(), name.end()},
+ };
+ std::copy(mac.begin(), mac.end(), entry.mac.begin());
+ res.push_back(entry);
+ }
+ return res;
+}
+
+template <>
+auto Setting<std::vector<bluetooth::MacAndName>>::store(
+ nvs_handle_t nvs,
+ std::vector<bluetooth::MacAndName> v) -> void {
+ cppbor::Map cbor{};
+ for (const auto& i : v) {
+ cbor.add(cppbor::Bstr{{i.mac.data(), i.mac.size()}}, cppbor::Tstr{i.name});
+ }
+ auto encoded = cbor.encode();
+ nvs_set_blob(nvs, name_, encoded.data(), encoded.size());
+}
+
+template <>
+auto Setting<std::string>::store(
+ nvs_handle_t nvs,
+ std::string v) -> void {
+ cppbor::Tstr cbor{v};
+ auto encoded = cbor.encode();
+ nvs_set_blob(nvs, name_, encoded.data(), encoded.size());
+}
+
+template <>
+auto Setting<std::string>::load(nvs_handle_t nvs)
+ -> std::optional<std::string> {
+ auto raw = nvs_get_string(nvs, name_);
+ if (!raw) {
+ return {};
+ }
+ auto [parsed, unused, err] = cppbor::parseWithViews(
+ reinterpret_cast<const uint8_t*>(raw->data()), raw->size());
+ if (parsed->type() != cppbor::TSTR) {
+ return {};
+ }
+ auto v = parsed->asViewTstr()->view();
+ return std::string{v.begin(), v.end()};
+}
+
+template <>
auto Setting<NvsStorage::LraData>::load(nvs_handle_t nvs)
-> std::optional<NvsStorage::LraData> {
auto raw = nvs_get_string(nvs, name_);
@@ -200,6 +266,7 @@ NvsStorage::NvsStorage(nvs_handle_t handle)
display_rows_(kKeyDisplayRows),
haptic_motor_type_(kKeyHapticMotorType),
lra_calibration_(kKeyLraCalibration),
+ fast_charge_(kKeyFastCharge),
brightness_(kKeyBrightness),
sensitivity_(kKeyScrollSensitivity),
amp_max_vol_(kKeyAmpMaxVolume),
@@ -207,7 +274,9 @@ NvsStorage::NvsStorage(nvs_handle_t handle)
amp_left_bias_(kKeyAmpLeftBias),
input_mode_(kKeyPrimaryInput),
output_mode_(kKeyOutput),
+ theme_{kKeyInterfaceTheme},
bt_preferred_(kKeyBluetoothPreferred),
+ bt_names_(kKeyBluetoothNames),
db_auto_index_(kKeyDbAutoIndex),
bt_volumes_(),
bt_volumes_dirty_(false) {}
@@ -231,7 +300,9 @@ auto NvsStorage::Read() -> void {
amp_left_bias_.read(handle_);
input_mode_.read(handle_);
output_mode_.read(handle_);
+ theme_.read(handle_);
bt_preferred_.read(handle_);
+ bt_names_.read(handle_);
db_auto_index_.read(handle_);
readBtVolumes();
}
@@ -250,7 +321,9 @@ auto NvsStorage::Write() -> bool {
amp_left_bias_.write(handle_);
input_mode_.write(handle_);
output_mode_.write(handle_);
+ theme_.write(handle_);
bt_preferred_.write(handle_);
+ bt_names_.write(handle_);
db_auto_index_.write(handle_);
writeBtVolumes();
return nvs_commit(handle_) == ESP_OK;
@@ -341,6 +414,47 @@ auto NvsStorage::BluetoothVolume(const bluetooth::mac_addr_t& mac, uint8_t vol)
bt_volumes_.Put(mac, vol);
}
+auto NvsStorage::BluetoothNames() -> std::vector<bluetooth::MacAndName> {
+ std::lock_guard<std::mutex> lock{mutex_};
+ return bt_names_.get().value_or(std::vector<bluetooth::MacAndName>{});
+}
+
+auto NvsStorage::BluetoothName(const bluetooth::mac_addr_t& mac,
+ std::optional<std::string> name) -> void {
+ std::lock_guard<std::mutex> lock{mutex_};
+ auto val = bt_names_.get();
+ if (!val) {
+ val.emplace();
+ }
+
+ bool mut = false;
+ bool found = false;
+ for (auto it = val->begin(); it != val->end(); it++) {
+ if (it->mac == mac) {
+ if (name) {
+ it->name = *name;
+ } else {
+ val->erase(it);
+ }
+ found = true;
+ mut = true;
+ break;
+ }
+ }
+
+ if (!found && name) {
+ val->push_back(bluetooth::MacAndName{
+ .mac = mac,
+ .name = *name,
+ });
+ mut = true;
+ }
+
+ if (mut) {
+ bt_names_.set(*val);
+ }
+}
+
auto NvsStorage::OutputMode() -> Output {
std::lock_guard<std::mutex> lock{mutex_};
switch (output_mode_.get().value_or(0xFF)) {
@@ -361,6 +475,16 @@ auto NvsStorage::OutputMode(Output out) -> void {
nvs_commit(handle_);
}
+auto NvsStorage::FastCharge() -> bool {
+ std::lock_guard<std::mutex> lock{mutex_};
+ return fast_charge_.get().value_or(true);
+}
+
+auto NvsStorage::FastCharge(bool en) -> void {
+ std::lock_guard<std::mutex> lock{mutex_};
+ fast_charge_.set(en);
+}
+
auto NvsStorage::ScreenBrightness() -> uint_fast8_t {
std::lock_guard<std::mutex> lock{mutex_};
return std::clamp<uint8_t>(brightness_.get().value_or(50), 0, 100);
@@ -371,6 +495,16 @@ auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> void {
brightness_.set(val);
}
+auto NvsStorage::InterfaceTheme() -> std::optional<std::string> {
+ std::lock_guard<std::mutex> lock{mutex_};
+ return theme_.get();
+}
+
+auto NvsStorage::InterfaceTheme(std::string themeFile) -> void {
+ std::lock_guard<std::mutex> lock{mutex_};
+ theme_.set(themeFile);
+}
+
auto NvsStorage::ScrollSensitivity() -> uint_fast8_t {
std::lock_guard<std::mutex> lock{mutex_};
return std::clamp<uint8_t>(sensitivity_.get().value_or(128), 0, 255);