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
+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();
}