diff options
| author | ailurux <ailuruxx@gmail.com> | 2024-02-13 10:32:59 +1100 |
|---|---|---|
| committer | ailurux <ailuruxx@gmail.com> | 2024-02-13 10:32:59 +1100 |
| commit | 15f3da0f8ca9b85125ac557aa15c222e50fc8c8a (patch) | |
| tree | 3f24a7429ed922781c22084c1dc0aeb319d96675 /src/drivers/include | |
| parent | 26ae027d6721510e4b4a8107e95acc57efaaf2c6 (diff) | |
| parent | cb379f4bc3c51eacf80b786566ab3c2675191164 (diff) | |
| download | tangara-fw-15f3da0f8ca9b85125ac557aa15c222e50fc8c8a.tar.gz | |
Merge branch 'main' into scroll-sensitivity
Diffstat (limited to 'src/drivers/include')
| -rw-r--r-- | src/drivers/include/bluetooth.hpp | 2 | ||||
| -rw-r--r-- | src/drivers/include/bluetooth_types.hpp | 2 | ||||
| -rw-r--r-- | src/drivers/include/nvs.hpp | 84 |
3 files changed, 76 insertions, 12 deletions
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<bluetooth::Device>; + auto ConnectedDevice() -> std::optional<bluetooth::MacAndName>; auto KnownDevices() -> std::vector<bluetooth::Device>; 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<uint8_t, 6> 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 560cdefd..f862b43e 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -14,44 +14,90 @@ #include "nvs.h" #include "bluetooth_types.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 <typename T> +class Setting { + public: + Setting(const char* name) : name_(name), val_(), dirty_(false) {} + + auto set(const std::optional<T>&& v) -> void { + if (val_.has_value() != v.has_value() || *val_ != *v) { + val_ = v; + dirty_ = true; + } + } + auto get() -> std::optional<T>& { return val_; } + + /* Reads the stored value from NVS and parses it into the correct type. */ + auto load(nvs_handle_t) -> std::optional<T>; + /* 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<T> 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<bluetooth::MacAndName>; - auto PreferredBluetoothDevice(std::optional<bluetooth::MacAndName>) -> bool; + auto PreferredBluetoothDevice(std::optional<bluetooth::MacAndName>) -> void; + + 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 ScrollSensitivity() -> uint_fast8_t; auto ScrollSensitivity(uint_fast8_t) -> bool; 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, @@ -61,7 +107,7 @@ class NvsStorage { }; auto PrimaryInput() -> InputModes; - auto PrimaryInput(InputModes) -> bool; + auto PrimaryInput(InputModes) -> void; explicit NvsStorage(nvs_handle_t); ~NvsStorage(); @@ -70,7 +116,23 @@ class NvsStorage { auto DowngradeSchemaSync() -> bool; auto SchemaVersionSync() -> uint8_t; + std::mutex mutex_; nvs_handle_t handle_; + + Setting<uint8_t> lock_polarity_; + Setting<uint8_t> brightness_; + Setting<uint16_t> amp_max_vol_; + Setting<uint16_t> amp_cur_vol_; + Setting<int8_t> amp_left_bias_; + Setting<uint8_t> input_mode_; + Setting<uint8_t> output_mode_; + Setting<bluetooth::MacAndName> 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 |
