summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-09-26 21:07:44 +1000
committerjacqueline <me@jacqueline.id.au>2023-09-26 21:07:44 +1000
commit252f685ef1502016ed00be4eeaa4a217386fbce0 (patch)
tree9b797795913c61c6b6c9f77e79e4a62419883f7b /src/drivers
parent96252973d9b142632b7c54f825ce1de2127754bd (diff)
downloadtangara-fw-252f685ef1502016ed00be4eeaa4a217386fbce0.tar.gz
Make NVS access synchronous again
Everything relevant is back in internal ram, and likely to stay there.
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/bluetooth.cpp4
-rw-r--r--src/drivers/include/nvs.hpp29
-rw-r--r--src/drivers/nvs.cpp212
3 files changed, 102 insertions, 143 deletions
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp
index ca9f1a9c..797a05d7 100644
--- a/src/drivers/bluetooth.cpp
+++ b/src/drivers/bluetooth.cpp
@@ -134,7 +134,7 @@ std::function<void(Event)> BluetoothState::sEventHandler_;
auto BluetoothState::Init(NvsStorage& storage) -> void {
sStorage_ = &storage;
- sPreferredDevice_ = storage.PreferredBluetoothDevice().get();
+ sPreferredDevice_ = storage.PreferredBluetoothDevice();
tinyfsm::FsmList<bluetooth::BluetoothState>::start();
}
@@ -451,7 +451,7 @@ void Connecting::react(const events::internal::A2dp& ev) {
void Connected::entry() {
ESP_LOGI(kTag, "entering connected state");
- auto stored_pref = sStorage_->PreferredBluetoothDevice().get();
+ auto stored_pref = sStorage_->PreferredBluetoothDevice();
if (stored_pref != sPreferredDevice_) {
sStorage_->PreferredBluetoothDevice(sPreferredDevice_);
}
diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp
index 91b68bc4..3e37c49e 100644
--- a/src/drivers/include/nvs.hpp
+++ b/src/drivers/include/nvs.hpp
@@ -22,38 +22,35 @@ class NvsStorage {
public:
static auto OpenSync() -> NvsStorage*;
- auto PreferredBluetoothDevice()
- -> std::future<std::optional<bluetooth::mac_addr_t>>;
- auto PreferredBluetoothDevice(std::optional<bluetooth::mac_addr_t>)
- -> std::future<bool>;
+ auto PreferredBluetoothDevice() -> std::optional<bluetooth::mac_addr_t>;
+ auto PreferredBluetoothDevice(std::optional<bluetooth::mac_addr_t>) -> bool;
enum class Output : uint8_t {
kHeadphones = 0,
kBluetooth = 1,
};
- auto OutputMode() -> std::future<Output>;
- auto OutputMode(Output) -> std::future<bool>;
+ auto OutputMode() -> Output;
+ auto OutputMode(Output) -> bool;
- auto ScreenBrightness() -> std::future<uint_fast8_t>;
- auto ScreenBrightness(uint_fast8_t) -> std::future<bool>;
+ auto ScreenBrightness() -> uint_fast8_t;
+ auto ScreenBrightness(uint_fast8_t) -> bool;
- auto AmpMaxVolume() -> std::future<uint16_t>;
- auto AmpMaxVolume(uint16_t) -> std::future<bool>;
+ auto AmpMaxVolume() -> uint16_t;
+ auto AmpMaxVolume(uint16_t) -> bool;
- auto AmpCurrentVolume() -> std::future<uint16_t>;
- auto AmpCurrentVolume(uint16_t) -> std::future<bool>;
+ auto AmpCurrentVolume() -> uint16_t;
+ auto AmpCurrentVolume(uint16_t) -> bool;
- auto HasShownOnboarding() -> std::future<bool>;
- auto HasShownOnboarding(bool) -> std::future<bool>;
+ auto HasShownOnboarding() -> bool;
+ auto HasShownOnboarding(bool) -> bool;
- explicit NvsStorage(std::unique_ptr<tasks::Worker>, nvs_handle_t);
+ explicit NvsStorage(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 67867c07..16a9609d 100644
--- a/src/drivers/nvs.cpp
+++ b/src/drivers/nvs.cpp
@@ -50,10 +50,7 @@ auto NvsStorage::OpenSync() -> NvsStorage* {
return nullptr;
}
- std::unique_ptr<NvsStorage> instance = std::make_unique<NvsStorage>(
- std::unique_ptr<tasks::Worker>(
- tasks::Worker::Start<tasks::Type::kNvsWriter>()),
- handle);
+ std::unique_ptr<NvsStorage> instance = std::make_unique<NvsStorage>(handle);
if (instance->SchemaVersionSync() < kSchemaVersion &&
!instance->DowngradeSchemaSync()) {
ESP_LOGW(kTag, "failed to init namespace");
@@ -64,9 +61,7 @@ auto NvsStorage::OpenSync() -> NvsStorage* {
return instance.release();
}
-NvsStorage::NvsStorage(std::unique_ptr<tasks::Worker> worker,
- nvs_handle_t handle)
- : writer_(std::move(worker)), handle_(handle) {}
+NvsStorage::NvsStorage(nvs_handle_t handle) : handle_(handle) {}
NvsStorage::~NvsStorage() {
nvs_close(handle_);
@@ -75,133 +70,100 @@ NvsStorage::~NvsStorage() {
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;
+ nvs_erase_all(handle_);
+ nvs_set_u8(handle_, kKeyVersion, kSchemaVersion);
+ return nvs_commit(handle_);
}
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();
+ uint8_t ret;
+ if (nvs_get_u8(handle_, kKeyVersion, &ret) != ESP_OK) {
+ return UINT8_MAX;
+ }
+ return ret;
}
auto NvsStorage::PreferredBluetoothDevice()
- -> 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;
- });
+ -> 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) -> 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() -> 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::kBluetooth;
- case static_cast<uint8_t>(Output::kHeadphones):
- default:
- return Output::kHeadphones;
- }
- });
-}
-
-auto NvsStorage::OutputMode(Output out) -> std::future<bool> {
- return writer_->Dispatch<bool>([&]() {
- uint8_t as_int = static_cast<uint8_t>(out);
- nvs_set_u8(handle_, kKeyOutput, as_int);
- 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::ScreenBrightness(uint_fast8_t val) -> std::future<bool> {
- return writer_->Dispatch<bool>([&]() {
- nvs_set_u8(handle_, kKeyBrightness, val);
- return nvs_commit(handle_) == ESP_OK;
- });
-}
-
-auto NvsStorage::AmpMaxVolume() -> std::future<uint16_t> {
- return writer_->Dispatch<uint16_t>([&]() -> uint16_t {
- uint16_t out = wm8523::kDefaultMaxVolume;
- nvs_get_u16(handle_, kKeyAmpMaxVolume, &out);
- return out;
- });
-}
-
-auto NvsStorage::AmpMaxVolume(uint16_t val) -> std::future<bool> {
- return writer_->Dispatch<bool>([&]() {
- nvs_set_u16(handle_, kKeyAmpMaxVolume, val);
- return nvs_commit(handle_) == ESP_OK;
- });
-}
-
-auto NvsStorage::AmpCurrentVolume() -> std::future<uint16_t> {
- return writer_->Dispatch<uint16_t>([&]() -> uint16_t {
- uint16_t out = wm8523::kDefaultVolume;
- nvs_get_u16(handle_, kKeyAmpCurrentVolume, &out);
- return out;
- });
-}
-
-auto NvsStorage::AmpCurrentVolume(uint16_t val) -> std::future<bool> {
- return writer_->Dispatch<bool>([&]() {
- nvs_set_u16(handle_, kKeyAmpCurrentVolume, val);
- return nvs_commit(handle_) == ESP_OK;
- });
-}
-
-auto NvsStorage::HasShownOnboarding() -> std::future<bool> {
- return writer_->Dispatch<bool>([&]() -> bool {
- uint8_t out = false;
- nvs_get_u8(handle_, kKeyOnboarded, &out);
- return out;
- });
-}
-
-auto NvsStorage::HasShownOnboarding(bool val) -> std::future<bool> {
- return writer_->Dispatch<bool>([&]() {
- nvs_set_u8(handle_, kKeyOnboarded, val);
- return nvs_commit(handle_) == ESP_OK;
- });
+ std::optional<bluetooth::mac_addr_t> addr) -> 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::kBluetooth;
+ case static_cast<uint8_t>(Output::kHeadphones):
+ default:
+ return Output::kHeadphones;
+ }
+}
+
+auto NvsStorage::OutputMode(Output out) -> bool {
+ uint8_t as_int = static_cast<uint8_t>(out);
+ nvs_set_u8(handle_, kKeyOutput, as_int);
+ return nvs_commit(handle_) == ESP_OK;
+}
+
+auto NvsStorage::ScreenBrightness() -> uint_fast8_t {
+ uint8_t out = 50;
+ nvs_get_u8(handle_, kKeyBrightness, &out);
+ return out;
+}
+
+auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> bool {
+ nvs_set_u8(handle_, kKeyBrightness, val);
+ return nvs_commit(handle_) == ESP_OK;
+}
+
+auto NvsStorage::AmpMaxVolume() -> uint16_t {
+ uint16_t out = wm8523::kDefaultMaxVolume;
+ nvs_get_u16(handle_, kKeyAmpMaxVolume, &out);
+ return out;
+}
+
+auto NvsStorage::AmpMaxVolume(uint16_t val) -> bool {
+ nvs_set_u16(handle_, kKeyAmpMaxVolume, val);
+ return nvs_commit(handle_) == ESP_OK;
+}
+
+auto NvsStorage::AmpCurrentVolume() -> uint16_t {
+ uint16_t out = wm8523::kDefaultVolume;
+ nvs_get_u16(handle_, kKeyAmpCurrentVolume, &out);
+ return out;
+}
+
+auto NvsStorage::AmpCurrentVolume(uint16_t val) -> bool {
+ nvs_set_u16(handle_, kKeyAmpCurrentVolume, val);
+ return nvs_commit(handle_) == ESP_OK;
+}
+
+auto NvsStorage::HasShownOnboarding() -> bool {
+ uint8_t out = false;
+ nvs_get_u8(handle_, kKeyOnboarded, &out);
+ return out;
+}
+
+auto NvsStorage::HasShownOnboarding(bool val) -> bool {
+ nvs_set_u8(handle_, kKeyOnboarded, val);
+ return nvs_commit(handle_) == ESP_OK;
}
} // namespace drivers