From 4247c9fe7d25c921fbfc73fc50e849c8780e7ad6 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 29 Aug 2023 16:07:56 +1000 Subject: store the screen brightness in nvs --- src/drivers/nvs.cpp | 135 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 46 deletions(-) (limited to 'src/drivers/nvs.cpp') diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp index d2110764..c2832bf4 100644 --- a/src/drivers/nvs.cpp +++ b/src/drivers/nvs.cpp @@ -6,14 +6,17 @@ #include "nvs.hpp" #include +#include #include #include #include "bluetooth.hpp" +#include "bluetooth_types.hpp" #include "esp_log.h" #include "nvs.h" #include "nvs_flash.h" +#include "tasks.hpp" namespace drivers { @@ -23,8 +26,9 @@ static constexpr uint8_t kSchemaVersion = 1; static constexpr char kKeyVersion[] = "ver"; static constexpr char kKeyBluetooth[] = "bt"; static constexpr char kKeyOutput[] = "out"; +static constexpr char kKeyBrightness[] = "bright"; -auto NvsStorage::Open() -> NvsStorage* { +auto NvsStorage::OpenSync() -> NvsStorage* { esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES) { ESP_LOGW(kTag, "partition needs initialisation"); @@ -42,73 +46,112 @@ auto NvsStorage::Open() -> NvsStorage* { return nullptr; } - std::unique_ptr instance = std::make_unique(handle); - if (instance->SchemaVersion() < kSchemaVersion) { - ESP_LOGW(kTag, "namespace needs downgrading"); - nvs_erase_all(handle); - nvs_set_u8(handle, kKeyVersion, kSchemaVersion); - err = nvs_commit(handle); - if (err != ESP_OK) { - ESP_LOGW(kTag, "failed to init namespace"); - return nullptr; - } + std::unique_ptr instance = std::make_unique( + std::unique_ptr( + tasks::Worker::Start()), + handle); + if (instance->SchemaVersionSync() < kSchemaVersion && + !instance->DowngradeSchemaSync()) { + ESP_LOGW(kTag, "failed to init namespace"); + return nullptr; } ESP_LOGI(kTag, "nvm storage initialised okay"); return instance.release(); } -NvsStorage::NvsStorage(nvs_handle_t handle) : handle_(handle) {} +NvsStorage::NvsStorage(std::unique_ptr worker, + nvs_handle_t handle) + : writer_(std::move(worker)), handle_(handle) {} NvsStorage::~NvsStorage() { nvs_close(handle_); nvs_flash_deinit(); } -auto NvsStorage::SchemaVersion() -> uint8_t { - uint8_t ret; - if (nvs_get_u8(handle_, kKeyVersion, &ret) != ESP_OK) { - return UINT8_MAX; - } - return ret; +auto NvsStorage::DowngradeSchemaSync() -> bool { + ESP_LOGW(kTag, "namespace needs downgrading"); + return writer_ + ->Dispatch([&]() -> bool { + nvs_erase_all(handle_); + nvs_set_u8(handle_, kKeyVersion, kSchemaVersion); + return nvs_commit(handle_); + }) + .get() == ESP_OK; +} + +auto NvsStorage::SchemaVersionSync() -> uint8_t { + return writer_ + ->Dispatch([&]() -> uint8_t { + uint8_t ret; + if (nvs_get_u8(handle_, kKeyVersion, &ret) != ESP_OK) { + return UINT8_MAX; + } + return ret; + }) + .get(); } auto NvsStorage::PreferredBluetoothDevice() - -> std::optional { - bluetooth::mac_addr_t out{0}; - size_t size = out.size(); - if (nvs_get_blob(handle_, kKeyBluetooth, out.data(), &size) != ESP_OK) { - return {}; - } - return out; + -> std::future> { + return writer_->Dispatch>( + [&]() -> std::optional { + bluetooth::mac_addr_t out{0}; + size_t size = out.size(); + if (nvs_get_blob(handle_, kKeyBluetooth, out.data(), &size) != ESP_OK) { + return {}; + } + return out; + }); } auto NvsStorage::PreferredBluetoothDevice( - std::optional addr) -> void { - if (!addr) { - nvs_erase_key(handle_, kKeyBluetooth); - } else { - nvs_set_blob(handle_, kKeyBluetooth, addr.value().data(), - addr.value().size()); - } - nvs_commit(handle_); + std::optional addr) -> std::future { + return writer_->Dispatch([&]() { + if (!addr) { + nvs_erase_key(handle_, kKeyBluetooth); + } else { + nvs_set_blob(handle_, kKeyBluetooth, addr.value().data(), + addr.value().size()); + } + return nvs_commit(handle_) == ESP_OK; + }); } -auto NvsStorage::OutputMode() -> Output { - uint8_t out = 0; - nvs_get_u8(handle_, kKeyOutput, &out); - switch (out) { - case static_cast(Output::kBluetooth): - return Output::kHeadphones; - case static_cast(Output::kHeadphones): - default: - return Output::kHeadphones; - } +auto NvsStorage::OutputMode() -> std::future { + return writer_->Dispatch([&]() -> Output { + uint8_t out = 0; + nvs_get_u8(handle_, kKeyOutput, &out); + switch (out) { + case static_cast(Output::kBluetooth): + return Output::kHeadphones; + case static_cast(Output::kHeadphones): + default: + return Output::kHeadphones; + } + }); +} + +auto NvsStorage::OutputMode(Output out) -> std::future { + return writer_->Dispatch([&]() { + nvs_set_u8(handle_, kKeyOutput, static_cast(out)); + return nvs_commit(handle_) == ESP_OK; + }); +} + +auto NvsStorage::ScreenBrightness() -> std::future { + return writer_->Dispatch([&]() -> uint_fast8_t { + uint8_t out = 50; + nvs_get_u8(handle_, kKeyBrightness, &out); + return out; + }); } -auto NvsStorage::OutputMode(Output out) -> void { - nvs_set_u8(handle_, kKeyOutput, static_cast(out)); - nvs_commit(handle_); +auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> std::future { + return writer_->Dispatch([&]() { + nvs_set_u8(handle_, kKeyBrightness, val); + return nvs_commit(handle_) == ESP_OK; + }); } } // namespace drivers -- cgit v1.2.3