Skip to content

Commit

Permalink
fix launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
paxo-rch committed Sep 7, 2024
1 parent 0bdd4af commit ef11bde
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 69 deletions.
71 changes: 25 additions & 46 deletions lib/applications/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,69 +373,48 @@ namespace AppManager {
}

void loop() {
threadsync.lock();
// Implementation for the main loop
if (!appStack.empty() && hardware::getHomeButton())
// if the home button is pressed, remove the app from the stack, and kill it if it's not running in the background
{
while (hardware::getHomeButton()) {}

if (!appStack.empty()) {
if (appStack.back()->background == false) {
const auto app = appStack.back();
int count = 0; // TODO: Use ?
updateForeground();
updateBackground();
}

for (const auto& it : appStack) {
if (it == app) {
count++;
}
}
void updateForeground() {
threadsync.lock();

// Run tick on every app
for (const auto& app: appList) {
if(app->background == false) // app is not in background
{
if (app->isRunning()) { // app is running
app->luaInstance->loop();
} else if (std::find(appStack.begin(), appStack.end(), app.get()) != appStack.end()) // if app is no longer in the stack (no gui is running) -> kill it
{
app->kill();
} else {
std::cerr << "Error: app is in background" << std::endl;
}

appStack.pop_back();
}
}

for (const auto& app: appList) {
if (app->isRunning()) {
app->luaInstance->loop();
} else if (app->luaInstance != nullptr) {
app->kill();
}
}

// Update foreground app GUI
if (!appStack.empty()) {
appStack.back()->luaInstance->lua_gui.update();
const App* app = appStack.back();

if (app->luaInstance != nullptr) {
app->luaInstance->lua_gui.update();
}
}

threadsync.unlock();

StandbyMode::wait();
}

void update() {
void updateBackground() {
threadsync.lock();

// Run tick on every app
for (const auto& app: appList) {
if (app->isRunning()) {
app->luaInstance->loop();
} else if (app->luaInstance != nullptr) {
app->kill();
}
}

// Update foreground app GUI
// TODO : What happens if a background app is on top of a foreground app on the stack ?
if (!appStack.empty()) {
const App* app = appStack.back();

if (app->luaInstance != nullptr) {
app->luaInstance->lua_gui.update();
if(app->background == true) // app is in background
{
if (app->isRunning()) { // app is running
app->luaInstance->loop();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/applications/src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ namespace AppManager
/**
* Update every application.
*/
void update();
void updateForeground(); // easy to understand
void updateBackground();

/**
* Quit the currently foreground running application.
Expand Down
43 changes: 32 additions & 11 deletions lib/applications/src/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,27 @@ void applications::launcher::init() {
}

void applications::launcher::update() {
std::cout << "Launcher update" << std::endl;

if (dirty) {
// If dirty, free to force redraw it
free();
std::cout << "Launcher free" << std::endl;
}

if (!allocated) {
// If launcher has been freed, redraw it
draw();
std::cout << "Launcher redraw" << std::endl;
}

// Update dynamic elements
// Do this before updating the window (so drawing it)
// Because it can cause weird "blinking" effects

// TODO : Refactor this
if (millis() > lastClockUpdate + 1000) {
// What ???
std::cout << "launcher::update 1" << std::endl;

{
static int min;

if(min != GSM::minutes) {
Expand All @@ -110,16 +114,20 @@ void applications::launcher::update() {

min = GSM::minutes;
}

lastClockUpdate = millis();
}

std::cout << "launcher::update 2" << std::endl;

if (millis() > lastBatteryUpdate + 10000) {
// batteryIcon->setImage();
batteryLabel->setText(std::to_string(static_cast<int>(GSM::getBatteryLevel() * 100)) + "%");

lastBatteryUpdate = millis();
}


std::cout << "launcher::update 3" << std::endl;

if (hardware::isCharging()) {
if (chargingStartTime == 0) {
chargingStartTime = millis();
Expand All @@ -135,14 +143,13 @@ void applications::launcher::update() {
chargingPopupBox->disable();
}

libsystem::log("launcher::update -");

// Update, draw AND update touch events
if (launcherWindow != nullptr) {
launcherWindow->updateAll();
}

// Update all events
eventHandlerApp.update();

// Check touch events

if (brightnessSliderBox->isFocused(true)) {
Expand Down Expand Up @@ -176,6 +183,8 @@ void applications::launcher::draw() {
launcherWindow = std::make_shared<Window>();
}

std::cout << "launcher::update 1.1" << std::endl;

StandbyMode::triggerPower();

// Clock
Expand All @@ -186,6 +195,9 @@ void applications::launcher::draw() {
clockLabel->setFontSize(36);
launcherWindow->addChild(clockLabel);


std::cout << "launcher::update 1.2" << std::endl;

// Date
dateLabel = new Label(55, 89, 210, 18);
dateLabel->setText(getFormatedDate());
Expand All @@ -194,12 +206,18 @@ void applications::launcher::draw() {
dateLabel->setFontSize(16);
launcherWindow->addChild(dateLabel);


std::cout << "launcher::update 1.3" << std::endl;

// Battery icon
const auto batteryIconDarkPath = storage::Path("system/icons/dark/" + getBatteryIconFilename() + "_64px.png");
batteryIcon = new Image(batteryIconDarkPath, 290, 2, 32, 32, TFT_WHITE);
batteryIcon->load();
launcherWindow->addChild(batteryIcon);


//std::cout << "launcher::update 1.4" << std::endl;

// Battery label
batteryLabel = new Label(255, 10, 40, 18);
batteryLabel->setText(std::to_string(static_cast<int>(GSM::getBatteryLevel() * 100)) + "%");
Expand All @@ -208,6 +226,9 @@ void applications::launcher::draw() {
batteryLabel->setFontSize(18);
launcherWindow->addChild(batteryLabel);


//std::cout << "launcher::update 1.5" << std::endl;

// Network
if (GSM::getNetworkStatus() == 99) {
auto* networkLabel = new Label(10, 10, 100, 18);
Expand All @@ -218,6 +239,9 @@ void applications::launcher::draw() {
launcherWindow->addChild(networkLabel);
}


//std::cout << "launcher::update 1.6" << std::endl;

// Brightness slider
brightnessSliderBox = new Box(0, 77, 50, 325);
//light->setBackgroundColor(COLOR_RED);
Expand Down Expand Up @@ -307,9 +331,6 @@ void applications::launcher::draw() {

lastClockUpdate = millis();
lastBatteryUpdate = millis();

// Is this the inverse of "triggerPower()" ?
StandbyMode::restorePower();
}

bool applications::launcher::iconTouched() {
Expand Down
11 changes: 3 additions & 8 deletions lib/gsm/src/gsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,15 @@ namespace GSM
endCall();
}

float getVoltage()
void getVoltage() // THIS IS A TASK, DO NOT CALL IT!
{
std::string answer = send("AT+CBC", "OK");

int start = answer.find("+CBC: ") + 6;
int end = answer.find("V", start);

if (start == std::string::npos || end == std::string::npos) // maybe wrong
return 0;
return;

std::string voltage_str = answer.substr(start, end - start);

Expand All @@ -994,19 +994,14 @@ namespace GSM
}
catch (std::exception)
{
voltage = -1;
}

return 0;
}

double getBatteryLevel() {
#ifdef ESP_PLATFORM
const float voltage = getVoltage();

if (voltage == -1) {
// Probably return something else ?
return 100;
return 1;
}

// Thanks NumWorks for the regression app
Expand Down
4 changes: 4 additions & 0 deletions lib/gui/src/ElementBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,16 @@ bool gui::ElementBase::isFocused(bool forced)

void gui::ElementBase::enable()
{
if(m_isEnabled)
return;
m_isEnabled = true;
globalGraphicalUpdate();
}

void gui::ElementBase::disable()
{
if(!m_isEnabled)
return;
m_isEnabled = false;
globalGraphicalUpdate();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/system/libsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void libsystem::setDeviceMode(const DeviceMode mode) {
switch (mode) {
case NORMAL:
StandbyMode::restorePower();
graphics::setBrightness(0xFF);
graphics::setBrightness(graphics::brightness);
break;
case SLEEP:
graphics::setBrightness(0x00);
Expand Down
Loading

0 comments on commit ef11bde

Please sign in to comment.