Time & date, system events and much more (#152)
## Time & Date - Added time to statusbar widget - Added Time & Date Settings app - Added TimeZone app for selecting TimeZone - Added `tt::time` namespace with timezone code ## Other changes - Added `SystemEvent` to publish/subscribe to system wide (e.g. for init code, but also for time settings changes) - Changed the way the statusbar widget works: now there's only 1 that gets shown/hidden, instead of 1 instance per app instance. - Moved `lowercase()` function to new namespace: `tt::string` - Increased T-Deck flash & PSRAM SPI frequencies to 120 MHz (from 80 MHz) - Temporary work-around (+ TODO item) for LVGL stack size (issue with WiFi app) - Suppress T-Deck keystroke debugging to debug level (privacy issue) - Improved SDL dependency wiring in various `CMakeLists.txt` - `Loader` service had some variables renamed to the newer C++ style (from previous C style)
This commit is contained in:
committed by
GitHub
parent
4f360741a1
commit
bf91e7530d
@@ -10,6 +10,7 @@
|
||||
#include "lvgl.h"
|
||||
#include "Tactility.h"
|
||||
#include "hal/usb/Usb.h"
|
||||
#include "kernel/SystemEvents.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "kernel/PanicHandler.h"
|
||||
@@ -35,6 +36,8 @@ struct Data {
|
||||
static int32_t bootThreadCallback(TT_UNUSED void* context) {
|
||||
TickType_t start_time = kernel::getTicks();
|
||||
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootSplash);
|
||||
|
||||
auto* lvgl_display = lv_display_get_default();
|
||||
tt_assert(lvgl_display != nullptr);
|
||||
auto* hal_display = (hal::Display*)lv_display_get_user_data(lvgl_display);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "TactilityCore.h"
|
||||
#include <cstring>
|
||||
#include <bits/stdc++.h>
|
||||
#include <StringUtils.h>
|
||||
|
||||
namespace tt::app::files {
|
||||
|
||||
@@ -69,25 +70,13 @@ bool isSupportedExecutableFile(const std::string& filename) {
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::basic_string<T> lowercase(const std::basic_string<T>& input) {
|
||||
std::basic_string<T> output = input;
|
||||
std::transform(
|
||||
output.begin(),
|
||||
output.end(),
|
||||
output.begin(),
|
||||
[](const T character) { return static_cast<T>(std::tolower(character)); }
|
||||
);
|
||||
return std::move(output);
|
||||
}
|
||||
|
||||
bool isSupportedImageFile(const std::string& filename) {
|
||||
// Currently only the PNG library is built into Tactility
|
||||
return lowercase(filename).ends_with(".png");
|
||||
return string::lowercase(filename).ends_with(".png");
|
||||
}
|
||||
|
||||
bool isSupportedTextFile(const std::string& filename) {
|
||||
std::string filename_lower = lowercase(filename);
|
||||
std::string filename_lower = string::lowercase(filename);
|
||||
return filename_lower.ends_with(".txt") ||
|
||||
filename_lower.ends_with(".ini") ||
|
||||
filename_lower.ends_with(".json") ||
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
#include <StringUtils.h>
|
||||
#include "lvgl.h"
|
||||
#include "lvgl/Toolbar.h"
|
||||
#include "service/loader/Loader.h"
|
||||
#include "app/timezone/TimeZone.h"
|
||||
#include "Assets.h"
|
||||
#include "Tactility.h"
|
||||
#include "time/Time.h"
|
||||
#include "lvgl/LvglSync.h"
|
||||
|
||||
#define TAG "text_viewer"
|
||||
|
||||
namespace tt::app::timedatesettings {
|
||||
|
||||
extern const AppManifest manifest;
|
||||
|
||||
struct Data {
|
||||
Mutex mutex = Mutex(Mutex::TypeRecursive);
|
||||
lv_obj_t* regionLabelWidget = nullptr;
|
||||
};
|
||||
|
||||
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
|
||||
std::shared_ptr<Data> _Nullable optData() {
|
||||
app::AppContext* app = service::loader::getCurrentApp();
|
||||
if (app->getManifest().id == manifest.id) {
|
||||
return std::static_pointer_cast<Data>(app->getData());
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static void onConfigureTimeZonePressed(TT_UNUSED lv_event_t* event) {
|
||||
timezone::start();
|
||||
}
|
||||
|
||||
static void onTimeFormatChanged(lv_event_t* event) {
|
||||
auto* widget = lv_event_get_target_obj(event);
|
||||
bool show_24 = lv_obj_has_state(widget, LV_STATE_CHECKED);
|
||||
time::setTimeFormat24Hour(show_24);
|
||||
}
|
||||
|
||||
static void onShow(AppContext& app, lv_obj_t* parent) {
|
||||
auto data = std::static_pointer_cast<Data>(app.getData());
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
auto* main_wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_flex_flow(main_wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_width(main_wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(main_wrapper, 1);
|
||||
|
||||
auto* region_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(region_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(region_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(region_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(region_wrapper, 0, 0);
|
||||
|
||||
auto* region_prefix_label = lv_label_create(region_wrapper);
|
||||
lv_label_set_text(region_prefix_label, "Region: ");
|
||||
lv_obj_align(region_prefix_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
auto* region_label = lv_label_create(region_wrapper);
|
||||
std::string timeZoneName = time::getTimeZoneName();
|
||||
if (timeZoneName.empty()) {
|
||||
timeZoneName = "not set";
|
||||
}
|
||||
data->regionLabelWidget = region_label;
|
||||
lv_label_set_text(region_label, timeZoneName.c_str());
|
||||
// TODO: Find out why Y offset is needed
|
||||
lv_obj_align_to(region_label, region_prefix_label, LV_ALIGN_OUT_RIGHT_MID, 0, 8);
|
||||
|
||||
auto* region_button = lv_button_create(region_wrapper);
|
||||
lv_obj_align(region_button, LV_ALIGN_TOP_RIGHT, 0, 0);
|
||||
auto* region_button_image = lv_image_create(region_button);
|
||||
lv_obj_add_event_cb(region_button, onConfigureTimeZonePressed, LV_EVENT_SHORT_CLICKED, nullptr);
|
||||
lv_image_set_src(region_button_image, LV_SYMBOL_SETTINGS);
|
||||
|
||||
auto* time_format_wrapper= lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(time_format_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(time_format_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(time_format_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(time_format_wrapper, 0, 0);
|
||||
|
||||
auto* time_24h_label = lv_label_create(time_format_wrapper);
|
||||
lv_label_set_text(time_24h_label, "24-hour clock");
|
||||
lv_obj_align(time_24h_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
auto* time_24h_switch = lv_switch_create(time_format_wrapper);
|
||||
lv_obj_align(time_24h_switch, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(time_24h_switch, onTimeFormatChanged, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
if (time::isTimeFormat24Hour()) {
|
||||
lv_obj_add_state(time_24h_switch, LV_STATE_CHECKED);
|
||||
} else {
|
||||
lv_obj_remove_state(time_24h_switch, LV_STATE_CHECKED);
|
||||
}
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::make_shared<Data>();
|
||||
app.setData(data);
|
||||
}
|
||||
|
||||
static void onResult(AppContext& app, Result result, const Bundle& bundle) {
|
||||
if (result == ResultOk) {
|
||||
auto data = std::static_pointer_cast<Data>(app.getData());
|
||||
auto name = timezone::getResultName(bundle);
|
||||
auto code = timezone::getResultCode(bundle);
|
||||
TT_LOG_I(TAG, "Result name=%s code=%s", name.c_str(), code.c_str());
|
||||
time::setTimeZone(name, code);
|
||||
|
||||
if (!name.empty()) {
|
||||
if (lvgl::lock(100 / portTICK_PERIOD_MS)) {
|
||||
lv_label_set_text(data->regionLabelWidget, name.c_str());
|
||||
lvgl::unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.id = "TimeDateSettings",
|
||||
.name = "Time & Date",
|
||||
.icon = TT_ASSETS_APP_ICON_TIME_DATE_SETTINGS,
|
||||
.type = TypeSettings,
|
||||
.onStart = onStart,
|
||||
.onShow = onShow,
|
||||
.onResult = onResult
|
||||
};
|
||||
|
||||
void start() {
|
||||
service::loader::startApp(manifest.id);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace tt::app::timedatesettings {
|
||||
|
||||
void start();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
#include "TimeZone.h"
|
||||
#include "app/AppManifest.h"
|
||||
#include "app/AppContext.h"
|
||||
#include "service/loader/Loader.h"
|
||||
#include "lvgl.h"
|
||||
#include "lvgl/Toolbar.h"
|
||||
#include "Partitions.h"
|
||||
#include "TactilityHeadless.h"
|
||||
#include "lvgl/LvglSync.h"
|
||||
#include "service/gui/Gui.h"
|
||||
#include <memory>
|
||||
#include <StringUtils.h>
|
||||
#include <Timer.h>
|
||||
|
||||
namespace tt::app::timezone {
|
||||
|
||||
#define TAG "timezone_select"
|
||||
|
||||
#define RESULT_BUNDLE_CODE_INDEX "code"
|
||||
#define RESULT_BUNDLE_NAME_INDEX "name"
|
||||
|
||||
extern const AppManifest manifest;
|
||||
|
||||
struct TimeZoneEntry {
|
||||
std::string name;
|
||||
std::string code;
|
||||
};
|
||||
|
||||
struct Data {
|
||||
Mutex mutex;
|
||||
std::vector<TimeZoneEntry> entries;
|
||||
std::unique_ptr<Timer> updateTimer;
|
||||
lv_obj_t* listWidget = nullptr;
|
||||
lv_obj_t* filterTextareaWidget = nullptr;
|
||||
};
|
||||
|
||||
static void updateList(std::shared_ptr<Data>& data);
|
||||
|
||||
static bool parseEntry(const std::string& input, std::string& outName, std::string& outCode) {
|
||||
std::string partial_strip = input.substr(1, input.size() - 3);
|
||||
auto first_end_quote = partial_strip.find('"');
|
||||
if (first_end_quote == std::string::npos) {
|
||||
return false;
|
||||
} else {
|
||||
outName = partial_strip.substr(0, first_end_quote);
|
||||
outCode = partial_strip.substr(first_end_quote + 3);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// region Result
|
||||
|
||||
std::string getResultName(const Bundle& bundle) {
|
||||
std::string result;
|
||||
bundle.optString(RESULT_BUNDLE_NAME_INDEX, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string getResultCode(const Bundle& bundle) {
|
||||
std::string result;
|
||||
bundle.optString(RESULT_BUNDLE_CODE_INDEX, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void setResultName(std::shared_ptr<Bundle>& bundle, const std::string& name) {
|
||||
bundle->putString(RESULT_BUNDLE_NAME_INDEX, name);
|
||||
}
|
||||
|
||||
void setResultCode(std::shared_ptr<Bundle>& bundle, const std::string& code) {
|
||||
bundle->putString(RESULT_BUNDLE_CODE_INDEX, code);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static void onUpdateTimer(std::shared_ptr<void> context) {
|
||||
auto data = std::static_pointer_cast<Data>(context);
|
||||
updateList(data);
|
||||
}
|
||||
|
||||
static void onTextareaValueChanged(TT_UNUSED lv_event_t* e) {
|
||||
auto* app = service::loader::getCurrentApp();
|
||||
auto app_data = app->getData();
|
||||
auto data = std::static_pointer_cast<Data>(app_data);
|
||||
|
||||
if (data->mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
if (data->updateTimer->isRunning()) {
|
||||
data->updateTimer->stop();
|
||||
}
|
||||
|
||||
data->updateTimer->start(500 / portTICK_PERIOD_MS);
|
||||
|
||||
data->mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
static void onListItemSelected(lv_event_t* e) {
|
||||
auto index = reinterpret_cast<std::size_t>(lv_event_get_user_data(e));
|
||||
TT_LOG_I(TAG, "Selected item at index %zu", index);
|
||||
auto* app = service::loader::getCurrentApp();
|
||||
auto data = std::static_pointer_cast<Data>(app->getData());
|
||||
|
||||
auto& entry = data->entries[index];
|
||||
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
setResultName(bundle, entry.name);
|
||||
setResultCode(bundle, entry.code);
|
||||
app->setResult(app::ResultOk, bundle);
|
||||
|
||||
service::loader::stopApp();
|
||||
}
|
||||
|
||||
static void createListItem(lv_obj_t* list, const std::string& title, size_t index) {
|
||||
lv_obj_t* btn = lv_list_add_button(list, nullptr, title.c_str());
|
||||
lv_obj_add_event_cb(btn, &onListItemSelected, LV_EVENT_SHORT_CLICKED, (void*)index);
|
||||
}
|
||||
|
||||
static void readTimeZones(const std::shared_ptr<Data>& data, std::string filter) {
|
||||
auto path = std::string(MOUNT_POINT_SYSTEM) + "/timezones.csv";
|
||||
auto* file = fopen(path.c_str(), "rb");
|
||||
if (file == nullptr) {
|
||||
TT_LOG_E(TAG, "Failed to open %s", path.c_str());
|
||||
return;
|
||||
}
|
||||
char line[96];
|
||||
std::string name;
|
||||
std::string code;
|
||||
uint32_t count = 0;
|
||||
std::vector<TimeZoneEntry> entries;
|
||||
while (fgets(line, 96, file)) {
|
||||
if (parseEntry(line, name, code)) {
|
||||
if (tt::string::lowercase(name).find(filter) != std::string::npos) {
|
||||
count++;
|
||||
entries.push_back({
|
||||
.name = name,
|
||||
.code = code
|
||||
});
|
||||
|
||||
// Safety guard
|
||||
if (count > 50) {
|
||||
// TODO: Show warning that we're not displaying a complete list
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Parse error at line %lu", count);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
if (data->mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
data->entries = std::move(entries);
|
||||
data->mutex.unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Processed %lu entries", count);
|
||||
}
|
||||
|
||||
static void updateList(std::shared_ptr<Data>& data) {
|
||||
if (lvgl::lock(100 / portTICK_PERIOD_MS)) {
|
||||
std::string filter = tt::string::lowercase(std::string(lv_textarea_get_text(data->filterTextareaWidget)));
|
||||
readTimeZones(data, filter);
|
||||
lvgl::unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
|
||||
return;
|
||||
}
|
||||
|
||||
if (lvgl::lock(100 / portTICK_PERIOD_MS)) {
|
||||
if (data->mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
lv_obj_clean(data->listWidget);
|
||||
|
||||
uint32_t index = 0;
|
||||
for (auto& entry : data->entries) {
|
||||
createListItem(data->listWidget, entry.name, index);
|
||||
index++;
|
||||
}
|
||||
|
||||
data->mutex.unlock();
|
||||
}
|
||||
|
||||
lvgl::unlock();
|
||||
}
|
||||
}
|
||||
|
||||
static void onShow(AppContext& app, lv_obj_t* parent) {
|
||||
auto data = std::static_pointer_cast<Data>(app.getData());
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
auto* search_wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_size(search_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_flex_flow(search_wrapper, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(search_wrapper, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
|
||||
lv_obj_set_style_pad_all(search_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(search_wrapper, 0, 0);
|
||||
|
||||
auto* icon = lv_image_create(search_wrapper);
|
||||
lv_obj_set_style_margin_left(icon, 8, 0);
|
||||
lv_obj_set_style_image_recolor_opa(icon, 255, 0);
|
||||
lv_obj_set_style_image_recolor(icon, lv_theme_get_color_primary(parent), 0);
|
||||
|
||||
std::string icon_path = app.getPaths()->getSystemPathLvgl("search.png");
|
||||
lv_image_set_src(icon, icon_path.c_str());
|
||||
lv_obj_set_style_image_recolor(icon, lv_theme_get_color_primary(parent), 0);
|
||||
|
||||
auto* textarea = lv_textarea_create(search_wrapper);
|
||||
lv_textarea_set_placeholder_text(textarea, "e.g. Europe/Amsterdam");
|
||||
lv_textarea_set_one_line(textarea, true);
|
||||
lv_obj_add_event_cb(textarea, onTextareaValueChanged, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
data->filterTextareaWidget = textarea;
|
||||
lv_obj_set_flex_grow(textarea, 1);
|
||||
service::gui::keyboardAddTextArea(textarea);
|
||||
|
||||
auto* list = lv_list_create(parent);
|
||||
lv_obj_set_width(list, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(list, 1);
|
||||
lv_obj_set_style_border_width(list, 0, 0);
|
||||
data->listWidget = list;
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::make_shared<Data>();
|
||||
data->updateTimer = std::make_unique<Timer>(Timer::TypeOnce, onUpdateTimer, data);
|
||||
app.setData(data);
|
||||
}
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.id = "TimeZone",
|
||||
.name = "Select timezone",
|
||||
.type = TypeHidden,
|
||||
.onStart = onStart,
|
||||
.onShow = onShow,
|
||||
};
|
||||
|
||||
void start() {
|
||||
service::loader::startApp(manifest.id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Bundle.h"
|
||||
|
||||
namespace tt::app::timezone {
|
||||
|
||||
void start();
|
||||
|
||||
std::string getResultName(const Bundle& bundle);
|
||||
std::string getResultCode(const Bundle& bundle);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user