Skip to content

Commit

Permalink
Ajouts sur le launcher: Mode avion, mode veille automatique, et mises…
Browse files Browse the repository at this point in the history
… a jour automatiques sur le launcher
  • Loading branch information
paxo-rch committed Sep 8, 2024
1 parent ef11bde commit 33f5cf8
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 51 deletions.
94 changes: 80 additions & 14 deletions lib/applications/src/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ namespace applications::launcher {
Image* batteryIcon = nullptr;
Box* chargingPopupBox = nullptr;
Box* brightnessSliderBox = nullptr;
Label* networkLabel = nullptr;

Box *flightModeBox = nullptr;
Label* flightModeText = nullptr;
Switch* flightModeSwitch = nullptr;
Button* flightModeButton = nullptr;

uint64_t lastClockUpdate = 0;
uint64_t lastBatteryUpdate = 0;
Expand Down Expand Up @@ -103,7 +109,7 @@ void applications::launcher::update() {
// Do this before updating the window (so drawing it)
// Because it can cause weird "blinking" effects

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

{
static int min;
Expand All @@ -116,17 +122,40 @@ void applications::launcher::update() {
}
}

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

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

lastBatteryUpdate = millis();
lastBattery = GSM::getBatteryLevel();
}
}

{
if(clockLabel->isTouched())
{
flightModeBox->enable();
clockLabel->disable();
dateLabel->disable();
}
if(flightModeButton->isTouched())
{
flightModeBox->disable();
clockLabel->enable();
dateLabel->enable();
}

std::cout << "launcher::update 3" << std::endl;
if(flightModeSwitch->isTouched())
{
GSM::setFlightMode(flightModeSwitch->getState());
}
}


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

