Various services improved (#110)
This commit is contained in:
committed by
GitHub
parent
36bb25deba
commit
d52fe52d96
@@ -29,38 +29,30 @@ static Loader* loader_singleton = nullptr;
|
||||
static Loader* loader_alloc() {
|
||||
assert(loader_singleton == nullptr);
|
||||
loader_singleton = new Loader();
|
||||
loader_singleton->pubsub_internal = tt_pubsub_alloc();
|
||||
loader_singleton->pubsub_external = tt_pubsub_alloc();
|
||||
loader_singleton->thread = new Thread(
|
||||
"loader",
|
||||
4096, // Last known minimum was 2400 for starting Hello World app
|
||||
&loader_main,
|
||||
nullptr
|
||||
);
|
||||
loader_singleton->mutex = tt_mutex_alloc(MutexTypeRecursive);
|
||||
return loader_singleton;
|
||||
}
|
||||
|
||||
static void loader_free() {
|
||||
tt_assert(loader_singleton != nullptr);
|
||||
delete loader_singleton->thread;
|
||||
tt_pubsub_free(loader_singleton->pubsub_internal);
|
||||
tt_pubsub_free(loader_singleton->pubsub_external);
|
||||
tt_mutex_free(loader_singleton->mutex);
|
||||
delete loader_singleton;
|
||||
loader_singleton = nullptr;
|
||||
}
|
||||
|
||||
static void loader_lock() {
|
||||
tt_assert(loader_singleton);
|
||||
tt_assert(loader_singleton->mutex);
|
||||
tt_check(tt_mutex_acquire(loader_singleton->mutex, TtWaitForever) == TtStatusOk);
|
||||
tt_check(loader_singleton->mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void loader_unlock() {
|
||||
tt_assert(loader_singleton);
|
||||
tt_assert(loader_singleton->mutex);
|
||||
tt_check(tt_mutex_release(loader_singleton->mutex) == TtStatusOk);
|
||||
tt_check(loader_singleton->mutex.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
LoaderStatus startApp(const std::string& id, bool blocking, std::shared_ptr<const Bundle> parameters) {
|
||||
@@ -107,7 +99,7 @@ app::AppContext* _Nullable getCurrentApp() {
|
||||
return dynamic_cast<app::AppContext*>(app);
|
||||
}
|
||||
|
||||
PubSub* getPubsub() {
|
||||
std::shared_ptr<PubSub> getPubsub() {
|
||||
tt_assert(loader_singleton);
|
||||
// it's safe to return pubsub without locking
|
||||
// because it's never freed and loader is never exited
|
||||
|
||||
@@ -37,6 +37,6 @@ app::AppContext* _Nullable getCurrentApp();
|
||||
/**
|
||||
* @brief PubSub for LoaderEvent
|
||||
*/
|
||||
PubSub* getPubsub();
|
||||
std::shared_ptr<PubSub> getPubsub();
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "Assets.h"
|
||||
#include "Mutex.h"
|
||||
#include "Timer.h"
|
||||
#include "Tactility.h"
|
||||
|
||||
#include "hal/Power.h"
|
||||
#include "hal/sdcard/Sdcard.h"
|
||||
#include "Mutex.h"
|
||||
#include "lvgl/Statusbar.h"
|
||||
#include "service/ServiceContext.h"
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "Tactility.h"
|
||||
#include "lvgl/Statusbar.h"
|
||||
#include "service/ServiceRegistry.h"
|
||||
|
||||
namespace tt::service::statusbar {
|
||||
@@ -16,8 +18,7 @@ extern const ServiceManifest manifest;
|
||||
|
||||
struct ServiceData {
|
||||
Mutex mutex;
|
||||
Thread thread;
|
||||
bool service_interrupted = false;
|
||||
std::unique_ptr<Timer> updateTimer;
|
||||
int8_t wifi_icon_id = lvgl::statusbar_icon_add(nullptr);
|
||||
const char* wifi_last_icon = nullptr;
|
||||
int8_t sdcard_icon_id = lvgl::statusbar_icon_add(nullptr);
|
||||
@@ -153,51 +154,35 @@ static void service_data_free(ServiceData* data) {
|
||||
free(data);
|
||||
}
|
||||
|
||||
int32_t serviceMain(TT_UNUSED void* parameter) {
|
||||
TT_LOG_I(TAG, "Started main loop");
|
||||
delay_ms(20); // TODO: Make service instance findable earlier on (but expose "starting" state?)
|
||||
auto context = tt::service::findServiceById(manifest.id);
|
||||
if (context == nullptr) {
|
||||
TT_LOG_E(TAG, "Service not found");
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto data = std::static_pointer_cast<ServiceData>(context->getData());
|
||||
|
||||
while (!data->service_interrupted) {
|
||||
update_wifi_icon(data);
|
||||
update_sdcard_icon(data);
|
||||
update_power_icon(data);
|
||||
delay_ms(1000);
|
||||
}
|
||||
return 0;
|
||||
static void onUpdate(std::shared_ptr<void> parameter) {
|
||||
auto data = std::static_pointer_cast<ServiceData>(parameter);
|
||||
// TODO: Make thread-safe for LVGL
|
||||
update_wifi_icon(data);
|
||||
update_sdcard_icon(data);
|
||||
update_power_icon(data);
|
||||
}
|
||||
|
||||
static void onStart(ServiceContext& service) {
|
||||
auto data = std::make_shared<ServiceData>();
|
||||
service.setData(data);
|
||||
|
||||
// TODO: Make thread-safe for LVGL
|
||||
lvgl::statusbar_icon_set_visibility(data->wifi_icon_id, true);
|
||||
update_wifi_icon(data);
|
||||
update_sdcard_icon(data); // also updates visibility
|
||||
update_power_icon(data);
|
||||
|
||||
|
||||
data->thread.setCallback(serviceMain, nullptr);
|
||||
data->thread.setPriority(Thread::PriorityLow);
|
||||
data->thread.setStackSize(3000);
|
||||
data->thread.setName("statusbar");
|
||||
data->thread.start();
|
||||
data->updateTimer = std::make_unique<Timer>(Timer::TypePeriodic, onUpdate, data);
|
||||
// We want to try and scan more often in case of startup or scan lock failure
|
||||
data->updateTimer->start(1000);
|
||||
}
|
||||
|
||||
static void onStop(ServiceContext& service) {
|
||||
auto data = std::static_pointer_cast<ServiceData>(service.getData());
|
||||
|
||||
// Stop thread
|
||||
data->lock();
|
||||
data->service_interrupted = true;
|
||||
data->unlock();
|
||||
data->thread.join();
|
||||
data->updateTimer->stop();
|
||||
data->updateTimer = nullptr;
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
|
||||
Reference in New Issue
Block a user