summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/drivers/CMakeLists.txt3
-rw-r--r--src/drivers/bluetooth.cpp11
-rw-r--r--src/drivers/include/bluetooth.hpp19
-rw-r--r--src/drivers/include/nvs.hpp27
-rw-r--r--src/drivers/nvs.cpp73
-rw-r--r--src/system_fsm/booting.cpp4
-rw-r--r--src/system_fsm/include/system_fsm.hpp2
-rw-r--r--src/system_fsm/system_fsm.cpp1
8 files changed, 138 insertions, 2 deletions
diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt
index 40cd0c4f..9774f80b 100644
--- a/src/drivers/CMakeLists.txt
+++ b/src/drivers/CMakeLists.txt
@@ -5,6 +5,7 @@
idf_component_register(
SRCS "touchwheel.cpp" "i2s_dac.cpp" "gpios.cpp" "battery.cpp" "storage.cpp" "i2c.cpp"
"spi.cpp" "display.cpp" "display_init.cpp" "samd.cpp" "relative_wheel.cpp" "wm8523.cpp"
+ "nvs.cpp" "bluetooth.cpp"
INCLUDE_DIRS "include"
- REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span" "tasks")
+ REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span" "tasks" "nvs_flash" "bt")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp
new file mode 100644
index 00000000..9748522f
--- /dev/null
+++ b/src/drivers/bluetooth.cpp
@@ -0,0 +1,11 @@
+#include "bluetooth.hpp"
+
+#include "esp_bt.h"
+
+namespace drivers {
+
+auto Bluetooth::Enable() -> Bluetooth* {
+ return nullptr;
+}
+
+} // namespace drivers
diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp
new file mode 100644
index 00000000..f3a4b2ac
--- /dev/null
+++ b/src/drivers/include/bluetooth.hpp
@@ -0,0 +1,19 @@
+
+#pragma once
+
+#include <vector>
+
+namespace drivers {
+
+class Bluetooth {
+ public:
+ static auto Enable() -> Bluetooth*;
+ Bluetooth();
+ ~Bluetooth();
+
+ struct Device {};
+ auto Scan() -> std::vector<Device>;
+ private:
+ };
+
+}
diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp
new file mode 100644
index 00000000..be783583
--- /dev/null
+++ b/src/drivers/include/nvs.hpp
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include "esp_err.h"
+#include "nvs.h"
+
+namespace drivers {
+
+class NvsStorage {
+ public:
+ static auto Open() -> NvsStorage*;
+
+ auto SchemaVersion() -> uint8_t;
+
+ explicit NvsStorage(nvs_handle_t);
+ ~NvsStorage();
+
+ private:
+ nvs_handle_t handle_;
+};
+
+} // namespace drivers \ No newline at end of file
diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp
new file mode 100644
index 00000000..a2de9518
--- /dev/null
+++ b/src/drivers/nvs.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "nvs.hpp"
+#include <stdint.h>
+
+#include <cstdint>
+#include <memory>
+
+#include "esp_log.h"
+#include "nvs.h"
+#include "nvs_flash.h"
+
+namespace drivers {
+
+static constexpr char kTag[] = "nvm";
+static constexpr uint8_t kSchemaVersion = 1;
+
+static constexpr char kKeyVersion[] = "ver";
+
+auto NvsStorage::Open() -> NvsStorage* {
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+ ESP_LOGW(kTag, "partition needs initialisation");
+ nvs_flash_erase();
+ err = nvs_flash_init();
+ }
+ if (err != ESP_OK) {
+ ESP_LOGE(kTag, "failed to init nvm");
+ return nullptr;
+ }
+
+ nvs_handle_t handle;
+ if ((err = nvs_open("tangara", NVS_READWRITE, &handle)) != ESP_OK) {
+ ESP_LOGE(kTag, "failed to open nvs namespace");
+ return nullptr;
+ }
+
+ std::unique_ptr<NvsStorage> instance = std::make_unique<NvsStorage>(handle);
+ if (instance->SchemaVersion() < kSchemaVersion) {
+ ESP_LOGW(kTag, "namespace needs downgrading");
+ nvs_erase_all(handle);
+ nvs_set_u8(handle, kKeyVersion, kSchemaVersion);
+ err = nvs_commit(handle);
+ if (err != ESP_OK) {
+ ESP_LOGW(kTag, "failed to init namespace");
+ return nullptr;
+ }
+ }
+
+ ESP_LOGI(kTag, "nvm storage initialised okay");
+ return instance.release();
+}
+
+NvsStorage::NvsStorage(nvs_handle_t handle) : handle_(handle) {}
+
+NvsStorage::~NvsStorage() {
+ nvs_close(handle_);
+ nvs_flash_deinit();
+}
+
+auto NvsStorage::SchemaVersion() -> uint8_t {
+ uint8_t ret;
+ if (nvs_get_u8(handle_, kKeyVersion, &ret) != ESP_OK) {
+ return UINT8_MAX;
+ }
+ return ret;
+}
+
+} // namespace drivers
diff --git a/src/system_fsm/booting.cpp b/src/system_fsm/booting.cpp
index 4686748e..3d6c6a46 100644
--- a/src/system_fsm/booting.cpp
+++ b/src/system_fsm/booting.cpp
@@ -13,6 +13,7 @@
#include "event_queue.hpp"
#include "gpios.hpp"
#include "lvgl/lvgl.h"
+#include "nvs.hpp"
#include "relative_wheel.hpp"
#include "spi.hpp"
#include "system_events.hpp"
@@ -50,9 +51,10 @@ auto Booting::entry() -> void {
ESP_LOGI(kTag, "installing remaining drivers");
sSamd.reset(drivers::Samd::Create());
sBattery.reset(drivers::Battery::Create());
+ sNvs.reset(drivers::NvsStorage::Open());
sTagParser.reset(new database::TagParserImpl());
- if (!sSamd || !sBattery) {
+ if (!sSamd || !sBattery || !sNvs) {
events::System().Dispatch(FatalError{});
events::Ui().Dispatch(FatalError{});
return;
diff --git a/src/system_fsm/include/system_fsm.hpp b/src/system_fsm/include/system_fsm.hpp
index 6f0eb563..dc188780 100644
--- a/src/system_fsm/include/system_fsm.hpp
+++ b/src/system_fsm/include/system_fsm.hpp
@@ -16,6 +16,7 @@
#include "relative_wheel.hpp"
#include "samd.hpp"
#include "storage.hpp"
+#include "nvs.hpp"
#include "tag_parser.hpp"
#include "tinyfsm.hpp"
#include "touchwheel.hpp"
@@ -54,6 +55,7 @@ class SystemState : public tinyfsm::Fsm<SystemState> {
protected:
static std::shared_ptr<drivers::Gpios> sGpios;
static std::shared_ptr<drivers::Samd> sSamd;
+ static std::shared_ptr<drivers::NvsStorage> sNvs;
static std::shared_ptr<drivers::TouchWheel> sTouch;
static std::shared_ptr<drivers::RelativeWheel> sRelativeTouch;
diff --git a/src/system_fsm/system_fsm.cpp b/src/system_fsm/system_fsm.cpp
index 5f85d43c..527a8770 100644
--- a/src/system_fsm/system_fsm.cpp
+++ b/src/system_fsm/system_fsm.cpp
@@ -17,6 +17,7 @@ namespace system_fsm {
std::shared_ptr<drivers::Gpios> SystemState::sGpios;
std::shared_ptr<drivers::Samd> SystemState::sSamd;
+std::shared_ptr<drivers::NvsStorage> SystemState::sNvs;
std::shared_ptr<drivers::TouchWheel> SystemState::sTouch;
std::shared_ptr<drivers::RelativeWheel> SystemState::sRelativeTouch;