Merge develop into main branch (#137)

* SdCard HAL refactored (#135)

- Refactor SdCard HAL
- introduce Lockable

* Screenshot and FatFS improvements (#136)

- Fix screenshots on ESP32
- Improve Screenshot service
- Convert Screenshot app to class-based instead of structs
- Screenshot app now automatically updates when task is finished
- Enable FatFS long filename support

* Re-use common log messages (#138)

For consistency and binary size reduction

* Toolbar spinner should get margin to the right

* More TactilityC features (#139)

* Rewrote Loader

- Simplified Loader by removing custom threa
- Created DispatcherThread
- Move auto-starting apps to Boot app
- Fixed Dispatcher bug where it could get stuck not processing new
messages

* Hide AP settings if the AP is not saved

* Missing from previous commit

* Replace LV_EVENT_CLICKED with LV_EVENT_SHORT_CLICKED

* Refactored files app and created InputDialog (#140)

- Changed Files app so that it has a View and State
- Files app now allows for long-pressing on files to perform actions
- Files app now has rename and delete actions
- Created InputDialog app
- Improved AlertDialog app layout
This commit is contained in:
Ken Van Hoeylandt
2024-12-27 22:12:39 +00:00
committed by GitHub
parent 9033daa6dd
commit 50bd6e8bf6
144 changed files with 3244 additions and 2038 deletions
@@ -1,9 +1,10 @@
#include "TactilityConfig.h"
#if TT_FEATURE_SCREENSHOT_ENABLED
#include "Screenshot.h"
#include <cstdlib>
#include <memory>
#include "Mutex.h"
#include "ScreenshotTask.h"
#include "service/ServiceContext.h"
#include "service/ServiceRegistry.h"
@@ -13,105 +14,84 @@ namespace tt::service::screenshot {
extern const ServiceManifest manifest;
struct ServiceData {
Mutex mutex;
task::ScreenshotTask* task = nullptr;
Mode mode = ScreenshotModeNone;
~ServiceData() {
if (task) {
task::free(task);
}
std::shared_ptr<ScreenshotService> _Nullable optScreenshotService() {
ServiceContext* context = service::findServiceById(manifest.id);
if (context != nullptr) {
return std::static_pointer_cast<ScreenshotService>(context->getData());
} else {
return nullptr;
}
}
void lock() {
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
}
void unlock() {
tt_check(mutex.release() == TtStatusOk);
}
};
void startApps(const char* path) {
_Nullable auto* service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
void ScreenshotService::startApps(const char* path) {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
if (data->task == nullptr) {
data->task = task::alloc();
data->mode = ScreenshotModeApps;
task::startApps(data->task, path);
if (task == nullptr || task->isFinished()) {
task = std::make_unique<ScreenshotTask>();
mode = ScreenshotModeApps;
task->startApps(path);
} else {
TT_LOG_E(TAG, "Screenshot task already running");
TT_LOG_W(TAG, "Screenshot task already running");
}
data->unlock();
}
void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
_Nullable auto* service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
void ScreenshotService::startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
if (data->task == nullptr) {
data->task = task::alloc();
data->mode = ScreenshotModeTimed;
task::startTimed(data->task, path, delay_in_seconds, amount);
if (task == nullptr || task->isFinished()) {
task = std::make_unique<ScreenshotTask>();
mode = ScreenshotModeTimed;
task->startTimed(path, delay_in_seconds, amount);
} else {
TT_LOG_E(TAG, "Screenshot task already running");
TT_LOG_W(TAG, "Screenshot task already running");
}
data->unlock();
}
void stop() {
_Nullable ServiceContext* service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
void ScreenshotService::stop() {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
if (data->task != nullptr) {
task::stop(data->task);
task::free(data->task);
data->task = nullptr;
data->mode = ScreenshotModeNone;
if (task != nullptr) {
task = nullptr;
mode = ScreenshotModeNone;
} else {
TT_LOG_E(TAG, "Screenshot task not running");
TT_LOG_W(TAG, "Screenshot task not running");
}
data->unlock();
}
Mode getMode() {
_Nullable auto* service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
Mode ScreenshotService::getMode() {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return ScreenshotModeNone;
}
return mode;
}
bool ScreenshotService::isTaskStarted() {
auto* current_task = task.get();
if (current_task == nullptr) {
return false;
} else {
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
Mode mode = data->mode;
data->unlock();
return mode;
return !current_task->isFinished();
}
}
bool isStarted() {
return getMode() != ScreenshotModeNone;
}
static void onStart(ServiceContext& service) {
auto data = std::make_shared<ServiceData>();
service.setData(data);
static void onStart(ServiceContext& serviceContext) {
auto service = std::make_shared<ScreenshotService>();
serviceContext.setData(service);
}
extern const ServiceManifest manifest = {
@@ -120,3 +100,5 @@ extern const ServiceManifest manifest = {
};
} // namespace
#endif
@@ -1,5 +1,11 @@
#pragma once
#include "Mutex.h"
#include "ScreenshotTask.h"
#include "TactilityConfig.h"
#if TT_FEATURE_SCREENSHOT_ENABLED
#include <cstdint>
namespace tt::service::screenshot {
@@ -10,22 +16,23 @@ typedef enum {
ScreenshotModeApps
} Mode;
/** @brief Starts taking screenshot with a timer
* @param path the path to store the screenshots in
* @param delay_in_seconds the delay before starting (and between successive screenshots)
* @param amount 0 = indefinite, >0 for a specific
*/
void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount);
/** @brief Starts taking screenshot when an app is started
* @param path the path to store the screenshots in
*/
void startApps(const char* path);
class ScreenshotService {
Mutex mutex;
std::unique_ptr<ScreenshotTask> task;
Mode mode = ScreenshotModeNone;
void stop();
public:
Mode getMode();
bool isTaskStarted();
Mode getMode();
void startApps(const char* path);
void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount);
void stop();
};
bool isStarted();
std::shared_ptr<ScreenshotService> _Nullable optScreenshotService();
} // namespace
#endif
@@ -1,184 +1,184 @@
#include "TactilityConfig.h"
#if TT_FEATURE_SCREENSHOT_ENABLED
#include <cstring>
#include "ScreenshotTask.h"
#include "lv_screenshot.h"
#include "app/AppContext.h"
#include "Mutex.h"
#include "TactilityCore.h"
#include "Thread.h"
#include "service/loader/Loader.h"
#include "lvgl/LvglSync.h"
namespace tt::service::screenshot::task {
namespace tt::service::screenshot {
#define TAG "screenshot_task"
#define TASK_WORK_TYPE_DELAY 1
#define TASK_WORK_TYPE_APPS 2
#define SCREENSHOT_PATH_LIMIT 128
struct ScreenshotTaskWork {
int type = TASK_WORK_TYPE_DELAY ;
uint8_t delay_in_seconds = 0;
uint8_t amount = 0;
char path[SCREENSHOT_PATH_LIMIT] = { 0 };
};
struct ScreenshotTaskData {
Thread* thread = nullptr;
Mutex mutex = Mutex(Mutex::TypeRecursive);
bool interrupted = false;
ScreenshotTaskWork work;
};
static void task_lock(ScreenshotTaskData* data) {
tt_check(data->mutex.acquire(TtWaitForever) == TtStatusOk);
}
static void task_unlock(ScreenshotTaskData* data) {
tt_check(data->mutex.release() == TtStatusOk);
}
ScreenshotTask* alloc() {
return new ScreenshotTaskData();
}
void free(ScreenshotTask* task) {
auto* data = static_cast<ScreenshotTaskData*>(task);
if (data->thread) {
stop(data);
ScreenshotTask::~ScreenshotTask() {
if (thread) {
stop();
}
delete data;
}
static bool is_interrupted(ScreenshotTaskData* data) {
task_lock(data);
bool interrupted = data->interrupted;
task_unlock(data);
bool ScreenshotTask::isInterrupted() {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return true;
}
return interrupted;
}
static int32_t screenshot_task(void* context) {
auto* data = static_cast<ScreenshotTaskData*>(context);
bool ScreenshotTask::isFinished() {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return false;
}
return finished;
}
bool interrupted = false;
void ScreenshotTask::setFinished() {
auto scoped_lockable = mutex.scoped();
scoped_lockable->lock(TtWaitForever);
finished = true;
}
static void makeScreenshot(const char* filename) {
if (lvgl::lock(50 / portTICK_PERIOD_MS)) {
if (lv_screenshot_create(lv_scr_act(), LV_100ASK_SCREENSHOT_SV_PNG, filename)) {
TT_LOG_I(TAG, "Screenshot saved to %s", filename);
} else {
TT_LOG_E(TAG, "Screenshot not saved to %s", filename);
}
lvgl::unlock();
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
}
}
static int32_t screenshotTaskCallback(void* context) {
auto* data = static_cast<ScreenshotTask*>(context);
assert(data != nullptr);
data->taskMain();
return 0;
}
void ScreenshotTask::taskMain() {
uint8_t screenshots_taken = 0;
std::string last_app_id;
while (!interrupted) {
interrupted = is_interrupted(data);
if (data->work.type == TASK_WORK_TYPE_DELAY) {
while (!isInterrupted()) {
if (work.type == TASK_WORK_TYPE_DELAY) {
// Splitting up the delays makes it easier to stop the service
for (int i = 0; i < (data->work.delay_in_seconds * 10) && !is_interrupted(data); ++i){
for (int i = 0; i < (work.delay_in_seconds * 10) && !isInterrupted(); ++i){
kernel::delayMillis(100);
}
if (is_interrupted(data)) {
break;
}
if (!isInterrupted()) {
screenshots_taken++;
char filename[SCREENSHOT_PATH_LIMIT + 32];
sprintf(filename, "%s/screenshot-%d.png", work.path, screenshots_taken);
makeScreenshot(filename);
screenshots_taken++;
char filename[SCREENSHOT_PATH_LIMIT + 32];
sprintf(filename, "%s/screenshot-%d.png", data->work.path, screenshots_taken);
lvgl::lock(TtWaitForever);
if (lv_screenshot_create(lv_scr_act(), LV_COLOR_FORMAT_NATIVE, LV_100ASK_SCREENSHOT_SV_PNG, filename)){
TT_LOG_I(TAG, "Screenshot saved to %s", filename);
} else {
TT_LOG_E(TAG, "Screenshot not saved to %s", filename);
if (work.amount > 0 && screenshots_taken >= work.amount) {
break; // Interrupted loop
}
}
lvgl::unlock();
if (data->work.amount > 0 && screenshots_taken >= data->work.amount) {
break; // Interrupted loop
}
} else if (data->work.type == TASK_WORK_TYPE_APPS) {
} else if (work.type == TASK_WORK_TYPE_APPS) {
app::AppContext* _Nullable app = loader::getCurrentApp();
if (app) {
const app::AppManifest& manifest = app->getManifest();
if (manifest.id != last_app_id) {
kernel::delayMillis(100);
last_app_id = manifest.id;
char filename[SCREENSHOT_PATH_LIMIT + 32];
sprintf(filename, "%s/screenshot-%s.png", data->work.path, manifest.id.c_str());
lvgl::lock(TtWaitForever);
if (lv_screenshot_create(lv_scr_act(), LV_COLOR_FORMAT_NATIVE, LV_100ASK_SCREENSHOT_SV_PNG, filename)){
TT_LOG_I(TAG, "Screenshot saved to %s", filename);
} else {
TT_LOG_E(TAG, "Screenshot not saved to %s", filename);
}
lvgl::unlock();
sprintf(filename, "%s/screenshot-%s.png", work.path, manifest.id.c_str());
makeScreenshot(filename);
}
}
// Ensure the LVGL widgets are rendered as the app just started
kernel::delayMillis(250);
}
}
return 0;
setFinished();
}
static void task_start(ScreenshotTaskData* data) {
task_lock(data);
tt_check(data->thread == nullptr);
data->thread = new Thread(
void ScreenshotTask::taskStart() {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
tt_check(thread == nullptr);
thread = new Thread(
"screenshot",
8192,
&screenshot_task,
data
&screenshotTaskCallback,
this
);
data->thread->start();
task_unlock(data);
thread->start();
}
void startApps(ScreenshotTask* task, const char* path) {
void ScreenshotTask::startApps(const char* path) {
tt_check(strlen(path) < (SCREENSHOT_PATH_LIMIT - 1));
auto* data = static_cast<ScreenshotTaskData*>(task);
task_lock(data);
if (data->thread == nullptr) {
data->interrupted = false;
data->work.type = TASK_WORK_TYPE_APPS;
strcpy(data->work.path, path);
task_start(data);
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
if (thread == nullptr) {
interrupted = false;
work.type = TASK_WORK_TYPE_APPS;
strcpy(work.path, path);
taskStart();
} else {
TT_LOG_E(TAG, "Task was already running");
}
task_unlock(data);
}
void startTimed(ScreenshotTask* task, const char* path, uint8_t delay_in_seconds, uint8_t amount) {
void ScreenshotTask::startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
tt_check(strlen(path) < (SCREENSHOT_PATH_LIMIT - 1));
auto* data = static_cast<ScreenshotTaskData*>(task);
task_lock(data);
if (data->thread == nullptr) {
data->interrupted = false;
data->work.type = TASK_WORK_TYPE_DELAY;
data->work.delay_in_seconds = delay_in_seconds;
data->work.amount = amount;
strcpy(data->work.path, path);
task_start(data);
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
if (thread == nullptr) {
interrupted = false;
work.type = TASK_WORK_TYPE_DELAY;
work.delay_in_seconds = delay_in_seconds;
work.amount = amount;
strcpy(work.path, path);
taskStart();
} else {
TT_LOG_E(TAG, "Task was already running");
}
task_unlock(data);
}
void stop(ScreenshotTask* task) {
auto* data = static_cast<ScreenshotTaskData*>(task);
if (data->thread != nullptr) {
task_lock(data);
data->interrupted = true;
task_unlock(data);
void ScreenshotTask::stop() {
if (thread != nullptr) {
if (mutex.lock(50 / portTICK_PERIOD_MS)) {
interrupted = true;
tt_check(mutex.unlock());
}
data->thread->join();
thread->join();
task_lock(data);
delete data->thread;
data->thread = nullptr;
task_unlock(data);
if (mutex.lock(50 / portTICK_PERIOD_MS)) {
delete thread;
thread = nullptr;
tt_check(mutex.unlock());
}
}
}
} // namespace
#endif
@@ -1,32 +1,69 @@
#include "TactilityConfig.h"
#if TT_FEATURE_SCREENSHOT_ENABLED
#pragma once
#include <cstdint>
#include <Thread.h>
#include <Mutex.h>
namespace tt::service::screenshot::task {
namespace tt::service::screenshot {
typedef void ScreenshotTask;
#define TASK_WORK_TYPE_DELAY 1
#define TASK_WORK_TYPE_APPS 2
ScreenshotTask* alloc();
#define SCREENSHOT_PATH_LIMIT 128
void free(ScreenshotTask* task);
class ScreenshotTask {
/** @brief Start taking screenshots after a certain delay
* @param task the screenshot task
* @param path the path to store the screenshots at
* @param delay_in_seconds the delay before starting (and between successive screenshots)
* @param amount 0 = indefinite, >0 for a specific
*/
void startTimed(ScreenshotTask* task, const char* path, uint8_t delay_in_seconds, uint8_t amount);
struct ScreenshotTaskWork {
int type = TASK_WORK_TYPE_DELAY ;
uint8_t delay_in_seconds = 0;
uint8_t amount = 0;
char path[SCREENSHOT_PATH_LIMIT] = { 0 };
};
/** @brief Start taking screenshot whenever an app is started
* @param task the screenshot task
* @param path the path to store the screenshots at
*/
void startApps(ScreenshotTask* task, const char* path);
Thread* thread = nullptr;
Mutex mutex = Mutex(Mutex::TypeRecursive);
bool interrupted = false;
bool finished = false;
ScreenshotTaskWork work;
/** @brief Stop taking screenshots
* @param task the screenshot task
*/
void stop(ScreenshotTask* task);
public:
ScreenshotTask() = default;
~ScreenshotTask();
/** @brief Start taking screenshots after a certain delay
* @param task the screenshot task
* @param path the path to store the screenshots at
* @param delay_in_seconds the delay before starting (and between successive screenshots)
* @param amount 0 = indefinite, >0 for a specific
*/
void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount);
/** @brief Start taking screenshot whenever an app is started
* @param task the screenshot task
* @param path the path to store the screenshots at
*/
void startApps(const char* path);
/** @brief Stop taking screenshots
* @param task the screenshot task
*/
void stop();
void taskMain();
bool isFinished();
private:
bool isInterrupted();
void setFinished();
void taskStart();
};
}
#endif