Tactiliest/Tactility/Source/settings/SystemSettings.cpp
Ken Van Hoeylandt 0f8380e8fe
Merge develop into main (#313)
- Add app path get() functions to `TactilityC`
- Improved `Dispatcher` and `DispatcherThread`
- Improved `PubSub` (type safety)
- Created test for `DispatcherThread` and `PubSub`
- Save properties files on app exit (various apps) by posting it to the main dispatcher (fixes UI hanging briefly on app exit)
- Fixed bug with `SystemSettings` being read from the wrong file path.
- `loadPropertiesFile()` now uses `file::readLines()` instead of doing that manually
- Increased timer task stack size (required due to issues when reading a properties file for the very first time)
- General cleanup
- Created `EstimatedPower` driver that uses an ADC pin to measure voltage and estimate the battery charge that is left.
- Cleanup of T-Deck board (updated to new style)
2025-09-01 23:07:00 +02:00

77 lines
2.2 KiB
C++

#include <Tactility/Mutex.h>
#include <Tactility/file/FileLock.h>
#include <Tactility/file/PropertiesFile.h>
#include <Tactility/settings/Language.h>
#include <Tactility/settings/SystemSettings.h>
namespace tt::settings {
constexpr auto* TAG = "SystemSettings";
constexpr auto* FILE_PATH = "/data/settings/system.properties";
static Mutex mutex = Mutex();
static bool cached = false;
static SystemSettings cachedSettings;
static bool loadSystemSettingsFromFile(SystemSettings& properties) {
std::map<std::string, std::string> map;
if (!file::withLock<bool>(FILE_PATH, [&map] {
return file::loadPropertiesFile(FILE_PATH, map);
})) {
TT_LOG_E(TAG, "Failed to load %s", FILE_PATH);
return false;
}
auto language_entry = map.find("language");
if (language_entry != map.end()) {
if (!fromString(language_entry->second, properties.language)) {
TT_LOG_W(TAG, "Unknown language \"%s\" in %s", language_entry->second.c_str(), FILE_PATH);
properties.language = Language::en_US;
}
} else {
properties.language = Language::en_US;
}
auto time_format_entry = map.find("timeFormat24h");
bool time_format_24h = time_format_entry == map.end() ? true : (time_format_entry->second == "true");
properties.timeFormat24h = time_format_24h;
return true;
}
bool loadSystemSettings(SystemSettings& properties) {
auto scoped_lock = mutex.asScopedLock();
scoped_lock.lock();
if (!cached) {
if (!loadSystemSettingsFromFile(cachedSettings)) {
return false;
}
cached = true;
}
properties = cachedSettings;
return true;
}
bool saveSystemSettings(const SystemSettings& properties) {
auto scoped_lock = mutex.asScopedLock();
scoped_lock.lock();
return file::withLock<bool>(FILE_PATH, [&properties] {
std::map<std::string, std::string> map;
map["language"] = toString(properties.language);
map["timeFormat24h"] = properties.timeFormat24h ? "true" : "false";
if (!file::savePropertiesFile(FILE_PATH, map)) {
TT_LOG_E(TAG, "Failed to save %s", FILE_PATH);
return false;
}
cachedSettings = properties;
cached = true;
return true;
});
}
}