Ken Van Hoeylandt 27730260e0
Project restructuring: add tactility-headless (#55)
- Created `tactility-headless` to support ESP32 firmwares that don't require graphics
- `tactility` subproject now contains both PC and ESP32 code (to avoid having to split up `tactility` and `tactility-headless` into separate projects, which would result in a very complex dependency tree)
- `tactility` subproject is now defined as component for ESP32 and as regular module for PC
- Improvements for dispatcher
- Added `project-structure.puml` to docs
- `Gui` service now depends on `Loader` service instead of the reverse
- Added `statusbar_updater` service for updating Wi-Fi and SD card icons
2024-08-31 17:56:28 +02:00

63 lines
1.9 KiB
C

#include "dispatcher.h"
#include "tactility_core.h"
typedef struct {
Callback callback;
void* context;
} DispatcherMessage;
typedef struct {
MessageQueue* queue;
Mutex* mutex;
DispatcherMessage buffer; // Buffer for consuming a message
} DispatcherData;
Dispatcher* tt_dispatcher_alloc(uint32_t message_count) {
DispatcherData* data = malloc(sizeof(DispatcherData));
*data = (DispatcherData) {
.queue = tt_message_queue_alloc(message_count, sizeof(DispatcherMessage)),
.mutex = tt_mutex_alloc(MutexTypeNormal),
.buffer = {
.callback = NULL,
.context = NULL
}
};
return data;
}
void tt_dispatcher_free(Dispatcher* dispatcher) {
DispatcherData* data = (DispatcherData*)dispatcher;
tt_mutex_acquire(data->mutex, TtWaitForever);
tt_message_queue_reset(data->queue);
tt_message_queue_free(data->queue);
tt_mutex_release(data->mutex);
tt_mutex_free(data->mutex);
free(data);
}
void tt_dispatcher_dispatch(Dispatcher* dispatcher, Callback callback, void* context) {
DispatcherData* data = (DispatcherData*)dispatcher;
DispatcherMessage message = {
.callback = callback,
.context = context
};
tt_mutex_acquire(data->mutex, TtWaitForever);
tt_message_queue_put(data->queue, &message, TtWaitForever);
tt_mutex_release(data->mutex);
}
bool tt_dispatcher_consume(Dispatcher* dispatcher, uint32_t timeout_ticks) {
DispatcherData* data = (DispatcherData*)dispatcher;
tt_mutex_acquire(data->mutex, TtWaitForever);
if (tt_message_queue_get(data->queue, &(data->buffer), timeout_ticks) == TtStatusOk) {
DispatcherMessage* message = &(data->buffer);
message->callback(message->context);
tt_mutex_release(data->mutex);
return true;
} else {
tt_mutex_release(data->mutex);
return false;
}
}