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:
committed by
GitHub
parent
9033daa6dd
commit
50bd6e8bf6
@@ -59,7 +59,7 @@ void redraw(Gui* gui) {
|
||||
// Unlock GUI and LVGL
|
||||
lvgl::unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to lock lvgl");
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
|
||||
}
|
||||
|
||||
unlock();
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include "Tactility.h"
|
||||
#include <Mutex.h>
|
||||
#include "app/AppManifest.h"
|
||||
#include "app/ManifestRegistry.h"
|
||||
#include "service/ServiceManifest.h"
|
||||
@@ -9,95 +7,72 @@
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "esp_heap_caps.h"
|
||||
#include "TactilityHeadless.h"
|
||||
|
||||
#else
|
||||
#include "lvgl/LvglSync.h"
|
||||
#include "TactilityHeadless.h"
|
||||
#endif
|
||||
|
||||
namespace tt::service::loader {
|
||||
|
||||
#define TAG "loader"
|
||||
#define LOADER_EVENT_FLAG 1
|
||||
|
||||
typedef struct {
|
||||
LoaderEventType type;
|
||||
} LoaderEventInternal;
|
||||
|
||||
// Forward declarations
|
||||
static int32_t loader_main(void* p);
|
||||
static void onStartAppMessage(std::shared_ptr<void> message);
|
||||
static void onStopAppMessage(TT_UNUSED std::shared_ptr<void> message);
|
||||
static void stopAppInternal();
|
||||
static LoaderStatus startAppInternal(const std::string& id, std::shared_ptr<const Bundle> _Nullable parameters);
|
||||
|
||||
static Loader* loader_singleton = nullptr;
|
||||
|
||||
static Loader* loader_alloc() {
|
||||
assert(loader_singleton == nullptr);
|
||||
loader_singleton = new Loader();
|
||||
loader_singleton->thread = new Thread(
|
||||
"loader",
|
||||
4096, // Last known minimum was 2400 for starting Hello World app
|
||||
&loader_main,
|
||||
nullptr
|
||||
);
|
||||
return loader_singleton;
|
||||
}
|
||||
|
||||
static void loader_free() {
|
||||
tt_assert(loader_singleton != nullptr);
|
||||
delete loader_singleton->thread;
|
||||
delete loader_singleton;
|
||||
loader_singleton = nullptr;
|
||||
}
|
||||
|
||||
static void loader_lock() {
|
||||
tt_assert(loader_singleton);
|
||||
tt_check(loader_singleton->mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void loader_unlock() {
|
||||
tt_assert(loader_singleton);
|
||||
tt_check(loader_singleton->mutex.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
LoaderStatus startApp(const std::string& id, bool blocking, std::shared_ptr<const Bundle> parameters) {
|
||||
void startApp(const std::string& id, bool blocking, std::shared_ptr<const Bundle> parameters) {
|
||||
TT_LOG_I(TAG, "Start app %s", id.c_str());
|
||||
tt_assert(loader_singleton);
|
||||
|
||||
LoaderMessageLoaderStatusResult result = {
|
||||
.value = LoaderStatusOk
|
||||
};
|
||||
auto message = std::make_shared<LoaderMessageAppStart>(id, parameters);
|
||||
loader_singleton->dispatcherThread->dispatch(onStartAppMessage, message);
|
||||
|
||||
auto* start_message = new LoaderMessageAppStart(id, parameters);
|
||||
LoaderMessage message(start_message, result);
|
||||
|
||||
EventFlag* event_flag = blocking ? new EventFlag() : nullptr;
|
||||
if (event_flag != nullptr) {
|
||||
message.setApiLock(event_flag);
|
||||
}
|
||||
|
||||
loader_singleton->queue.put(&message, TtWaitForever);
|
||||
|
||||
if (event_flag != nullptr) {
|
||||
auto event_flag = message->getApiLockEventFlag();
|
||||
if (blocking) {
|
||||
/* TODO: Check if task id is not the LVGL one,
|
||||
because otherwise this fails as the apps starting logic will try to lock lvgl
|
||||
to update the UI and fail. */
|
||||
event_flag->wait(LOADER_EVENT_FLAG);
|
||||
delete event_flag;
|
||||
event_flag->wait(message->getApiLockEventFlagValue());
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
void stopApp() {
|
||||
TT_LOG_I(TAG, "Stop app");
|
||||
tt_check(loader_singleton);
|
||||
LoaderMessage message(LoaderMessageTypeAppStop);
|
||||
loader_singleton->queue.put(&message, TtWaitForever);
|
||||
loader_singleton->dispatcherThread->dispatch(onStopAppMessage, nullptr);
|
||||
}
|
||||
|
||||
app::AppContext* _Nullable getCurrentApp() {
|
||||
tt_assert(loader_singleton);
|
||||
loader_lock();
|
||||
app::AppInstance* app = loader_singleton->app_stack.top();
|
||||
loader_unlock();
|
||||
return dynamic_cast<app::AppContext*>(app);
|
||||
if (loader_singleton->mutex.lock(10 / portTICK_PERIOD_MS)) {
|
||||
app::AppInstance* app = loader_singleton->app_stack.top();
|
||||
loader_singleton->mutex.unlock();
|
||||
return dynamic_cast<app::AppContext*>(app);
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<PubSub> getPubsub() {
|
||||
@@ -108,7 +83,7 @@ std::shared_ptr<PubSub> getPubsub() {
|
||||
return loader_singleton->pubsub_external;
|
||||
}
|
||||
|
||||
static const char* app_state_to_string(app::State state) {
|
||||
static const char* appStateToString(app::State state) {
|
||||
switch (state) {
|
||||
case app::StateInitial:
|
||||
return "initial";
|
||||
@@ -125,16 +100,16 @@ static const char* app_state_to_string(app::State state) {
|
||||
}
|
||||
}
|
||||
|
||||
static void app_transition_to_state(app::AppInstance& app, app::State state) {
|
||||
static void transitionAppToState(app::AppInstance& app, app::State state) {
|
||||
const app::AppManifest& manifest = app.getManifest();
|
||||
const app::State old_state = app.getState();
|
||||
|
||||
TT_LOG_I(
|
||||
TAG,
|
||||
"app \"%s\" state: %s -> %s",
|
||||
"App \"%s\" state: %s -> %s",
|
||||
manifest.id.c_str(),
|
||||
app_state_to_string(old_state),
|
||||
app_state_to_string(state)
|
||||
appStateToString(old_state),
|
||||
appStateToString(state)
|
||||
);
|
||||
|
||||
switch (state) {
|
||||
@@ -179,30 +154,33 @@ static void app_transition_to_state(app::AppInstance& app, app::State state) {
|
||||
}
|
||||
}
|
||||
|
||||
static LoaderStatus loader_do_start_app_with_manifest(
|
||||
static LoaderStatus startAppWithManifestInternal(
|
||||
const app::AppManifest* manifest,
|
||||
std::shared_ptr<const Bundle> _Nullable parameters
|
||||
) {
|
||||
TT_LOG_I(TAG, "start with manifest %s", manifest->id.c_str());
|
||||
tt_check(loader_singleton != nullptr);
|
||||
|
||||
loader_lock();
|
||||
TT_LOG_I(TAG, "Start with manifest %s", manifest->id.c_str());
|
||||
|
||||
auto scoped_lock = loader_singleton->mutex.scoped();
|
||||
if (!scoped_lock->lock(50 / portTICK_PERIOD_MS)) {
|
||||
return LoaderStatusErrorInternal;
|
||||
}
|
||||
|
||||
auto previous_app = !loader_singleton->app_stack.empty() ? loader_singleton->app_stack.top() : nullptr;
|
||||
auto new_app = new app::AppInstance(*manifest, parameters);
|
||||
new_app->mutableFlags().showStatusbar = (manifest->type != app::TypeBoot);
|
||||
|
||||
loader_singleton->app_stack.push(new_app);
|
||||
app_transition_to_state(*new_app, app::StateInitial);
|
||||
app_transition_to_state(*new_app, app::StateStarted);
|
||||
transitionAppToState(*new_app, app::StateInitial);
|
||||
transitionAppToState(*new_app, app::StateStarted);
|
||||
|
||||
// We might have to hide the previous app first
|
||||
if (previous_app != nullptr) {
|
||||
app_transition_to_state(*previous_app, app::StateHiding);
|
||||
transitionAppToState(*previous_app, app::StateHiding);
|
||||
}
|
||||
|
||||
app_transition_to_state(*new_app, app::StateShowing);
|
||||
|
||||
loader_unlock();
|
||||
transitionAppToState(*new_app, app::StateShowing);
|
||||
|
||||
LoaderEventInternal event_internal = {.type = LoaderEventTypeApplicationStarted};
|
||||
tt_pubsub_publish(loader_singleton->pubsub_internal, &event_internal);
|
||||
@@ -218,7 +196,16 @@ static LoaderStatus loader_do_start_app_with_manifest(
|
||||
return LoaderStatusOk;
|
||||
}
|
||||
|
||||
static LoaderStatus do_start_by_id(
|
||||
static void onStartAppMessage(std::shared_ptr<void> message) {
|
||||
auto start_message = std::reinterpret_pointer_cast<LoaderMessageAppStart>(message);
|
||||
startAppInternal(start_message->id, start_message->parameters);
|
||||
}
|
||||
|
||||
static void onStopAppMessage(TT_UNUSED std::shared_ptr<void> message) {
|
||||
stopAppInternal();
|
||||
}
|
||||
|
||||
static LoaderStatus startAppInternal(
|
||||
const std::string& id,
|
||||
std::shared_ptr<const Bundle> _Nullable parameters
|
||||
) {
|
||||
@@ -229,18 +216,21 @@ static LoaderStatus do_start_by_id(
|
||||
TT_LOG_E(TAG, "App not found: %s", id.c_str());
|
||||
return LoaderStatusErrorUnknownApp;
|
||||
} else {
|
||||
return loader_do_start_app_with_manifest(manifest, parameters);
|
||||
return startAppWithManifestInternal(manifest, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
static void stopAppInternal() {
|
||||
tt_check(loader_singleton != nullptr);
|
||||
|
||||
static void do_stop_app() {
|
||||
loader_lock();
|
||||
auto scoped_lock = loader_singleton->mutex.scoped();
|
||||
if (!scoped_lock->lock(50 / portTICK_PERIOD_MS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t original_stack_size = loader_singleton->app_stack.size();
|
||||
|
||||
if (original_stack_size == 0) {
|
||||
loader_unlock();
|
||||
TT_LOG_E(TAG, "Stop app: no app running");
|
||||
return;
|
||||
}
|
||||
@@ -249,7 +239,6 @@ static void do_stop_app() {
|
||||
app::AppInstance* app_to_stop = loader_singleton->app_stack.top();
|
||||
|
||||
if (original_stack_size == 1 && app_to_stop->getManifest().type != app::TypeBoot) {
|
||||
loader_unlock();
|
||||
TT_LOG_E(TAG, "Stop app: can't stop root app");
|
||||
return;
|
||||
}
|
||||
@@ -257,8 +246,8 @@ static void do_stop_app() {
|
||||
std::unique_ptr<app::ResultHolder> result_holder = std::move(app_to_stop->getResult());
|
||||
|
||||
const app::AppManifest& manifest = app_to_stop->getManifest();
|
||||
app_transition_to_state(*app_to_stop, app::StateHiding);
|
||||
app_transition_to_state(*app_to_stop, app::StateStopped);
|
||||
transitionAppToState(*app_to_stop, app::StateHiding);
|
||||
transitionAppToState(*app_to_stop, app::StateStopped);
|
||||
|
||||
loader_singleton->app_stack.pop();
|
||||
delete app_to_stop;
|
||||
@@ -273,12 +262,14 @@ static void do_stop_app() {
|
||||
if (!loader_singleton->app_stack.empty()) {
|
||||
app_to_resume = loader_singleton->app_stack.top();
|
||||
tt_assert(app_to_resume);
|
||||
app_transition_to_state(*app_to_resume, app::StateShowing);
|
||||
transitionAppToState(*app_to_resume, app::StateShowing);
|
||||
|
||||
on_result = app_to_resume->getManifest().onResult;
|
||||
}
|
||||
|
||||
loader_unlock();
|
||||
// Unlock so that we can send results to app and they can also start/stop new apps while processing these results
|
||||
scoped_lock->unlock();
|
||||
// WARNING: After this point we cannot change the app states from this method directly anymore as we don't have a lock!
|
||||
|
||||
LoaderEventInternal event_internal = {.type = LoaderEventTypeApplicationStopped};
|
||||
tt_pubsub_publish(loader_singleton->pubsub_internal, &event_internal);
|
||||
@@ -319,61 +310,24 @@ static void do_stop_app() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int32_t loader_main(TT_UNUSED void* parameter) {
|
||||
LoaderMessage message;
|
||||
bool exit_requested = false;
|
||||
while (!exit_requested) {
|
||||
tt_assert(loader_singleton != nullptr);
|
||||
if (loader_singleton->queue.get(&message, TtWaitForever) == TtStatusOk) {
|
||||
TT_LOG_I(TAG, "Processing message of type %d", message.type);
|
||||
switch (message.type) {
|
||||
case LoaderMessageTypeAppStart:
|
||||
message.result.status_value.value = do_start_by_id(
|
||||
message.payload.start->id,
|
||||
message.payload.start->parameters
|
||||
);
|
||||
if (message.api_lock != nullptr) {
|
||||
message.api_lock->set(LOADER_EVENT_FLAG);
|
||||
}
|
||||
message.cleanup();
|
||||
break;
|
||||
case LoaderMessageTypeAppStop:
|
||||
do_stop_app();
|
||||
break;
|
||||
case LoaderMessageTypeServiceStop:
|
||||
exit_requested = true;
|
||||
break;
|
||||
case LoaderMessageTypeNone:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// region AppManifest
|
||||
|
||||
static void loader_start(TT_UNUSED ServiceContext& service) {
|
||||
tt_check(loader_singleton == nullptr);
|
||||
loader_singleton = loader_alloc();
|
||||
|
||||
loader_singleton->thread->setPriority(THREAD_PRIORITY_SERVICE);
|
||||
loader_singleton->thread->start();
|
||||
loader_singleton->dispatcherThread->start();
|
||||
}
|
||||
|
||||
static void loader_stop(TT_UNUSED ServiceContext& service) {
|
||||
tt_check(loader_singleton != nullptr);
|
||||
|
||||
// Send stop signal to thread and wait for thread to finish
|
||||
loader_lock();
|
||||
LoaderMessage message(LoaderMessageTypeServiceStop);
|
||||
loader_singleton->queue.put(&message, TtWaitForever);
|
||||
loader_unlock();
|
||||
if (!loader_singleton->mutex.lock(2000 / portTICK_PERIOD_MS)) {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "loader_stop");
|
||||
}
|
||||
loader_singleton->dispatcherThread->stop();
|
||||
|
||||
loader_singleton->thread->join();
|
||||
delete loader_singleton->thread;
|
||||
loader_singleton->mutex.unlock();
|
||||
|
||||
loader_free();
|
||||
loader_singleton = nullptr;
|
||||
|
||||
@@ -23,9 +23,8 @@ typedef enum {
|
||||
* @param[in] id application name or id
|
||||
* @param[in] blocking whether this call is blocking or not. You cannot call this from an LVGL thread.
|
||||
* @param[in] parameters optional parameters to pass onto the application
|
||||
* @return LoaderStatus
|
||||
*/
|
||||
LoaderStatus startApp(const std::string& id, bool blocking = false, std::shared_ptr<const Bundle> _Nullable parameters = nullptr);
|
||||
void startApp(const std::string& id, bool blocking = false, std::shared_ptr<const Bundle> _Nullable parameters = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Stop the currently showing app. Show the previous app if any app was still running.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
#include "Tactility.h"
|
||||
|
||||
#include "hal/Power.h"
|
||||
#include "hal/sdcard/Sdcard.h"
|
||||
#include "hal/SdCard.h"
|
||||
#include "lvgl/Statusbar.h"
|
||||
#include "service/ServiceContext.h"
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "service/ServiceRegistry.h"
|
||||
#include "TactilityHeadless.h"
|
||||
|
||||
namespace tt::service::statusbar {
|
||||
|
||||
@@ -85,25 +86,29 @@ static void update_wifi_icon(std::shared_ptr<ServiceData> data) {
|
||||
|
||||
// region sdcard
|
||||
|
||||
static _Nullable const char* sdcard_get_status_icon(hal::sdcard::State state) {
|
||||
static const char* sdcard_get_status_icon(hal::SdCard::State state) {
|
||||
switch (state) {
|
||||
case hal::sdcard::StateMounted:
|
||||
case hal::SdCard::StateMounted:
|
||||
return TT_ASSETS_ICON_SDCARD;
|
||||
case hal::sdcard::StateError:
|
||||
case hal::sdcard::StateUnmounted:
|
||||
case hal::SdCard::StateError:
|
||||
case hal::SdCard::StateUnmounted:
|
||||
case hal::SdCard::StateUnknown:
|
||||
return TT_ASSETS_ICON_SDCARD_ALERT;
|
||||
default:
|
||||
return nullptr;
|
||||
tt_crash("Unhandled SdCard state");
|
||||
}
|
||||
}
|
||||
|
||||
static void update_sdcard_icon(std::shared_ptr<ServiceData> data) {
|
||||
hal::sdcard::State state = hal::sdcard::getState();
|
||||
const char* desired_icon = sdcard_get_status_icon(state);
|
||||
if (data->sdcard_last_icon != desired_icon) {
|
||||
lvgl::statusbar_icon_set_image(data->sdcard_icon_id, desired_icon);
|
||||
lvgl::statusbar_icon_set_visibility(data->sdcard_icon_id, desired_icon != nullptr);
|
||||
data->sdcard_last_icon = desired_icon;
|
||||
auto sdcard = tt::hal::getConfiguration().sdcard;
|
||||
if (sdcard != nullptr) {
|
||||
auto state = sdcard->getState();
|
||||
const char* desired_icon = sdcard_get_status_icon(state);
|
||||
if (data->sdcard_last_icon != desired_icon) {
|
||||
lvgl::statusbar_icon_set_image(data->sdcard_icon_id, desired_icon);
|
||||
lvgl::statusbar_icon_set_visibility(data->sdcard_icon_id, desired_icon != nullptr);
|
||||
data->sdcard_last_icon = desired_icon;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user