summaryrefslogtreecommitdiff
path: root/src/drivers/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-02-13 10:12:04 +1100
committerjacqueline <me@jacqueline.id.au>2024-02-13 10:12:04 +1100
commitcb379f4bc3c51eacf80b786566ab3c2675191164 (patch)
treee8930a80e1c39f57bfb939594d140309a743c0e2 /src/drivers/include
parent79879fbfa075007ba3db30086b0730016028daf5 (diff)
downloadtangara-fw-cb379f4bc3c51eacf80b786566ab3c2675191164.tar.gz
Cache pending nvs writes in memory
Includes refactoring nvs settings to be a bit less duplicated
Diffstat (limited to 'src/drivers/include')
-rw-r--r--src/drivers/include/bluetooth_types.hpp2
-rw-r--r--src/drivers/include/nvs.hpp88
2 files changed, 74 insertions, 16 deletions
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 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 <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;
- 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<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