#include #include #include #include #include #include #include #include #include #include #include #include #ifdef ESP_PLATFORM #include "Tactility/app/crashdiagnostics/CrashDiagnostics.h" #include #include #else #define CONFIG_TT_SPLASH_DURATION 0 #endif namespace tt::app::boot { constexpr auto* TAG = "Boot"; static std::shared_ptr getHalDisplay() { return hal::findFirstDevice(hal::Device::Type::Display); } class BootApp : public App { Thread thread = Thread( "boot", 5120, [] { return bootThreadCallback(); }, getCpuAffinityConfiguration().system ); static void setupDisplay() { const auto hal_display = getHalDisplay(); assert(hal_display != nullptr); settings::display::DisplaySettings settings; if (settings::display::load(settings)) { if (hal_display->getGammaCurveCount() > 0) { hal_display->setGammaCurve(settings.gammaCurve); TT_LOG_I(TAG, "Gamma curve %du", settings.gammaCurve); } } else { settings = settings::display::getDefault(); } if (hal_display->supportsBacklightDuty()) { TT_LOG_I(TAG, "Backlight %du", settings.backlightDuty); hal_display->setBacklightDuty(settings.backlightDuty); } else { TT_LOG_I(TAG, "no backlight"); } } static bool setupUsbBootMode() { if (!hal::usb::isUsbBootMode()) { return false; } TT_LOG_I(TAG, "Rebooting into mass storage device mode"); hal::usb::resetUsbBootMode(); hal::usb::startMassStorageWithSdmmc(); return true; } static void waitForMinimalSplashDuration(TickType_t startTime) { const auto end_time = kernel::getTicks(); const auto ticks_passed = end_time - startTime; constexpr auto minimum_ticks = (CONFIG_TT_SPLASH_DURATION / portTICK_PERIOD_MS); if (minimum_ticks > ticks_passed) { kernel::delayTicks(minimum_ticks - ticks_passed); } } static int32_t bootThreadCallback() { const auto start_time = kernel::getTicks(); kernel::publishSystemEvent(kernel::SystemEvent::BootSplash); setupDisplay(); // Set backlight if (!setupUsbBootMode()) { initFromBootApp(); waitForMinimalSplashDuration(start_time); service::loader::stopApp(); startNextApp(); } return 0; } static void startNextApp() { #ifdef ESP_PLATFORM if (esp_reset_reason() == ESP_RST_PANIC) { crashdiagnostics::start(); return; } #endif settings::BootSettings boot_properties; if (!settings::loadBootSettings(boot_properties) || boot_properties.launcherAppId.empty()) { TT_LOG_E(TAG, "Launcher not configured"); stop(); return; } service::loader::startApp(boot_properties.launcherAppId); } public: void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) override { auto* image = lv_image_create(parent); lv_obj_set_size(image, LV_PCT(100), LV_PCT(100)); const auto paths = app.getPaths(); const char* logo = hal::usb::isUsbBootMode() ? "logo_usb.png" : "logo.png"; const auto logo_path = paths->getSystemPathLvgl(logo); TT_LOG_I(TAG, "%s", logo_path.c_str()); lv_image_set_src(image, logo_path.c_str()); lvgl::obj_set_style_bg_blacken(parent); // Just in case this app is somehow resumed if (thread.getState() == Thread::State::Stopped) { thread.start(); } } void onDestroy(AppContext& app) override { thread.join(); } }; extern const AppManifest manifest = { .id = "Boot", .name = "Boot", .type = Type::Boot, .createApp = create }; } // namespace