if (hardware::isCharging()) {
if (chargingStartTime == 0) {
Expand All @@ -143,7 +172,20 @@ void applications::launcher::update() {
chargingPopupBox->disable();
}

libsystem::log("launcher::update -");
{
static int lastNetwork = GSM::getNetworkStatus();
if(lastNetwork != GSM::getNetworkStatus())
{
if(GSM::getNetworkStatus() == 99)
networkLabel->setText("X");
else
networkLabel->setText(std::to_string((int) GSM::getNetworkStatus() * 100 / 31) + "%");

lastNetwork = GSM::getNetworkStatus();
}
}

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

// Update, draw AND update touch events
if (launcherWindow != nullptr) {
Expand Down Expand Up @@ -229,22 +271,46 @@ void applications::launcher::draw() {

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

// Network
if (GSM::getNetworkStatus() == 99) {
auto* networkLabel = new Label(10, 10, 100, 18);
networkLabel->setText("No network");

{ // Network
networkLabel = new Label(2, 2, 30, 18);
if(GSM::getNetworkStatus() == 99)
networkLabel->setText("X");
else
networkLabel->setText(std::to_string((int) GSM::getNetworkStatus() * 100 / 31) + "%");
networkLabel->setVerticalAlignment(Label::Alignement::CENTER);
networkLabel->setHorizontalAlignment(Label::Alignement::CENTER);
networkLabel->setFontSize(18);
launcherWindow->addChild(networkLabel);
}

{ // Flight mode
flightModeBox = new Box(50, 25, 220, 59);
flightModeBox->setBorderColor(0xCF3D);
flightModeBox->setBorderSize(1);
flightModeBox->setRadius(17);
flightModeBox->disable();
launcherWindow->addChild(flightModeBox);

flightModeText = new Label(14, 8, 46, 39);
flightModeText->setText("Mode\nAvion");
flightModeText->setVerticalAlignment(Label::Alignement::CENTER);
flightModeText->setFontSize(14);
flightModeBox->addChild(flightModeText);

flightModeSwitch = new Switch(89, 19);
flightModeSwitch->setState(GSM::isFlightMode());
flightModeBox->addChild(flightModeSwitch);

flightModeButton = new Button(169, 10, 38, 38);
flightModeButton->setText("OK");
flightModeBox->addChild(flightModeButton);
}

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

// Brightness slider
brightnessSliderBox = new Box(0, 77, 50, 325);
//light->setBackgroundColor(COLOR_RED);
brightnessSliderBox = new Box(0, 77, 40, 325);
launcherWindow->addChild(brightnessSliderBox);

/**
Expand Down
19 changes: 13 additions & 6 deletions lib/gsm/src/gsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
const char *daysOfWeek[7] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
const char *daysOfMonth[12] = {"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre"};

EventHandler eventHandlerGsm;

#ifdef ESP_PLATFORM
#include <Arduino.h>

Expand Down Expand Up @@ -198,7 +200,7 @@ namespace GSM
if (!request.function)
std::cout << "request.function is invalid -> can't run the new request" << std::endl;
else
eventHandlerBack.setTimeout(new Callback<>(std::bind([](Request r){ GSM::requests.push_back(r); }, request)), 0);
eventHandlerGsm.setTimeout(new Callback<>(std::bind([](Request r){ GSM::requests.push_back(r); }, request)), 0);
}

void process()
Expand Down Expand Up @@ -1007,6 +1009,8 @@ namespace GSM
// Thanks NumWorks for the regression app
const double batteryLevel = 3.083368 * std::pow(voltage, 3) - 37.21203 * std::pow(voltage, 2) + 150.5735 * voltage - 203.3347;

std::cout << "Battery level: " << batteryLevel << std::endl;

return std::clamp(batteryLevel, 0.0, 1.0);

// if (voltage > 4.12)
Expand Down Expand Up @@ -1109,6 +1113,7 @@ namespace GSM

int getNetworkStatus()
{
std::cout << "networkQuality: " << networkQuality << std::endl;
return networkQuality;
}

Expand Down Expand Up @@ -1149,7 +1154,7 @@ namespace GSM

void setFlightMode(bool mode)
{
eventHandlerBack.setTimeout(new Callback<>([mode](){ appendRequest({updateFlightMode}); }), 0);
eventHandlerGsm.setTimeout(new Callback<>([mode](){ appendRequest({updateFlightMode}); }), 0);
flightMode = mode;
}

Expand All @@ -1166,10 +1171,10 @@ namespace GSM
onMessage();
getVoltage();

eventHandlerBack.setInterval(&GSM::getHour, 5000);
eventHandlerBack.setInterval(&GSM::getNetworkQuality, 10000);
eventHandlerBack.setInterval([](){ requests.push_back({&GSM::getVoltage, GSM::priority::normal}); }, 5000);
eventHandlerBack.setInterval([](){ requests.push_back({&GSM::onMessage, GSM::priority::normal}); }, 5000);
eventHandlerGsm.setInterval(&GSM::getHour, 5000);
eventHandlerGsm.setInterval(&GSM::getNetworkQuality, 10000);
eventHandlerGsm.setInterval([](){ requests.push_back({&GSM::getVoltage, GSM::priority::normal}); }, 5000);
eventHandlerGsm.setInterval([](){ requests.push_back({&GSM::onMessage, GSM::priority::normal}); }, 5000);

keys.push_back({"RING", &GSM::onRinging});
keys.push_back({"+CMTI:", &GSM::onMessage});
Expand All @@ -1180,6 +1185,8 @@ namespace GSM
{
PaxOS_Delay(5);

eventHandlerGsm.update();

download();

process();
Expand Down
42 changes: 13 additions & 29 deletions lib/tasks/src/standby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace StandbyMode

void trigger()
{
if(enabled == true)
return;
lastTrigger = millis();

graphics::setBrightness(graphics::brightness);
Expand All @@ -42,18 +44,23 @@ namespace StandbyMode
}
}

bool expired()
{
return millis() - lastTrigger > sleepTime;
}

void reset()
{
lastTrigger = millis();
}

void update()
{
if (!enabled && millis() - lastTrigger > sleepTime - 10000)
{
graphics::setBrightness(graphics::brightness/3 + 3);
}

if (!enabled && millis() - lastTrigger > sleepTime)
{
enable();
}

if (millis() - lastPowerTrigger > 5000)
{
if(powerMode == true)
Expand All @@ -76,30 +83,7 @@ namespace StandbyMode
void enable()
{
enabled = true;
lastTrigger = millis() - sleepTime;

graphics::setBrightness(0);
StandbyMode::savePower();

while(AppManager::isAnyVisibleApp())
{
AppManager::appStack[AppManager::appStack.size() - 1]->kill();
AppManager::appStack.pop_back();
}

while (hardware::getHomeButton());
while (!hardware::getHomeButton() && !AppManager::isAnyVisibleApp() && millis() - sleepTime > lastTrigger)
{
eventHandlerApp.update();
AppManager::loop();
}

while (hardware::getHomeButton());

StandbyMode::restorePower();
graphics::setBrightness(graphics::brightness);

disable();
lastTrigger = millis();
}

void disable()
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/src/standby.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace StandbyMode
void setSleepTime(uint64_t sleepTime);

bool state();
bool expired(); // check if it's time to sleep
void reset();

void enable();
void disable();
Expand Down
27 changes: 25 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ void mainLoop(void* data) {
AppManager::loop();
eventHandlerApp.update();

if(AppManager::isAnyVisibleApp() && launcher)
{
applications::launcher::free();
launcher = false;
}

if(libsystem::getDeviceMode() == libsystem::NORMAL && !AppManager::isAnyVisibleApp())
{
if(!launcher) // si pas de launcher -> afficher un launcher
Expand Down Expand Up @@ -100,21 +106,38 @@ void mainLoop(void* data) {
if(libsystem::getDeviceMode() == libsystem::SLEEP)
{
setDeviceMode(libsystem::NORMAL);
StandbyMode::disable();
} else if(launcher)
{
applications::launcher::free();
launcher = false;
libsystem::setDeviceMode(libsystem::SLEEP);
StandbyMode::enable();
} else if(AppManager::isAnyVisibleApp())
{
AppManager::quitApp();
}
}

std::cout << "Main loop" << std::endl;
if(libsystem::getDeviceMode() != libsystem::SLEEP && StandbyMode::expired())
{
if(launcher)
{
applications::launcher::free();
launcher = false;
}
for (uint i = 0; i < 10 && AppManager::isAnyVisibleApp(); i++) // define a limit on how many apps can be stopped (prevent from a loop)
{
AppManager::quitApp();
}
libsystem::setDeviceMode(libsystem::SLEEP);
StandbyMode::enable();
}

/*std::cout << "Main loop" << std::endl;
std::cout << "Launcher: " << launcher << std::endl;
std::cout << "Visible app: " << AppManager::isAnyVisibleApp() << std::endl;
std::cout << "Device mode: " << libsystem::getDeviceMode() << std::endl;
std::cout << "Device mode: " << libsystem::getDeviceMode() << std::endl;*/

StandbyMode::wait();
}
Expand Down

0 comments on commit 33f5cf8

Please sign in to comment.