Cleanup and improvements (#194)

- Lots of changes for migrating C code to C++
- Improved `Lockable` in several ways like adding `withLock()` (+ tests)
- Improved `Semaphore` a bit for improved readability, and also added some tests
- Upgrade Linux machine in GitHub Actions so that we can compile with a newer GCC
- Simplification of WiFi connection
- Updated funding options
- (and more)
This commit is contained in:
Ken Van Hoeylandt
2025-01-28 17:39:58 +01:00
committed by GitHub
parent 1bb1260ea0
commit 6c67845645
54 changed files with 518 additions and 531 deletions
+21 -22
View File
@@ -85,15 +85,16 @@ std::shared_ptr<PubSub> getPubsub() {
static const char* appStateToString(app::State state) {
switch (state) {
case app::StateInitial:
using enum app::State;
case Initial:
return "initial";
case app::StateStarted:
case Started:
return "started";
case app::StateShowing:
case Showing:
return "showing";
case app::StateHiding:
case Hiding:
return "hiding";
case app::StateStopped:
case Stopped:
return "stopped";
default:
return "?";
@@ -113,31 +114,29 @@ static void transitionAppToState(std::shared_ptr<app::AppInstance> app, app::Sta
);
switch (state) {
case app::StateInitial:
app->setState(app::StateInitial);
using enum app::State;
case Initial:
break;
case app::StateStarted:
case Started:
app->getApp()->onStart(*app);
app->setState(app::StateStarted);
break;
case app::StateShowing: {
case Showing: {
LoaderEvent event_showing = { .type = LoaderEventTypeApplicationShowing };
loader_singleton->pubsubExternal->publish(&event_showing);
app->setState(app::StateShowing);
break;
}
case app::StateHiding: {
case Hiding: {
LoaderEvent event_hiding = { .type = LoaderEventTypeApplicationHiding };
loader_singleton->pubsubExternal->publish(&event_hiding);
app->setState(app::StateHiding);
break;
}
case app::StateStopped:
case Stopped:
// TODO: Verify manifest
app->getApp()->onStop(*app);
app->setState(app::StateStopped);
break;
}
app->setState(state);
}
static LoaderStatus startAppWithManifestInternal(
@@ -160,15 +159,15 @@ static LoaderStatus startAppWithManifestInternal(
new_app->mutableFlags().showStatusbar = (manifest->type != app::Type::Boot);
loader_singleton->appStack.push(new_app);
transitionAppToState(new_app, app::StateInitial);
transitionAppToState(new_app, app::StateStarted);
transitionAppToState(new_app, app::State::Initial);
transitionAppToState(new_app, app::State::Started);
// We might have to hide the previous app first
if (previous_app != nullptr) {
transitionAppToState(previous_app, app::StateHiding);
transitionAppToState(previous_app, app::State::Hiding);
}
transitionAppToState(new_app, app::StateShowing);
transitionAppToState(new_app, app::State::Showing);
LoaderEvent event_external = { .type = LoaderEventTypeApplicationStarted };
loader_singleton->pubsubExternal->publish(&event_external);
@@ -230,8 +229,8 @@ static void stopAppInternal() {
result_set = true;
}
transitionAppToState(app_to_stop, app::StateHiding);
transitionAppToState(app_to_stop, app::StateStopped);
transitionAppToState(app_to_stop, app::State::Hiding);
transitionAppToState(app_to_stop, app::State::Stopped);
loader_singleton->appStack.pop();
@@ -254,7 +253,7 @@ static void stopAppInternal() {
if (!loader_singleton->appStack.empty()) {
instance_to_resume = loader_singleton->appStack.top();
assert(instance_to_resume);
transitionAppToState(instance_to_resume, app::StateShowing);
transitionAppToState(instance_to_resume, app::State::Showing);
}
// Unlock so that we can send results to app and they can also start/stop new apps while processing these results
@@ -18,7 +18,7 @@ std::shared_ptr<ScreenshotService> _Nullable optScreenshotService() {
return service::findServiceById<ScreenshotService>(manifest.id);
}
void ScreenshotService::startApps(const char* path) {
void ScreenshotService::startApps(const std::string& path) {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
@@ -34,7 +34,7 @@ void ScreenshotService::startApps(const char* path) {
}
}
void ScreenshotService::startTimed(const char* path, uint8_t delayInSeconds, uint8_t amount) {
void ScreenshotService::startTimed(const std::string& path, uint8_t delayInSeconds, 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);
@@ -35,14 +35,14 @@ public:
/** @brief Start taking screenshot whenever an app is started
* @param[in] path the path to store the screenshots at
*/
void startApps(const char* path);
void startApps(const std::string& path);
/** @brief Start taking screenshots after a certain delay
* @param[in] path the path to store the screenshots at
* @param[in] delayInSeconds the delay before starting (and between successive screenshots)
* @param[in] amount 0 = indefinite, >0 for a specific
*/
void startTimed(const char* path, uint8_t delayInSeconds, uint8_t amount);
void startTimed(const std::string& path, uint8_t delayInSeconds, uint8_t amount);
/** @brief Stop taking screenshots */
void stop();
@@ -2,11 +2,10 @@
#if TT_FEATURE_SCREENSHOT_ENABLED
#include <cstring>
#include "ScreenshotTask.h"
#include "lv_screenshot.h"
#include <format>
#include "app/AppContext.h"
#include "TactilityCore.h"
#include "service/loader/Loader.h"
#include "lvgl/LvglSync.h"
@@ -45,12 +44,12 @@ void ScreenshotTask::setFinished() {
finished = true;
}
static void makeScreenshot(const char* filename) {
static void makeScreenshot(const std::string& 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);
if (lv_screenshot_create(lv_scr_act(), LV_100ASK_SCREENSHOT_SV_PNG, filename.c_str())) {
TT_LOG_I(TAG, "Screenshot saved to %s", filename.c_str());
} else {
TT_LOG_E(TAG, "Screenshot not saved to %s", filename);
TT_LOG_E(TAG, "Screenshot not saved to %s", filename.c_str());
}
lvgl::unlock();
} else {
@@ -78,8 +77,7 @@ void ScreenshotTask::taskMain() {
if (!isInterrupted()) {
screenshots_taken++;
char filename[SCREENSHOT_PATH_LIMIT + 32];
sprintf(filename, "%s/screenshot-%d.png", work.path, screenshots_taken);
std::string filename = std::format("{}/screenshot-{}.png", work.path, screenshots_taken);
makeScreenshot(filename);
if (work.amount > 0 && screenshots_taken >= work.amount) {
@@ -93,8 +91,7 @@ void ScreenshotTask::taskMain() {
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", work.path, manifest.id.c_str());
auto filename = std::format("{}/screenshot-{}.png", work.path, manifest.id);
makeScreenshot(filename);
}
}
@@ -123,9 +120,7 @@ void ScreenshotTask::taskStart() {
thread->start();
}
void ScreenshotTask::startApps(const char* path) {
tt_check(strlen(path) < (SCREENSHOT_PATH_LIMIT - 1));
void ScreenshotTask::startApps(const std::string& path) {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
@@ -135,15 +130,14 @@ void ScreenshotTask::startApps(const char* path) {
if (thread == nullptr) {
interrupted = false;
work.type = TASK_WORK_TYPE_APPS;
strcpy(work.path, path);
work.path = path;
taskStart();
} else {
TT_LOG_E(TAG, "Task was already running");
}
}
void ScreenshotTask::startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
tt_check(strlen(path) < (SCREENSHOT_PATH_LIMIT - 1));
void ScreenshotTask::startTimed(const std::string& path, uint8_t delay_in_seconds, uint8_t amount) {
auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
@@ -155,7 +149,7 @@ void ScreenshotTask::startTimed(const char* path, uint8_t delay_in_seconds, uint
work.type = TASK_WORK_TYPE_DELAY;
work.delay_in_seconds = delay_in_seconds;
work.amount = amount;
strcpy(work.path, path);
work.path = path;
taskStart();
} else {
TT_LOG_E(TAG, "Task was already running");
@@ -13,15 +13,13 @@ namespace tt::service::screenshot {
#define TASK_WORK_TYPE_DELAY 1
#define TASK_WORK_TYPE_APPS 2
#define SCREENSHOT_PATH_LIMIT 128
class ScreenshotTask {
struct ScreenshotTaskWork {
int type = TASK_WORK_TYPE_DELAY ;
uint8_t delay_in_seconds = 0;
uint8_t amount = 0;
char path[SCREENSHOT_PATH_LIMIT] = { 0 };
std::string path;
};
Thread* thread = nullptr;
@@ -39,12 +37,12 @@ public:
* @param[in] delayInSeconds the delay before starting (and between successive screenshots)
* @param[in] amount 0 = indefinite, >0 for a specific
*/
void startTimed(const char* path, uint8_t delayInSeconds, uint8_t amount);
void startTimed(const std::string& path, uint8_t delayInSeconds, uint8_t amount);
/** @brief Start taking screenshot whenever an app is started
* @param[in] path the path to store the screenshots at
*/
void startApps(const char* path);
void startApps(const std::string& path);
/** @brief Stop taking screenshots */
void stop();
@@ -54,14 +54,15 @@ const char* getWifiStatusIconForRssi(int rssi) {
static const char* getWifiStatusIcon(wifi::RadioState state, bool secure) {
int rssi;
switch (state) {
case wifi::RadioState::On:
case wifi::RadioState::OnPending:
case wifi::RadioState::ConnectionPending:
using enum wifi::RadioState;
case On:
case OnPending:
case ConnectionPending:
return STATUSBAR_ICON_WIFI_SCAN_WHITE;
case wifi::RadioState::OffPending:
case wifi::RadioState::Off:
case OffPending:
case Off:
return STATUSBAR_ICON_WIFI_OFF_WHITE;
case wifi::RadioState::ConnectionActive:
case ConnectionActive:
rssi = wifi::getRssi();
return getWifiStatusIconForRssi(rssi);
default:
@@ -71,11 +72,12 @@ static const char* getWifiStatusIcon(wifi::RadioState state, bool secure) {
static const char* getSdCardStatusIcon(hal::SdCard::State state) {
switch (state) {
case hal::SdCard::State::Mounted:
using enum hal::SdCard::State;
case Mounted:
return STATUSBAR_ICON_SDCARD;
case hal::SdCard::State::Error:
case hal::SdCard::State::Unmounted:
case hal::SdCard::State::Unknown:
case Error:
case Unmounted:
case Unknown:
return STATUSBAR_ICON_SDCARD_ALERT;
default:
tt_crash("Unhandled SdCard state");