diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-08-29 16:07:56 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-08-29 16:07:56 +1000 |
| commit | 4247c9fe7d25c921fbfc73fc50e849c8780e7ad6 (patch) | |
| tree | b8288c7772010edfb4a398596bef8fd35b771a95 /src/drivers | |
| parent | 773f2857678727f416a67a3a5ae71bd5b6761078 (diff) | |
| download | tangara-fw-4247c9fe7d25c921fbfc73fc50e849c8780e7ad6.tar.gz | |
store the screen brightness in nvs
Diffstat (limited to 'src/drivers')
| -rw-r--r-- | src/drivers/bluetooth.cpp | 4 | ||||
| -rw-r--r-- | src/drivers/include/nvs.hpp | 25 | ||||
| -rw-r--r-- | src/drivers/nvs.cpp | 135 |
3 files changed, 108 insertions, 56 deletions
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index f6992f05..f3373849 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -120,7 +120,7 @@ std::atomic<StreamBufferHandle_t> BluetoothState::sSource_; auto BluetoothState::Init(NvsStorage* storage) -> void { sStorage_ = storage; - sPreferredDevice_ = storage->PreferredBluetoothDevice(); + sPreferredDevice_ = storage->PreferredBluetoothDevice().get(); tinyfsm::FsmList<bluetooth::BluetoothState>::start(); } @@ -428,7 +428,7 @@ void Connecting::react(const events::internal::A2dp& ev) { void Connected::entry() { ESP_LOGI(kTag, "entering connected state"); - auto stored_pref = sStorage_->PreferredBluetoothDevice(); + auto stored_pref = sStorage_->PreferredBluetoothDevice().get(); if (stored_pref != sPreferredDevice_) { sStorage_->PreferredBluetoothDevice(sPreferredDevice_); } diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp index 913ad51e..bc88f88d 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -7,35 +7,44 @@ #pragma once #include <stdint.h> +#include <memory> #include <optional> #include "esp_err.h" #include "nvs.h" #include "bluetooth_types.hpp" +#include "tasks.hpp" namespace drivers { class NvsStorage { public: - static auto Open() -> NvsStorage*; + static auto OpenSync() -> NvsStorage*; - auto SchemaVersion() -> uint8_t; - - auto PreferredBluetoothDevice() -> std::optional<bluetooth::mac_addr_t>; - auto PreferredBluetoothDevice(std::optional<bluetooth::mac_addr_t>) -> void; + auto PreferredBluetoothDevice() + -> std::future<std::optional<bluetooth::mac_addr_t>>; + auto PreferredBluetoothDevice(std::optional<bluetooth::mac_addr_t>) + -> std::future<bool>; enum class Output : uint8_t { kHeadphones = 0, kBluetooth = 1, }; - auto OutputMode() -> Output; - auto OutputMode(Output) -> void; + auto OutputMode() -> std::future<Output>; + auto OutputMode(Output) -> std::future<bool>; + + auto ScreenBrightness() -> std::future<uint_fast8_t>; + auto ScreenBrightness(uint_fast8_t) -> std::future<bool>; - explicit NvsStorage(nvs_handle_t); + explicit NvsStorage(std::unique_ptr<tasks::Worker>, nvs_handle_t); ~NvsStorage(); private: + auto DowngradeSchemaSync() -> bool; + auto SchemaVersionSync() -> uint8_t; + + std::unique_ptr<tasks::Worker> writer_; nvs_handle_t handle_; }; 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 <stdint.h> +#include <sys/_stdint.h> #include <cstdint> #include <memory> #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<NvsStorage> instance = std::make_unique<NvsStorage>(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<NvsStorage> instance = std::make_unique<NvsStorage>( + std::unique_ptr<tasks::Worker>( + tasks::Worker::Start<tasks::Type::kNvsWriter>()), + 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<tasks::Worker> 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>([&]() -> 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 { + 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> { - 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<std::optional<bluetooth::mac_addr_t>> { + return writer_->Dispatch<std::optional<bluetooth::mac_addr_t>>( + [&]() -> std::optional<bluetooth::mac_addr_t> { + 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<bluetooth::mac_addr_t> 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<bluetooth::mac_addr_t> addr) -> std::future<bool> { + return writer_->Dispatch<bool>([&]() { + 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<uint8_t>(Output::kBluetooth): - return Output::kHeadphones; - case static_cast<uint8_t>(Output::kHeadphones): - default: - return Output::kHeadphones; - } +auto NvsStorage::OutputMode() -> std::future<Output> { + return writer_->Dispatch<Output>([&]() -> Output { + uint8_t out = 0; + nvs_get_u8(handle_, kKeyOutput, &out); + switch (out) { + case static_cast<uint8_t>(Output::kBluetooth): + return Output::kHeadphones; + case static_cast<uint8_t>(Output::kHeadphones): + default: + return Output::kHeadphones; + } + }); +} + +auto NvsStorage::OutputMode(Output out) -> std::future<bool> { + return writer_->Dispatch<bool>([&]() { + nvs_set_u8(handle_, kKeyOutput, static_cast<uint8_t>(out)); + return nvs_commit(handle_) == ESP_OK; + }); +} + +auto NvsStorage::ScreenBrightness() -> std::future<uint_fast8_t> { + return writer_->Dispatch<uint_fast8_t>([&]() -> 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<uint8_t>(out)); - nvs_commit(handle_); +auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> std::future<bool> { + return writer_->Dispatch<bool>([&]() { + nvs_set_u8(handle_, kKeyBrightness, val); + return nvs_commit(handle_) == ESP_OK; + }); } } // namespace drivers |
