Implement UI scaling and more (#501)

**New Features**
 * Runtime font accessors and new symbol fonts for text, launcher, statusbar, and shared icons.
 * Added font height base setting to device.properties
 * Text fonts now have 3 sizes: small, default, large

**Improvements**
 * Renamed `UiScale` to `UiDensity`
 * Statusbar, toolbar and many UI components now compute heights and spacing from fonts/density.
 * SSD1306 initialization sequence refined for more stable startup.
 * Multiple image assets replaced by symbol-font rendering.
 * Many layout improvements related to density, font scaling and icon scaling
 * Updated folder name capitalization for newer style
This commit is contained in:
Ken Van Hoeylandt
2026-02-15 01:41:47 +01:00
committed by GitHub
parent 72c9b2b113
commit 9a11e6f47b
264 changed files with 5923 additions and 494 deletions
@@ -0,0 +1,142 @@
// SPDX-License-Identifier: Apache-2.0
#include <queue>
#include <tactility/concurrent/dispatcher.h>
#include "tactility/error.h"
#include <atomic>
#include <tactility/concurrent/event_group.h>
#include <tactility/concurrent/mutex.h>
#include <tactility/log.h>
#define TAG "Dispatcher"
static constexpr EventBits_t BACKPRESSURE_WARNING_COUNT = 100U;
static constexpr EventBits_t WAIT_FLAG = 1U;
struct QueuedItem {
DispatcherCallback callback;
void* context;
};
struct DispatcherData {
Mutex mutex = { 0 };
std::queue<QueuedItem> queue = {};
EventGroupHandle_t eventGroup = nullptr;
std::atomic<bool> shutdown{false}; // TODO: Use EventGroup
DispatcherData() {
event_group_construct(&eventGroup);
mutex_construct(&mutex);
}
~DispatcherData() {
event_group_destruct(&eventGroup);
mutex_destruct(&mutex);
}
};
#define dispatcher_data(handle) static_cast<DispatcherData*>(handle)
extern "C" {
DispatcherHandle_t dispatcher_alloc(void) {
return new DispatcherData();
}
void dispatcher_free(DispatcherHandle_t dispatcher) {
auto* data = dispatcher_data(dispatcher);
data->shutdown.store(true, std::memory_order_release);
mutex_lock(&data->mutex);
mutex_unlock(&data->mutex);
delete data;
}
error_t dispatcher_dispatch_timed(DispatcherHandle_t dispatcher, void* callbackContext, DispatcherCallback callback, TickType_t timeout) {
auto* data = dispatcher_data(dispatcher);
// Mutate
if (!mutex_try_lock(&data->mutex, timeout)) {
#ifdef ESP_PLATFORM
LOG_E(TAG, "Mutex acquisition timeout");
#endif
return ERROR_TIMEOUT;
}
if (data->shutdown.load(std::memory_order_acquire)) {
mutex_unlock(&data->mutex);
return ERROR_INVALID_STATE;
}
data->queue.push({
.callback = callback,
.context = callbackContext
});
if (data->queue.size() == BACKPRESSURE_WARNING_COUNT) {
#ifdef ESP_PLATFORM
LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
#endif
}
mutex_unlock(&data->mutex);
if (event_group_set(data->eventGroup, WAIT_FLAG) != ERROR_NONE) {
#ifdef ESP_PLATFORM
LOG_E(TAG, "Failed to set flag");
#endif
return ERROR_RESOURCE;
}
return ERROR_NONE;
}
error_t dispatcher_consume_timed(DispatcherHandle_t dispatcher, TickType_t timeout) {
auto* data = dispatcher_data(dispatcher);
// TODO: keep track of time and consider the timeout input as total timeout
// Wait for signal
error_t error = event_group_wait(data->eventGroup, WAIT_FLAG, false, true, nullptr, timeout);
if (error != ERROR_NONE) {
if (error == ERROR_TIMEOUT) {
return ERROR_TIMEOUT;
} else {
return ERROR_RESOURCE;
}
}
if (data->shutdown.load(std::memory_order_acquire)) {
return ERROR_INVALID_STATE;
}
// Mutate
bool processing = true;
do {
if (mutex_try_lock(&data->mutex, 10)) {
if (!data->queue.empty()) {
// Make a copy, so it's thread-safe when we unlock
auto entry = data->queue.front();
data->queue.pop();
processing = !data->queue.empty();
// Don't keep lock as callback might be slow and we want to allow dispatch in the meanwhile
mutex_unlock(&data->mutex);
entry.callback(entry.context);
} else {
processing = false;
mutex_unlock(&data->mutex);
}
} else {
#ifdef ESP_PLATFORM
LOG_W(TAG, "Mutex acquisition timeout");
#endif
}
} while (processing && !data->shutdown.load(std::memory_order_acquire));
return ERROR_NONE;
}
}
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/concurrent/event_group.h>
#include <tactility/error.h>
#ifdef __cplusplus
extern "C" {
#endif
error_t event_group_set(EventGroupHandle_t eventGroup, uint32_t inFlags) {
if (xPortInIsrContext() == pdTRUE) {
BaseType_t yield = pdFALSE;
if (xEventGroupSetBitsFromISR(eventGroup, inFlags, &yield) == pdFAIL) {
return ERROR_RESOURCE;
}
portYIELD_FROM_ISR(yield);
} else {
xEventGroupSetBits(eventGroup, inFlags);
}
return ERROR_NONE;
}
error_t event_group_clear(EventGroupHandle_t eventGroup, uint32_t flags) {
if (xPortInIsrContext() == pdTRUE) {
if (xEventGroupClearBitsFromISR(eventGroup, flags) == pdFAIL) {
return ERROR_RESOURCE;
}
portYIELD_FROM_ISR(pdTRUE);
} else {
xEventGroupClearBits(eventGroup, flags);
}
return ERROR_NONE;
}
uint32_t event_group_get(EventGroupHandle_t eventGroup) {
if (xPortInIsrContext() == pdTRUE) {
return xEventGroupGetBitsFromISR(eventGroup);
} else {
return xEventGroupGetBits(eventGroup);
}
}
error_t event_group_wait(
EventGroupHandle_t eventGroup,
uint32_t inFlags,
bool awaitAll,
bool clearOnExit,
uint32_t* outFlags,
TickType_t timeout
) {
if (xPortInIsrContext() == pdTRUE) {
return ERROR_ISR_STATUS;
}
uint32_t result_flags = xEventGroupWaitBits(
eventGroup,
inFlags,
clearOnExit ? pdTRUE : pdFALSE,
awaitAll ? pdTRUE : pdFALSE,
timeout
);
auto invalid_flags = awaitAll
? ((inFlags & result_flags) != inFlags) // await all
: ((inFlags & result_flags) == 0U); // await any
if (invalid_flags) {
const uint32_t matched = inFlags & result_flags;
if (matched == 0U) {
return ERROR_TIMEOUT;
}
return ERROR_RESOURCE;
}
if (outFlags != nullptr) {
*outFlags = result_flags;
}
return ERROR_NONE;
}
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,278 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/concurrent/thread.h>
#include <tactility/concurrent/mutex.h>
#include <tactility/check.h>
#include <tactility/delay.h>
#include <tactility/log.h>
#include <tactility/time.h>
#include <cstdlib>
#include <cstring>
#include <string>
static const size_t LOCAL_STORAGE_SELF_POINTER_INDEX = 0;
static const char* TAG = "Thread";
struct Thread {
TaskHandle_t taskHandle = nullptr;
ThreadState state = THREAD_STATE_STOPPED;
thread_main_fn_t mainFunction = nullptr;
void* mainFunctionContext = nullptr;
int32_t callbackResult = 0;
thread_state_callback_t stateCallback = nullptr;
void* stateCallbackContext = nullptr;
std::string name = "unnamed";
enum ThreadPriority priority = THREAD_PRIORITY_NORMAL;
struct Mutex mutex = { 0 };
configSTACK_DEPTH_TYPE stackSize = 4096;
portBASE_TYPE affinity = -1;
Thread() {
mutex_construct(&mutex);
}
~Thread() {
mutex_destruct(&mutex);
}
void lock() { mutex_lock(&mutex); }
void unlock() { mutex_unlock(&mutex); }
};
static void thread_set_state_internal(Thread* thread, ThreadState newState) {
thread->lock();
thread->state = newState;
auto cb = thread->stateCallback;
auto cb_ctx = thread->stateCallbackContext;
thread->unlock();
if (cb) {
cb(newState, cb_ctx);
}
}
static void thread_main_body(void* context) {
check(context != nullptr);
auto* thread = static_cast<Thread*>(context);
// Save Thread instance pointer to task local storage
check(pvTaskGetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX) == nullptr);
vTaskSetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX, thread);
LOG_I(TAG, "Starting %s", thread->name.c_str()); // No need to lock as we don't allow mutation after thread start
check(thread->state == THREAD_STATE_STARTING);
thread_set_state_internal(thread, THREAD_STATE_RUNNING);
int32_t result = thread->mainFunction(thread->mainFunctionContext);
thread->lock();
thread->callbackResult = result;
thread->unlock();
check(thread->state == THREAD_STATE_RUNNING);
thread_set_state_internal(thread, THREAD_STATE_STOPPED);
LOG_I(TAG, "Stopped %s", thread->name.c_str()); // No need to lock as we don't allow mutation after thread start
vTaskSetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX, nullptr);
thread->lock();
thread->taskHandle = nullptr;
thread->unlock();
vTaskDelete(nullptr);
}
extern "C" {
Thread* thread_alloc(void) {
auto* thread = new(std::nothrow) Thread();
if (thread == nullptr) {
return nullptr;
}
return thread;
}
Thread* thread_alloc_full(
const char* name,
configSTACK_DEPTH_TYPE stack_size,
thread_main_fn_t function,
void* function_context,
portBASE_TYPE affinity
) {
auto* thread = new(std::nothrow) Thread();
if (thread != nullptr) {
thread_set_name(thread, name);
thread_set_stack_size(thread, stack_size);
thread_set_main_function(thread, function, function_context);
thread_set_affinity(thread, affinity);
}
return thread;
}
void thread_free(Thread* thread) {
check(thread);
check(thread->state == THREAD_STATE_STOPPED);
check(thread->taskHandle == nullptr);
delete thread;
}
void thread_set_name(Thread* thread, const char* name) {
check(name != nullptr);
thread->lock();
check(thread->state == THREAD_STATE_STOPPED);
thread->name = name;
thread->unlock();
}
void thread_set_stack_size(Thread* thread, size_t stack_size) {
thread->lock();
check(stack_size > 0);
check(thread->state == THREAD_STATE_STOPPED);
thread->stackSize = stack_size;
thread->unlock();
}
void thread_set_affinity(Thread* thread, portBASE_TYPE affinity) {
thread->lock();
check(thread->state == THREAD_STATE_STOPPED);
thread->affinity = affinity;
thread->unlock();
}
void thread_set_main_function(Thread* thread, thread_main_fn_t function, void* context) {
thread->lock();
check(function != nullptr);
check(thread->state == THREAD_STATE_STOPPED);
thread->mainFunction = function;
thread->mainFunctionContext = context;
thread->unlock();
}
void thread_set_priority(Thread* thread, enum ThreadPriority priority) {
thread->lock();
check(thread->state == THREAD_STATE_STOPPED);
thread->priority = priority;
thread->unlock();
}
void thread_set_state_callback(Thread* thread, thread_state_callback_t callback, void* context) {
thread->lock();
check(callback != nullptr);
check(thread->state == THREAD_STATE_STOPPED);
thread->stateCallback = callback;
thread->stateCallbackContext = context;
thread->unlock();
}
ThreadState thread_get_state(Thread* thread) {
check(xPortInIsrContext() == pdFALSE);
thread->lock();
ThreadState state = thread->state;
thread->unlock();
return state;
}
error_t thread_start(Thread* thread) {
thread->lock();
check(thread->mainFunction != nullptr);
check(thread->state == THREAD_STATE_STOPPED);
check(thread->stackSize);
thread->unlock();
thread_set_state_internal(thread, THREAD_STATE_STARTING);
thread->lock();
uint32_t stack_depth = thread->stackSize / sizeof(StackType_t);
enum ThreadPriority priority = thread->priority;
portBASE_TYPE affinity = thread->affinity;
thread->unlock();
BaseType_t result;
if (affinity != -1) {
#ifdef ESP_PLATFORM
result = xTaskCreatePinnedToCore(
thread_main_body,
thread->name.c_str(),
stack_depth,
thread,
(UBaseType_t)priority,
&thread->taskHandle,
affinity
);
#else
result = xTaskCreate(
thread_main_body,
thread->name.c_str(),
stack_depth,
thread,
(UBaseType_t)priority,
&thread->taskHandle
);
#endif
} else {
result = xTaskCreate(
thread_main_body,
thread->name.c_str(),
stack_depth,
thread,
(UBaseType_t)priority,
&thread->taskHandle
);
}
if (result != pdPASS) {
thread_set_state_internal(thread, THREAD_STATE_STOPPED);
thread->lock();
thread->taskHandle = nullptr;
thread->unlock();
return ERROR_UNDEFINED;
}
return ERROR_NONE;
}
error_t thread_join(Thread* thread, TickType_t timeout, TickType_t poll_interval) {
check(thread_get_current() != thread);
TickType_t start_ticks = get_ticks();
while (thread_get_task_handle(thread)) {
delay_ticks(poll_interval);
if (get_ticks() - start_ticks > timeout) {
return ERROR_TIMEOUT;
}
}
return ERROR_NONE;
}
TaskHandle_t thread_get_task_handle(Thread* thread) {
thread->lock();
auto* handle = thread->taskHandle;
thread->unlock();
return handle;
}
int32_t thread_get_return_code(Thread* thread) {
thread->lock();
check(thread->state == THREAD_STATE_STOPPED);
auto result = thread->callbackResult;
thread->unlock();
return result;
}
uint32_t thread_get_stack_space(Thread* thread) {
if (xPortInIsrContext() == pdTRUE) {
return 0;
}
thread->lock();
check(thread->state == THREAD_STATE_RUNNING);
auto result = uxTaskGetStackHighWaterMark(thread->taskHandle) * sizeof(StackType_t);
thread->unlock();
return result;
}
Thread* thread_get_current(void) {
check(xPortInIsrContext() == pdFALSE);
return (Thread*)pvTaskGetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX);
}
}
+110
View File
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/concurrent/timer.h>
#include <tactility/check.h>
#include <tactility/freertos/timers.h>
#include <stdlib.h>
struct Timer {
TimerHandle_t handle;
timer_callback_t callback;
void* context;
};
static void timer_callback_internal(TimerHandle_t xTimer) {
struct Timer* timer = (struct Timer*)pvTimerGetTimerID(xTimer);
if (timer != NULL && timer->callback != NULL) {
timer->callback(timer->context);
}
}
struct Timer* timer_alloc(enum TimerType type, TickType_t ticks, timer_callback_t callback, void* context) {
check(xPortInIsrContext() == pdFALSE);
check(callback != NULL);
struct Timer* timer = (struct Timer*)malloc(sizeof(struct Timer));
if (timer == NULL) {
return NULL;
}
timer->callback = callback;
timer->context = context;
BaseType_t auto_reload = (type == TIMER_TYPE_ONCE) ? pdFALSE : pdTRUE;
timer->handle = xTimerCreate(NULL, ticks, auto_reload, timer, timer_callback_internal);
if (timer->handle == NULL) {
free(timer);
return NULL;
}
return timer;
}
void timer_free(struct Timer* timer) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
// MAX_TICKS or a reasonable timeout for the timer command queue
xTimerDelete(timer->handle, portMAX_DELAY);
free(timer);
}
error_t timer_start(struct Timer* timer) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
return (xTimerStart(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
}
error_t timer_stop(struct Timer* timer) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
return (xTimerStop(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
}
error_t timer_reset_with_interval(struct Timer* timer, TickType_t interval) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
if (xTimerChangePeriod(timer->handle, interval, portMAX_DELAY) != pdPASS) {
return ERROR_TIMEOUT;
}
return (xTimerReset(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
}
error_t timer_reset(struct Timer* timer) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
return (xTimerReset(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
}
bool timer_is_running(struct Timer* timer) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
return xTimerIsTimerActive(timer->handle) != pdFALSE;
}
TickType_t timer_get_expiry_time(struct Timer* timer) {
check(xPortInIsrContext() == pdFALSE);
check(timer != NULL);
return xTimerGetExpiryTime(timer->handle);
}
error_t timer_set_pending_callback(struct Timer* timer, timer_pending_callback_t callback, void* context, uint32_t arg, TickType_t timeout) {
(void)timer; // Unused in this implementation but kept for API consistency if needed later
BaseType_t result;
if (xPortInIsrContext() == pdTRUE) {
check(timeout == 0);
result = xTimerPendFunctionCallFromISR(callback, context, arg, NULL);
} else {
result = xTimerPendFunctionCall(callback, context, arg, timeout);
}
return (result == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
}
void timer_set_callback_priority(struct Timer* timer, enum ThreadPriority priority) {
(void)timer; // Unused in this implementation but kept for API consistency if needed later
check(xPortInIsrContext() == pdFALSE);
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();
check(task_handle != NULL);
vTaskPrioritySet(task_handle, (UBaseType_t)priority);
}