From 7e7b50910cd7daeb14025adc5532172332d87d4d Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Sun, 8 Jun 2025 14:11:41 +0200 Subject: [PATCH] Implement tt_preferences and tt_time --- Tactility/Include/Tactility/Preferences.h | 6 +- TactilityC/Include/tt_preferences.h | 83 +++++++++++++++++++++++ TactilityC/Include/tt_time.h | 36 ++++++++++ TactilityC/Source/tt_bundle.cpp | 20 +++--- TactilityC/Source/tt_init.cpp | 15 ++++ TactilityC/Source/tt_preferences.cpp | 53 +++++++++++++++ TactilityC/Source/tt_time.cpp | 29 ++++++++ 7 files changed, 232 insertions(+), 10 deletions(-) create mode 100644 TactilityC/Include/tt_preferences.h create mode 100644 TactilityC/Include/tt_time.h create mode 100644 TactilityC/Source/tt_preferences.cpp create mode 100644 TactilityC/Source/tt_time.cpp diff --git a/Tactility/Include/Tactility/Preferences.h b/Tactility/Include/Tactility/Preferences.h index d2a34812..b1f778ac 100644 --- a/Tactility/Include/Tactility/Preferences.h +++ b/Tactility/Include/Tactility/Preferences.h @@ -8,9 +8,13 @@ namespace tt { /** * Settings that persist on NVS flash for ESP32. * On simulator, the settings are only in-memory. + * + * Note that on ESP32, there are limitations: + * - namespace name is limited by NVS_NS_NAME_MAX_SIZE (generally 16 characters) + * - key is limited by NVS_KEY_NAME_MAX_SIZE (generally 16 characters) */ class Preferences { -private: + const char* namespace_; public: diff --git a/TactilityC/Include/tt_preferences.h b/TactilityC/Include/tt_preferences.h new file mode 100644 index 00000000..735982b3 --- /dev/null +++ b/TactilityC/Include/tt_preferences.h @@ -0,0 +1,83 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +/** + * Note that on ESP32, there are limitations: + * - namespace name is limited by NVS_NS_NAME_MAX_SIZE (generally 16 characters) + * - key is limited by NVS_KEY_NAME_MAX_SIZE (generally 16 characters) + */ + +/** The handle that represents a Preferences instance */ +typedef void* PreferencesHandle; + +/** + * @param[in] identifier the name of the preferences. This determines the NVS namespace on ESP. + * @return a new preferences instance + */ +PreferencesHandle tt_preferences_alloc(const char* identifier); + +/** Dealloc an existing preferences instance */ +void tt_preferences_free(PreferencesHandle handle); + +/** + * Try to get a boolean value + * @param[in] handle the handle that represents the preferences + * @param[in] key the identifier that represents the stored value (~variable name) + * @param[out] out the output value (only set when return value is set to true) + * @return true if "out" was set + */ +bool tt_preferences_opt_bool(PreferencesHandle handle, const char* key, bool* out); + +/** + * Try to get an int32_t value + * @param[in] handle the handle that represents the preferences + * @param[in] key the identifier that represents the stored value (~variable name) + * @param[out] out the output value (only set when return value is set to true) + * @return true if "out" was set + */ +bool tt_preferences_opt_int32(PreferencesHandle handle, const char* key, int32_t* out); + +/** + * Try to get a string + * @warning outSize must be large enough to include null terminator. This means that your string has to be the expected text length + 1 extra character. + * @param[in] handle the handle that represents the preferences + * @param[in] key the identifier that represents the stored value (~variable name) + * @param[out] out the buffer to store the string in + * @param[in] outSize the size of the buffer + * @return true if "out" was set + */ +bool tt_preferences_opt_string(PreferencesHandle handle, const char* key, char* out, uint32_t outSize); + +/** + * Store a boolean value + * @param[in] handle the handle that represents the preferences + * @param[in] key the identifier that represents the stored value (~variable name) + * @param[in] value the value to store + */ +void tt_preferences_put_bool(PreferencesHandle handle, const char* key, bool value); + +/** + * Store an int32_t value + * @param[in] handle the handle that represents the preferences + * @param[in] key the identifier that represents the stored value (~variable name) + * @param[in] value the value to store + */ +void tt_preferences_put_int32(PreferencesHandle handle, const char* key, int32_t value); + +/** + * Store a string value + * @param[in] handle the handle that represents the preferences + * @param[in] key the identifier that represents the stored value (~variable name) + * @param[in] value the value to store + */ +void tt_preferences_put_string(PreferencesHandle handle, const char* key, const char* value); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/TactilityC/Include/tt_time.h b/TactilityC/Include/tt_time.h new file mode 100644 index 00000000..8f444864 --- /dev/null +++ b/TactilityC/Include/tt_time.h @@ -0,0 +1,36 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * Set the timezone + * @param[in] name human-readable name + * @param[in] code the technical code (from timezones.csv) + */ +void tt_timezone_set(const char* name, const char* code); + +/** + * Get the name of the timezone + */ +const char* tt_timezone_get_name(); + +/** + * Get the code of the timezone (see timezones.csv) + */ +const char* tt_timezone_get_code(); + +/** @return true when clocks should be shown as a 24 hours one instead of 12 hours */ +bool tt_timezone_is_format_24_hour(); + +/** Set whether clocks should be shown as a 24 hours instead of 12 hours + * @param[in] show24Hour + */ +void tt_timezone_set_format_24_hour(bool show24Hour); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Source/tt_bundle.cpp b/TactilityC/Source/tt_bundle.cpp index 14530a32..4172bf2c 100644 --- a/TactilityC/Source/tt_bundle.cpp +++ b/TactilityC/Source/tt_bundle.cpp @@ -23,17 +23,19 @@ bool tt_bundle_opt_int32(BundleHandle handle, const char* key, int32_t* out) { } bool tt_bundle_opt_string(BundleHandle handle, const char* key, char* out, uint32_t outSize) { std::string out_string; - if (HANDLE_AS_BUNDLE(handle)->optString(key, out_string)) { - if (out_string.length() < outSize) { // Need 1 byte to add 0 at the end - memcpy(out, out_string.c_str(), out_string.length()); - out[out_string.length()] = 0x00; - return true; - } else { - return false; - } - } else { + + if (!HANDLE_AS_BUNDLE(handle)->optString(key, out_string)) { return false; } + + if (out_string.length() >= outSize) { + // Need 1 byte to add 0 at the end + return false; + } + + memcpy(out, out_string.c_str(), out_string.length()); + out[out_string.length()] = 0x00; + return true; } void tt_bundle_put_bool(BundleHandle handle, const char* key, bool value) { diff --git a/TactilityC/Source/tt_init.cpp b/TactilityC/Source/tt_init.cpp index a766a883..2b7af9fd 100644 --- a/TactilityC/Source/tt_init.cpp +++ b/TactilityC/Source/tt_init.cpp @@ -11,8 +11,10 @@ #include "tt_lvgl_toolbar.h" #include "tt_message_queue.h" #include "tt_mutex.h" +#include "tt_preferences.h" #include "tt_semaphore.h" #include "tt_thread.h" +#include "tt_time.h" #include "tt_timer.h" #include @@ -72,6 +74,14 @@ const struct esp_elfsym elf_symbols[] { ESP_ELFSYM_EXPORT(tt_mutex_free), ESP_ELFSYM_EXPORT(tt_mutex_lock), ESP_ELFSYM_EXPORT(tt_mutex_unlock), + ESP_ELFSYM_EXPORT(tt_preferences_alloc), + ESP_ELFSYM_EXPORT(tt_preferences_free), + ESP_ELFSYM_EXPORT(tt_preferences_opt_bool), + ESP_ELFSYM_EXPORT(tt_preferences_opt_int32), + ESP_ELFSYM_EXPORT(tt_preferences_opt_string), + ESP_ELFSYM_EXPORT(tt_preferences_put_bool), + ESP_ELFSYM_EXPORT(tt_preferences_put_int32), + ESP_ELFSYM_EXPORT(tt_preferences_put_string), ESP_ELFSYM_EXPORT(tt_semaphore_alloc), ESP_ELFSYM_EXPORT(tt_semaphore_free), ESP_ELFSYM_EXPORT(tt_semaphore_acquire), @@ -99,6 +109,11 @@ const struct esp_elfsym elf_symbols[] { ESP_ELFSYM_EXPORT(tt_timer_get_expire_time), ESP_ELFSYM_EXPORT(tt_timer_set_pending_callback), ESP_ELFSYM_EXPORT(tt_timer_set_thread_priority), + ESP_ELFSYM_EXPORT(tt_timezone_set), + ESP_ELFSYM_EXPORT(tt_timezone_get_name), + ESP_ELFSYM_EXPORT(tt_timezone_get_code), + ESP_ELFSYM_EXPORT(tt_timezone_is_format_24_hour), + ESP_ELFSYM_EXPORT(tt_timezone_set_format_24_hour), // tt::lvgl ESP_ELFSYM_EXPORT(tt_lvgl_spinner_create), // lv_event diff --git a/TactilityC/Source/tt_preferences.cpp b/TactilityC/Source/tt_preferences.cpp new file mode 100644 index 00000000..5aeb6d1d --- /dev/null +++ b/TactilityC/Source/tt_preferences.cpp @@ -0,0 +1,53 @@ +#include "tt_preferences.h" +#include +#include + +#define HANDLE_AS_PREFERENCES(handle) ((tt::Preferences*)(handle)) + +extern "C" { + +PreferencesHandle tt_preferences_alloc(const char* identifier) { + return new tt::Preferences(identifier); +} + +void tt_preferences_free(PreferencesHandle handle) { + delete HANDLE_AS_PREFERENCES(handle); +} + +bool tt_preferences_opt_bool(PreferencesHandle handle, const char* key, bool* out) { + return HANDLE_AS_PREFERENCES(handle)->optBool(key, *out); +} + +bool tt_preferences_opt_int32(PreferencesHandle handle, const char* key, int32_t* out) { + return HANDLE_AS_PREFERENCES(handle)->optInt32(key, *out); +} +bool tt_preferences_opt_string(PreferencesHandle handle, const char* key, char* out, uint32_t outSize) { + std::string out_string; + + if (!HANDLE_AS_PREFERENCES(handle)->optString(key, out_string)) { + return false; + } + + if (out_string.length() >= outSize) { + // Need 1 byte to add 0 at the end + return false; + } + + memcpy(out, out_string.c_str(), out_string.length()); + out[out_string.length()] = 0x00; + return true; +} + +void tt_preferences_put_bool(PreferencesHandle handle, const char* key, bool value) { + HANDLE_AS_PREFERENCES(handle)->putBool(key, value); +} + +void tt_preferences_put_int32(PreferencesHandle handle, const char* key, int32_t value) { + HANDLE_AS_PREFERENCES(handle)->putInt32(key, value); +} + +void tt_preferences_put_string(PreferencesHandle handle, const char* key, const char* value) { + HANDLE_AS_PREFERENCES(handle)->putString(key, value); +} + +} \ No newline at end of file diff --git a/TactilityC/Source/tt_time.cpp b/TactilityC/Source/tt_time.cpp new file mode 100644 index 00000000..c1637e3d --- /dev/null +++ b/TactilityC/Source/tt_time.cpp @@ -0,0 +1,29 @@ +#include "tt_time.h" + +#include + +using namespace tt; + +extern "C" { + +void tt_timezone_set(const char* name, const char* code) { + time::setTimeZone(name, code); +} + +const char* tt_timezone_get_name() { + return time::getTimeZoneName().c_str(); +} + +const char* tt_timezone_get_code() { + return time::getTimeZoneCode().c_str(); +} + +bool tt_timezone_is_format_24_hour() { + return time::isTimeFormat24Hour(); +} + +void tt_timezone_set_format_24_hour(bool show24Hour) { + return time::setTimeFormat24Hour(show24Hour); +} + +}