From 62dce8d9fcc139ca6dc2041c86723d19faab304f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 16 Aug 2023 10:22:30 +1000 Subject: [PATCH] Save current output mode to nvs --- src/drivers/include/nvs.hpp | 8 ++++++++ src/drivers/nvs.cpp | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp index 32c2ae73..913ad51e 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -6,6 +6,7 @@ #pragma once +#include #include #include "esp_err.h" @@ -24,6 +25,13 @@ class NvsStorage { auto PreferredBluetoothDevice() -> std::optional; auto PreferredBluetoothDevice(std::optional) -> void; + enum class Output : uint8_t { + kHeadphones = 0, + kBluetooth = 1, + }; + auto OutputMode() -> Output; + auto OutputMode(Output) -> void; + explicit NvsStorage(nvs_handle_t); ~NvsStorage(); diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp index 8c7e54a8..d2110764 100644 --- a/src/drivers/nvs.cpp +++ b/src/drivers/nvs.cpp @@ -22,6 +22,7 @@ static constexpr uint8_t kSchemaVersion = 1; static constexpr char kKeyVersion[] = "ver"; static constexpr char kKeyBluetooth[] = "bt"; +static constexpr char kKeyOutput[] = "out"; auto NvsStorage::Open() -> NvsStorage* { esp_err_t err = nvs_flash_init(); @@ -81,6 +82,7 @@ auto NvsStorage::PreferredBluetoothDevice() } return out; } + auto NvsStorage::PreferredBluetoothDevice( std::optional addr) -> void { if (!addr) { @@ -92,4 +94,21 @@ auto NvsStorage::PreferredBluetoothDevice( nvs_commit(handle_); } +auto NvsStorage::OutputMode() -> Output { + uint8_t out = 0; + nvs_get_u8(handle_, kKeyOutput, &out); + switch (out) { + case static_cast(Output::kBluetooth): + return Output::kHeadphones; + case static_cast(Output::kHeadphones): + default: + return Output::kHeadphones; + } +} + +auto NvsStorage::OutputMode(Output out) -> void { + nvs_set_u8(handle_, kKeyOutput, static_cast(out)); + nvs_commit(handle_); +} + } // namespace drivers