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
+8 -8
View File
@@ -10,13 +10,13 @@
namespace tt::app {
typedef enum {
StateInitial, // App is being activated in loader
StateStarted, // App is in memory
StateShowing, // App view is created
StateHiding, // App view is destroyed
StateStopped // App is not in memory
} State;
enum class State {
Initial, // App is being activated in loader
Started, // App is in memory
Showing, // App view is created
Hiding, // App view is destroyed
Stopped // App is not in memory
};
/**
* Thread-safe app instance.
@@ -27,7 +27,7 @@ private:
Mutex mutex = Mutex(Mutex::Type::Normal);
const std::shared_ptr<AppManifest> manifest;
State state = StateInitial;
State state = State::Initial;
Flags flags = { .showStatusbar = true };
/** @brief Optional parameters to start the app with
* When these are stored in the app struct, the struct takes ownership.
+5 -7
View File
@@ -41,13 +41,11 @@ public:
bool setEntriesForChildPath(const std::string& child_path);
bool setEntriesForPath(const std::string& path);
const std::vector<dirent>& lockEntries() const {
mutex.lock();
return dir_entries;
}
void unlockEntries() {
mutex.unlock();
template <std::invocable<const std::vector<dirent> &> Func>
void withEntries(Func&& onEntries) const {
mutex.withLock([&]() {
std::invoke(std::forward<Func>(onEntries), dir_entries);
});
}
bool getDirent(uint32_t index, dirent& dirent);
@@ -20,9 +20,6 @@ public:
void setConnectionError(bool error);
bool hasConnectionError() const;
const service::wifi::settings::WifiApSettings& lockApSettings();
void unlockApSettings();
void setApSettings(const service::wifi::settings::WifiApSettings* newSettings);
void setConnecting(bool isConnecting);
+6 -2
View File
@@ -30,8 +30,12 @@ public:
void updateApRecords();
const std::vector<service::wifi::ApRecord>& lockApRecords() const;
void unlockApRecords() const;
template <std::invocable<const std::vector<service::wifi::ApRecord>&> Func>
void withApRecords(Func&& onApRecords) const {
mutex.withLock([&]() {
std::invoke(std::forward<Func>(onApRecords), apRecords);
});
}
void setConnectSsid(const std::string& ssid);
std::string getConnectSsid() const;
+38 -43
View File
@@ -62,51 +62,46 @@ namespace app {
#ifndef ESP_PLATFORM
#endif
static const std::vector<const app::AppManifest*> system_apps = {
&app::alertdialog::manifest,
&app::applist::manifest,
&app::boot::manifest,
&app::display::manifest,
&app::files::manifest,
&app::gpio::manifest,
&app::i2cscanner::manifest,
&app::i2csettings::manifest,
&app::imageviewer::manifest,
&app::inputdialog::manifest,
&app::launcher::manifest,
&app::log::manifest,
&app::settings::manifest,
&app::selectiondialog::manifest,
&app::systeminfo::manifest,
&app::textviewer::manifest,
&app::timedatesettings::manifest,
&app::timezone::manifest,
&app::usbsettings::manifest,
&app::wifiapsettings::manifest,
&app::wificonnect::manifest,
&app::wifimanage::manifest,
#if TT_FEATURE_SCREENSHOT_ENABLED
&app::screenshot::manifest,
#endif
#ifdef ESP_PLATFORM
&app::crashdiagnostics::manifest
#endif
};
// endregion
static void register_system_apps() {
TT_LOG_I(TAG, "Registering default apps");
for (const auto* app_manifest: system_apps) {
addApp(*app_manifest);
}
static void registerSystemApps() {
addApp(app::alertdialog::manifest);
addApp(app::applist::manifest);
addApp(app::boot::manifest);
addApp(app::display::manifest);
addApp(app::files::manifest);
addApp(app::gpio::manifest);
addApp(app::i2cscanner::manifest);
addApp(app::i2csettings::manifest);
addApp(app::imageviewer::manifest);
addApp(app::inputdialog::manifest);
addApp(app::launcher::manifest);
addApp(app::log::manifest);
addApp(app::settings::manifest);
addApp(app::selectiondialog::manifest);
addApp(app::systeminfo::manifest);
addApp(app::textviewer::manifest);
addApp(app::timedatesettings::manifest);
addApp(app::timezone::manifest);
addApp(app::usbsettings::manifest);
addApp(app::wifiapsettings::manifest);
addApp(app::wificonnect::manifest);
addApp(app::wifimanage::manifest);
#if TT_FEATURE_SCREENSHOT_ENABLED
addApp(app::screenshot::manifest);
#endif
#ifdef ESP_PLATFORM
addApp(app::crashdiagnostics::manifest);
#endif
if (getConfiguration()->hardware->power != nullptr) {
addApp(app::power::manifest);
}
}
static void register_user_apps(const std::vector<const app::AppManifest*>& apps) {
static void registerUserApps(const std::vector<const app::AppManifest*>& apps) {
TT_LOG_I(TAG, "Registering user apps");
for (auto* manifest : apps) {
assert(manifest != nullptr);
@@ -114,7 +109,7 @@ static void register_user_apps(const std::vector<const app::AppManifest*>& apps)
}
}
static void register_and_start_system_services() {
static void registerAndStartSystemServices() {
TT_LOG_I(TAG, "Registering and starting system services");
addService(service::loader::manifest);
addService(service::gui::manifest);
@@ -124,7 +119,7 @@ static void register_and_start_system_services() {
#endif
}
static void register_and_start_user_services(const std::vector<const service::ServiceManifest*>& manifests) {
static void registerAndStartUserServices(const std::vector<const service::ServiceManifest*>& manifests) {
TT_LOG_I(TAG, "Registering and starting user services");
for (auto* manifest : manifests) {
assert(manifest != nullptr);
@@ -147,14 +142,14 @@ void run(const Configuration& config) {
// Note: the order of starting apps and services is critical!
// System services are registered first so the apps below can find them if needed
register_and_start_system_services();
registerAndStartSystemServices();
// Then we register system apps. They are not used/started yet.
register_system_apps();
registerSystemApps();
// Then we register and start user services. They are started after system app
// registration just in case they want to figure out which system apps are installed.
register_and_start_user_services(config.services);
registerAndStartUserServices(config.services);
// Now we register the user apps, as they might rely on the user services.
register_user_apps(config.apps);
registerUserApps(config.apps);
TT_LOG_I(TAG, "init starting desktop app");
service::loader::startApp(app::boot::manifest.id);
+6 -2
View File
@@ -7,18 +7,22 @@
namespace tt {
namespace app::launcher { extern const app::AppManifest manifest; }
/** @brief The configuration for the operating system
* It contains the hardware configuration, apps and services
*/
struct Configuration {
/** HAL configuration (drivers) */
const hal::Configuration* hardware;
const hal::Configuration* hardware = nullptr;
/** List of user applications */
const std::vector<const app::AppManifest*> apps = {};
/** List of user services */
const std::vector<const service::ServiceManifest*> services = {};
/** Optional app to start automatically after the splash screen. */
const char* _Nullable autoStartAppId = nullptr;
const std::string launcherAppId = app::launcher::manifest.id;
/** Optional app to start automatically after the splash screen. */
const std::string autoStartAppId = {};
};
/**
+2 -2
View File
@@ -61,10 +61,10 @@ typedef std::shared_ptr<App>(*CreateApp)();
struct AppManifest {
/** The identifier by which the app is launched by the system and other apps. */
std::string id;
std::string id = {};
/** The user-readable name of the app. Used in UI. */
std::string name;
std::string name = {};
/** Optional icon. */
std::string icon = {};
+2 -6
View File
@@ -73,12 +73,8 @@ private:
#endif
auto* config = tt::getConfiguration();
if (config->autoStartAppId) {
TT_LOG_I(TAG, "init auto-starting %s", config->autoStartAppId);
tt::service::loader::startApp(config->autoStartAppId);
} else {
app::launcher::start();
}
assert(!config->launcherAppId.empty());
tt::service::loader::startApp(config->launcherAppId);
}
public:
@@ -69,7 +69,7 @@ public:
TT_LOG_I(TAG, "Create canvas");
int32_t available_height = parent_height - top_label_height - bottom_label_height;
int32_t available_width = lv_display_get_horizontal_resolution(display);
int32_t smallest_size = TT_MIN(available_height, available_width);
int32_t smallest_size = std::min(available_height, available_width);
int32_t pixel_size;
if (qrcode.size * 2 <= smallest_size) {
pixel_size = 2;
+7 -6
View File
@@ -229,12 +229,13 @@ void View::update() {
auto scoped_lockable = lvgl::getLvglSyncLockable()->scoped();
if (scoped_lockable->lock(100 / portTICK_PERIOD_MS)) {
lv_obj_clean(dir_entry_list);
auto entries = state->lockEntries();
for (auto entry : entries) {
TT_LOG_D(TAG, "Entry: %s %d", entry.d_name, entry.d_type);
createDirEntryWidget(dir_entry_list, entry);
}
state->unlockEntries();
state->withEntries([this](const std::vector<dirent>& entries) {
for (auto entry : entries) {
TT_LOG_D(TAG, "Entry: %s %d", entry.d_name, entry.d_type);
createDirEntryWidget(dir_entry_list, entry);
}
});
if (state->getCurrentPath() == "/") {
lv_obj_add_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN);
+13 -2
View File
@@ -1,9 +1,12 @@
#include "Check.h"
#include "Tactility.h"
#include "app/AppContext.h"
#include "app/ManifestRegistry.h"
#include "Check.h"
#include "lvgl.h"
#include "service/loader/Loader.h"
#define TAG "launcher"
namespace tt::app::launcher {
static void onAppPressed(TT_UNUSED lv_event_t* e) {
@@ -44,6 +47,14 @@ static lv_obj_t* createAppButton(lv_obj_t* parent, const char* title, const char
class LauncherApp : public App {
void onStart(TT_UNUSED AppContext& app) override {
auto* config = tt::getConfiguration();
if (!config->autoStartAppId.empty()) {
TT_LOG_I(TAG, "auto-starting %s", config->autoStartAppId.c_str());
tt::service::loader::startApp(config->autoStartAppId);
}
}
void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) override {
auto* wrapper = lv_obj_create(parent);
@@ -64,7 +75,7 @@ class LauncherApp : public App {
}
int32_t available_width = lv_display_get_horizontal_resolution(display) - (3 * 80);
int32_t padding = is_landscape_display ? TT_MIN(available_width / 4, 64) : 0;
int32_t padding = is_landscape_display ? std::min(available_width / 4, (int32_t)64) : 0;
auto paths = app.getPaths();
auto apps_icon_path = paths->getSystemPathLvgl("icon_apps.png");
+26 -27
View File
@@ -1,5 +1,3 @@
#include <sstream>
#include <vector>
#include "lvgl.h"
#include "lvgl/Style.h"
#include "lvgl/Toolbar.h"
@@ -7,6 +5,10 @@
#include "service/loader/Loader.h"
#include "lvgl/LvglSync.h"
#include <sstream>
#include <vector>
#include <ranges>
#define TAG "text_viewer"
namespace tt::app::log {
@@ -18,39 +20,36 @@ private:
LogLevel filterLevel = LogLevel::Info;
lv_obj_t* labelWidget = nullptr;
static bool shouldShowLog(LogLevel filterLevel, LogLevel logLevel) {
if (filterLevel == LogLevel::None || logLevel == LogLevel::None) {
return false;
} else {
return filterLevel >= logLevel;
}
static inline bool shouldShowLog(LogLevel filterLevel, LogLevel logLevel) {
return (filterLevel != LogLevel::None) &&
(logLevel != LogLevel::None) &&
filterLevel >= logLevel;
}
void updateLogEntries() {
unsigned int index;
auto* entries = copyLogEntries(index);
std::size_t next_log_index;
auto entries = copyLogEntries(next_log_index);
std::stringstream buffer;
if (entries != nullptr) {
for (unsigned int i = index; i < TT_LOG_ENTRY_COUNT; ++i) {
if (shouldShowLog(filterLevel, entries[i].level)) {
buffer << entries[i].message;
if (next_log_index != 0) {
long to_drop = TT_LOG_ENTRY_COUNT - next_log_index;
for (auto entry : std::views::drop(*entries, (long)next_log_index)) {
if (shouldShowLog(filterLevel, entry.level) && entry.message[0] != 0x00) {
buffer << entry.message;
}
}
if (index != 0) {
for (unsigned int i = 0; i < index; ++i) {
if (shouldShowLog(filterLevel, entries[i].level)) {
buffer << entries[i].message;
}
}
}
delete entries;
if (!buffer.str().empty()) {
lv_label_set_text(labelWidget, buffer.str().c_str());
} else {
lv_label_set_text(labelWidget, "No logs for the selected log level");
}
for (auto entry : std::views::take(*entries, (long)next_log_index)) {
if (shouldShowLog(filterLevel, entry.level) && entry.message[0] != 0x00) {
buffer << entry.message;
}
}
if (!buffer.str().empty()) {
lv_label_set_text(labelWidget, buffer.str().c_str());
} else {
lv_label_set_text(labelWidget, "Failed to load log");
lv_label_set_text(labelWidget, "No logs for the selected log level");
}
}
@@ -22,15 +22,6 @@ void State::setApSettings(const service::wifi::settings::WifiApSettings* newSett
lock.unlock();
}
const service::wifi::settings::WifiApSettings& State::lockApSettings() {
lock.lock();
return apSettings;
}
void State::unlockApSettings() {
lock.unlock();
}
void State::setConnecting(bool isConnecting) {
lock.lock();
connecting = isConnecting;
@@ -55,6 +55,7 @@ static void onConnect(TT_UNUSED lv_event_t* event) {
service::wifi::settings::WifiApSettings settings;
strcpy((char*)settings.password, password);
strcpy((char*)settings.ssid, ssid);
settings.channel = 0;
settings.auto_connect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w
auto* bindings = &wifi->getBindings();
@@ -32,15 +32,6 @@ bool State::isScanning() const {
return result;
}
const std::vector<service::wifi::ApRecord>& State::lockApRecords() const {
mutex.lock();
return apRecords;
}
void State::unlockApRecords() const {
mutex.unlock();
}
void State::updateApRecords() {
mutex.lock();
apRecords = service::wifi::getScanResults();
+51 -49
View File
@@ -130,16 +130,17 @@ void View::createSsidListItem(const service::wifi::ApRecord& record, bool isConn
}
void View::updateConnectToHidden() {
using enum service::wifi::RadioState;
switch (state->getRadioState()) {
case service::wifi::RadioState::On:
case service::wifi::RadioState::ConnectionPending:
case service::wifi::RadioState::ConnectionActive:
case On:
case ConnectionPending:
case ConnectionActive:
lv_obj_remove_flag(connect_to_hidden, LV_OBJ_FLAG_HIDDEN);
break;
case service::wifi::RadioState::OnPending:
case service::wifi::RadioState::OffPending:
case service::wifi::RadioState::Off:
case OnPending:
case OffPending:
case Off:
lv_obj_add_flag(connect_to_hidden, LV_OBJ_FLAG_HIDDEN);
break;
}
@@ -149,20 +150,20 @@ void View::updateNetworkList() {
lv_obj_clean(networks_list);
switch (state->getRadioState()) {
case service::wifi::RadioState::OnPending:
case service::wifi::RadioState::On:
case service::wifi::RadioState::ConnectionPending:
case service::wifi::RadioState::ConnectionActive: {
using enum service::wifi::RadioState;
case OnPending:
case On:
case ConnectionPending:
case ConnectionActive: {
std::string connection_target = service::wifi::getConnectionTarget();
auto& ap_records = state->lockApRecords();
bool is_connected = !connection_target.empty() &&
state->getRadioState() == service::wifi::RadioState::ConnectionActive;
bool added_connected = false;
if (is_connected) {
if (!ap_records.empty()) {
for (auto &record : ap_records) {
state->withApRecords([this, &connection_target](const std::vector<service::wifi::ApRecord>& apRecords){
bool is_connected = !connection_target.empty() &&
state->getRadioState() == service::wifi::RadioState::ConnectionActive;
bool added_connected = false;
if (is_connected && !apRecords.empty()) {
for (auto &record : apRecords) {
if (record.ssid == connection_target) {
lv_list_add_text(networks_list, "Connected");
createSsidListItem(record, false);
@@ -171,38 +172,38 @@ void View::updateNetworkList() {
}
}
}
}
lv_list_add_text(networks_list, "Other networks");
std::set<std::string> used_ssids;
if (!ap_records.empty()) {
for (auto& record : ap_records) {
if (used_ssids.find(record.ssid) == used_ssids.end()) {
bool connection_target_match = (record.ssid == connection_target);
bool is_connecting = connection_target_match
&& state->getRadioState() == service::wifi::RadioState::ConnectionPending &&
!connection_target.empty();
bool skip = connection_target_match && added_connected;
if (!skip) {
createSsidListItem(record, is_connecting);
lv_list_add_text(networks_list, "Other networks");
std::set<std::string> used_ssids;
if (!apRecords.empty()) {
for (auto& record : apRecords) {
if (used_ssids.find(record.ssid) == used_ssids.end()) {
bool connection_target_match = (record.ssid == connection_target);
bool is_connecting = connection_target_match
&& state->getRadioState() == service::wifi::RadioState::ConnectionPending &&
!connection_target.empty();
bool skip = connection_target_match && added_connected;
if (!skip) {
createSsidListItem(record, is_connecting);
}
used_ssids.insert(record.ssid);
}
used_ssids.insert(record.ssid);
}
lv_obj_clear_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
} else if (!state->hasScannedAfterRadioOn() || state->isScanning()) {
// hasScannedAfterRadioOn() prevents briefly showing "No networks found" when turning radio on.
lv_obj_add_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_clear_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
lv_obj_t* label = lv_label_create(networks_list);
lv_label_set_text(label, "No networks found.");
}
lv_obj_clear_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
} else if (!state->hasScannedAfterRadioOn() || state->isScanning()) {
// hasScannedAfterRadioOn() prevents briefly showing "No networks found" when turning radio on.
lv_obj_add_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_clear_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
lv_obj_t* label = lv_label_create(networks_list);
lv_label_set_text(label, "No networks found.");
}
state->unlockApRecords();
});
break;
}
case service::wifi::RadioState::OffPending:
case service::wifi::RadioState::Off: {
case OffPending:
case Off: {
lv_obj_add_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
break;
}
@@ -220,18 +221,19 @@ void View::updateScanning() {
void View::updateWifiToggle() {
lv_obj_clear_state(enable_switch, LV_STATE_ANY);
switch (state->getRadioState()) {
case service::wifi::RadioState::On:
case service::wifi::RadioState::ConnectionPending:
case service::wifi::RadioState::ConnectionActive:
using enum service::wifi::RadioState;
case On:
case ConnectionPending:
case ConnectionActive:
lv_obj_add_state(enable_switch, LV_STATE_CHECKED);
break;
case service::wifi::RadioState::OnPending:
case OnPending:
lv_obj_add_state(enable_switch, LV_STATE_CHECKED | LV_STATE_DISABLED);
break;
case service::wifi::RadioState::Off:
case Off:
lv_obj_remove_state(enable_switch, LV_STATE_CHECKED | LV_STATE_DISABLED);
break;
case service::wifi::RadioState::OffPending:
case OffPending:
lv_obj_remove_state(enable_switch, LV_STATE_CHECKED);
lv_obj_add_state(enable_switch, LV_STATE_DISABLED);
break;
@@ -91,14 +91,15 @@ static void wifiManageEventCallback(const void* message, void* context) {
TT_LOG_I(TAG, "Update with state %s", service::wifi::radioStateToString(radio_state));
wifi->getState().setRadioState(radio_state);
switch (event->type) {
case tt::service::wifi::EventType::ScanStarted:
using enum tt::service::wifi::EventType;
case ScanStarted:
wifi->getState().setScanning(true);
break;
case tt::service::wifi::EventType::ScanFinished:
case ScanFinished:
wifi->getState().setScanning(false);
wifi->getState().updateApRecords();
break;
case tt::service::wifi::EventType::RadioStateOn:
case RadioStateOn:
if (!service::wifi::isScanning()) {
service::wifi::scan();
}
+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");