From 26df5c4a7f54d493a09724a5f4f8f4a3a5c90f31 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 12 Feb 2024 17:07:39 +1100 Subject: Remember per-device bluetooth volume --- src/drivers/include/bluetooth.hpp | 2 +- src/drivers/include/nvs.hpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'src/drivers/include') diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index 5fdd527c..988c7e93 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -35,7 +35,7 @@ class Bluetooth { auto IsEnabled() -> bool; auto IsConnected() -> bool; - auto ConnectedDevice() -> std::optional; + auto ConnectedDevice() -> std::optional; auto KnownDevices() -> std::vector; diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp index 1184b72c..264d9784 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -15,6 +15,7 @@ #include "bluetooth_types.hpp" #include "tasks.hpp" +#include "lru_cache.hpp" namespace drivers { @@ -28,6 +29,11 @@ class NvsStorage { auto PreferredBluetoothDevice() -> std::optional; auto PreferredBluetoothDevice(std::optional) -> bool; + using BtVolumes = util::LruCache<10, bluetooth::mac_addr_t, uint8_t> ; + + auto BluetoothVolumes() -> BtVolumes; + auto BluetoothVolumes(const BtVolumes&) -> bool; + enum class Output : uint8_t { kHeadphones = 0, kBluetooth = 1, -- cgit v1.2.3 From cb379f4bc3c51eacf80b786566ab3c2675191164 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 13 Feb 2024 10:12:04 +1100 Subject: Cache pending nvs writes in memory Includes refactoring nvs settings to be a bit less duplicated --- src/drivers/include/bluetooth_types.hpp | 2 + src/drivers/include/nvs.hpp | 88 +++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 16 deletions(-) (limited to 'src/drivers/include') diff --git a/src/drivers/include/bluetooth_types.hpp b/src/drivers/include/bluetooth_types.hpp index 74434182..7cfb236f 100644 --- a/src/drivers/include/bluetooth_types.hpp +++ b/src/drivers/include/bluetooth_types.hpp @@ -14,6 +14,8 @@ typedef std::array mac_addr_t; struct MacAndName { mac_addr_t mac; std::string name; + + bool operator==(const MacAndName&) const = default; }; struct Device { diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp index 264d9784..197591d5 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -14,47 +14,87 @@ #include "nvs.h" #include "bluetooth_types.hpp" -#include "tasks.hpp" #include "lru_cache.hpp" +#include "tasks.hpp" namespace drivers { +/* + * Wrapper for a single NVS setting, with its backing value cached in memory. + * NVS values that are just plain old data should generally use these for + * simpler implementation. + */ +template +class Setting { + public: + Setting(const char* name) : name_(name), val_(), dirty_(false) {} + + auto set(const std::optional&& v) -> void { + if (val_.has_value() != v.has_value() || *val_ != *v) { + val_ = v; + dirty_ = true; + } + } + auto get() -> std::optional& { return val_; } + + /* Reads the stored value from NVS and parses it into the correct type. */ + auto load(nvs_handle_t) -> std::optional; + /* Encodes the given value and writes it to NVS. */ + auto store(nvs_handle_t, T v) -> void; + + auto read(nvs_handle_t nvs) -> void { val_ = load(nvs); } + auto write(nvs_handle_t nvs) -> void { + if (!dirty_) { + return; + } + dirty_ = false; + if (val_) { + store(nvs, *val_); + } else { + nvs_erase_key(nvs, name_); + } + } + + private: + const char* name_; + std::optional val_; + bool dirty_; +}; + class NvsStorage { public: static auto OpenSync() -> NvsStorage*; + auto Read() -> void; + auto Write() -> bool; + auto LockPolarity() -> bool; - auto LockPolarity(bool) -> bool; + auto LockPolarity(bool) -> void; auto PreferredBluetoothDevice() -> std::optional; - auto PreferredBluetoothDevice(std::optional) -> bool; + auto PreferredBluetoothDevice(std::optional) -> void; - using BtVolumes = util::LruCache<10, bluetooth::mac_addr_t, uint8_t> ; - - auto BluetoothVolumes() -> BtVolumes; - auto BluetoothVolumes(const BtVolumes&) -> bool; + auto BluetoothVolume(const bluetooth::mac_addr_t&) -> uint8_t; + auto BluetoothVolume(const bluetooth::mac_addr_t&, uint8_t) -> void; enum class Output : uint8_t { kHeadphones = 0, kBluetooth = 1, }; auto OutputMode() -> Output; - auto OutputMode(Output) -> bool; + auto OutputMode(Output) -> void; auto ScreenBrightness() -> uint_fast8_t; - auto ScreenBrightness(uint_fast8_t) -> bool; + auto ScreenBrightness(uint_fast8_t) -> void; auto AmpMaxVolume() -> uint16_t; - auto AmpMaxVolume(uint16_t) -> bool; + auto AmpMaxVolume(uint16_t) -> void; auto AmpCurrentVolume() -> uint16_t; - auto AmpCurrentVolume(uint16_t) -> bool; + auto AmpCurrentVolume(uint16_t) -> void; auto AmpLeftBias() -> int_fast8_t; - auto AmpLeftBias(int_fast8_t) -> bool; - - auto HasShownOnboarding() -> bool; - auto HasShownOnboarding(bool) -> bool; + auto AmpLeftBias(int_fast8_t) -> void; enum class InputModes : uint8_t { kButtonsOnly = 0, @@ -64,7 +104,7 @@ class NvsStorage { }; auto PrimaryInput() -> InputModes; - auto PrimaryInput(InputModes) -> bool; + auto PrimaryInput(InputModes) -> void; explicit NvsStorage(nvs_handle_t); ~NvsStorage(); @@ -73,7 +113,23 @@ class NvsStorage { auto DowngradeSchemaSync() -> bool; auto SchemaVersionSync() -> uint8_t; + std::mutex mutex_; nvs_handle_t handle_; + + Setting lock_polarity_; + Setting brightness_; + Setting amp_max_vol_; + Setting amp_cur_vol_; + Setting amp_left_bias_; + Setting input_mode_; + Setting output_mode_; + Setting bt_preferred_; + + util::LruCache<10, bluetooth::mac_addr_t, uint8_t> bt_volumes_; + bool bt_volumes_dirty_; + + auto readBtVolumes() -> void; + auto writeBtVolumes() -> void; }; } // namespace drivers -- cgit v1.2.3