C++ refactoring (#91)

This commit is contained in:
Ken Van Hoeylandt
2024-11-26 17:51:05 +01:00
committed by GitHub
parent d06137ba76
commit ca5d4b8226
159 changed files with 904 additions and 1086 deletions
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include "MessageQueue.h"
#include "Mutex.h"
#include "Pubsub.h"
#include "service/gui/Gui.h"
#include "service/gui/ViewPort.h"
#include "service/gui/ViewPort_i.h"
#include <cstdio>
namespace tt::service::gui {
#define GUI_THREAD_FLAG_DRAW (1 << 0)
#define GUI_THREAD_FLAG_INPUT (1 << 1)
#define GUI_THREAD_FLAG_EXIT (1 << 2)
#define GUI_THREAD_FLAG_ALL (GUI_THREAD_FLAG_DRAW | GUI_THREAD_FLAG_INPUT | GUI_THREAD_FLAG_EXIT)
/** Gui structure */
struct Gui {
// Thread and lock
Thread* thread;
Mutex* mutex;
PubSubSubscription* loader_pubsub_subscription;
// Layers and Canvas
lv_obj_t* lvgl_parent;
// App-specific
ViewPort* app_view_port;
lv_obj_t* _Nullable keyboard;
lv_group_t* keyboard_group;
};
/** Update GUI, request redraw */
void request_draw();
/** Lock GUI */
void lock();
/** Unlock GUI */
void unlock();
} // namespace
@@ -0,0 +1,23 @@
#pragma once
#include "service/gui/ViewPort.h"
namespace tt::service::gui {
/** Process draw call. Calls on_show callback.
* To be used by GUI, called on redraw.
*
* @param view_port ViewPort instance
* @param canvas canvas to draw at
*/
void view_port_show(ViewPort* view_port, lv_obj_t* parent);
/**
* Process draw clearing call. Calls on_hdie callback.
* To be used by GUI, called on redraw.
*
* @param view_port
*/
void view_port_hide(ViewPort* view_port);
} // namespace
+115
View File
@@ -0,0 +1,115 @@
#pragma once
#include "app/Manifest.h"
#include "MessageQueue.h"
#include "Pubsub.h"
#include "Thread.h"
#include "service/gui/ViewPort.h"
#include "service/loader/Loader.h"
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#else
#include "FreeRTOS.h"
#include "semphr.h"
#endif
namespace tt::service::loader {
#define APP_STACK_SIZE 32
typedef enum {
LoaderMessageTypeNone,
LoaderMessageTypeAppStart,
LoaderMessageTypeAppStop,
LoaderMessageTypeServiceStop,
} LoaderMessageType;
class LoaderMessageAppStart {
public:
std::string id;
Bundle bundle;
LoaderMessageAppStart() = default;
LoaderMessageAppStart(const std::string& id, const Bundle& bundle) {
this->id = id;
this->bundle = bundle;
}
};
typedef struct {
LoaderStatus value;
} LoaderMessageLoaderStatusResult;
typedef struct {
bool value;
} LoaderMessageBoolResult;
class LoaderMessage {
public:
// This lock blocks anyone from starting an app as long
// as an app is already running via loader_start()
// This lock's lifecycle is not owned by this class.
EventFlag* _Nullable api_lock;
LoaderMessageType type;
struct {
union {
// TODO: Convert to smart pointer
const LoaderMessageAppStart* start;
};
} payload;
struct {
union {
LoaderMessageLoaderStatusResult status_value;
LoaderMessageBoolResult bool_value;
void* raw_value;
};
} result;
LoaderMessage() {
api_lock = nullptr;
type = LoaderMessageTypeNone;
payload = { .start = nullptr };
result = { .raw_value = nullptr };
}
LoaderMessage(const LoaderMessageAppStart* start, const LoaderMessageLoaderStatusResult& statusResult) {
api_lock = nullptr;
type = LoaderMessageTypeAppStart;
payload.start = start;
result.status_value = statusResult;
}
LoaderMessage(LoaderMessageType messageType) {
api_lock = nullptr;
type = messageType;
payload = { .start = nullptr };
result = { .raw_value = nullptr };
}
void setApiLock(EventFlag* eventFlag) {
api_lock = eventFlag;
}
void cleanup() {
if (type == LoaderMessageTypeAppStart) {
delete payload.start;
}
}
};
struct Loader {
Thread* thread;
PubSub* pubsub_internal;
PubSub* pubsub_external;
MessageQueue queue = MessageQueue(1, sizeof(LoaderMessage));
Mutex* mutex;
int8_t app_stack_index;
app::App app_stack[APP_STACK_SIZE];
};
} // namespace