From 90ac2ee71c5a814ad65afab5a2f1bd50c9213be2 Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Fri, 24 Oct 2025 12:14:56 +0200 Subject: [PATCH] Add i64 support for Preferences --- Tactility/Include/Tactility/Preferences.h | 3 ++ Tactility/Source/PreferencesEsp.cpp | 37 ++++++++++++++++++++--- Tactility/Source/PreferencesMock.cpp | 15 +++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Tactility/Include/Tactility/Preferences.h b/Tactility/Include/Tactility/Preferences.h index b1f778ac..8e5a921d 100644 --- a/Tactility/Include/Tactility/Preferences.h +++ b/Tactility/Include/Tactility/Preferences.h @@ -24,14 +24,17 @@ public: bool hasBool(const std::string& key) const; bool hasInt32(const std::string& key) const; + bool hasInt64(const std::string& key) const; bool hasString(const std::string& key) const; bool optBool(const std::string& key, bool& out) const; bool optInt32(const std::string& key, int32_t& out) const; + bool optInt64(const std::string& key, int64_t& out) const; bool optString(const std::string& key, std::string& out) const; void putBool(const std::string& key, bool value); void putInt32(const std::string& key, int32_t value); + void putInt64(const std::string& key, int64_t value); void putString(const std::string& key, const std::string& value); }; diff --git a/Tactility/Source/PreferencesEsp.cpp b/Tactility/Source/PreferencesEsp.cpp index bf75721f..263e952a 100644 --- a/Tactility/Source/PreferencesEsp.cpp +++ b/Tactility/Source/PreferencesEsp.cpp @@ -1,14 +1,14 @@ #ifdef ESP_PLATFORM -#include "nvs_flash.h" -#include "Tactility/Preferences.h" - +#include #include -#define TAG "preferences" +#include namespace tt { +constexpr auto* TAG = "Preferences"; + bool Preferences::optBool(const std::string& key, bool& out) const { nvs_handle_t handle; if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) { @@ -37,6 +37,18 @@ bool Preferences::optInt32(const std::string& key, int32_t& out) const { } } +bool Preferences::optInt64(const std::string& key, int64_t& out) const { + nvs_handle_t handle; + if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) { + TT_LOG_E(TAG, "Failed to open namespace %s", namespace_); + return false; + } else { + bool success = nvs_get_i64(handle, key.c_str(), &out) == ESP_OK; + nvs_close(handle); + return success; + } +} + bool Preferences::optString(const std::string& key, std::string& out) const { nvs_handle_t handle; if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) { @@ -63,6 +75,11 @@ bool Preferences::hasInt32(const std::string& key) const { return optInt32(key, temp); } +bool Preferences::hasInt64(const std::string& key) const { + int64_t temp; + return optInt64(key, temp); +} + bool Preferences::hasString(const std::string& key) const { std::string temp; return optString(key, temp); @@ -92,6 +109,18 @@ void Preferences::putInt32(const std::string& key, int32_t value) { } } +void Preferences::putInt64(const std::string& key, int64_t value) { + nvs_handle_t handle; + if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) { + if (nvs_set_i64(handle, key.c_str(), value) != ESP_OK) { + TT_LOG_E(TAG, "Failed to write %s:%s", namespace_, key.c_str()); + } + nvs_close(handle); + } else { + TT_LOG_E(TAG, "Failed to open namespace %s", namespace_); + } +} + void Preferences::putString(const std::string& key, const std::string& text) { nvs_handle_t handle; if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) { diff --git a/Tactility/Source/PreferencesMock.cpp b/Tactility/Source/PreferencesMock.cpp index 44b39152..d6536079 100644 --- a/Tactility/Source/PreferencesMock.cpp +++ b/Tactility/Source/PreferencesMock.cpp @@ -30,6 +30,11 @@ bool Preferences::hasInt32(const std::string& key) const { return preferences.hasInt32(bundle_key); } +bool Preferences::hasInt64(const std::string& key) const { + std::string bundle_key = get_bundle_key(namespace_, key); + return preferences.hasInt64(bundle_key); +} + bool Preferences::hasString(const std::string& key) const { std::string bundle_key = get_bundle_key(namespace_, key); return preferences.hasString(bundle_key); @@ -45,6 +50,11 @@ bool Preferences::optInt32(const std::string& key, int32_t& out) const { return preferences.optInt32(bundle_key, out); } +bool Preferences::optInt64(const std::string& key, int64_t& out) const { + std::string bundle_key = get_bundle_key(namespace_, key); + return preferences.optInt64(bundle_key, out); +} + bool Preferences::optString(const std::string& key, std::string& out) const { std::string bundle_key = get_bundle_key(namespace_, key); return preferences.optString(bundle_key, out); @@ -60,6 +70,11 @@ void Preferences::putInt32(const std::string& key, int32_t value) { return preferences.putInt32(bundle_key, value); } +void Preferences::putInt64(const std::string& key, int64_t value) { + std::string bundle_key = get_bundle_key(namespace_, key); + return preferences.putInt64(bundle_key, value); +} + void Preferences::putString(const std::string& key, const std::string& value) { std::string bundle_key = get_bundle_key(namespace_, key); return preferences.putString(bundle_key, value);