From f9c188c8a5a07b1610591f137491c27360992c8c Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Fri, 23 Apr 2021 19:16:28 -0400 Subject: [PATCH] Implement battery alerts in Reader framework --- alert.cpp | 2 +- functions.h | 1 + main.cpp | 4 ++ mainwindow.cpp | 1 + reader.cpp | 126 +++++++++++++++++++++++++++++-------------------- reader.h | 1 + 6 files changed, 84 insertions(+), 51 deletions(-) diff --git a/alert.cpp b/alert.cpp index 5494e32..8e5bf91 100644 --- a/alert.cpp +++ b/alert.cpp @@ -60,7 +60,7 @@ alert::alert(QWidget *parent) : ui->warningLabel->setText("Please charge your eReader."); ui->securityLabel->setText("The battery's charge level is critical."); ui->messageLabel->setText("To prevent filesystem damage, your device has been turned off.\nPlease consider charging it."); - poweroff(false); + //poweroff(false); } ui->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}"); diff --git a/functions.h b/functions.h index ed64041..a8cf713 100644 --- a/functions.h +++ b/functions.h @@ -20,6 +20,7 @@ namespace global { inline int pageNumber; inline int bookNumber; inline bool skipOpenDialog; + inline bool startBatteryWatchdog; } namespace mainwindow { inline bool updateDialog; diff --git a/main.cpp b/main.cpp index 71261ef..a5c9af4 100644 --- a/main.cpp +++ b/main.cpp @@ -58,6 +58,10 @@ int main(int argc, char *argv[]) } // If we're waking from sleep and we have the lockscreen enabled, we'll "resume" the book from scratch if(checkconfig("/tmp/suspendBook") == true) { + // Start the low/critical battery alert timer from the Reader framework since MainWindow is not going to be shown + global::reader::startBatteryWatchdog = true; + global::reader::skipOpenDialog = true; + string_writeconfig("/inkbox/skip_opendialog", "true"); QApplication a(argc, argv); reader w; diff --git a/mainwindow.cpp b/mainwindow.cpp index 43527c5..6bfb0d9 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -294,6 +294,7 @@ MainWindow::MainWindow(QWidget *parent) t->start(); } + // Battery watchdog QTimer *batteryWatchdog = new QTimer(this); batteryWatchdog->setInterval(2000); connect(batteryWatchdog, &QTimer::timeout, [&]() { diff --git a/reader.cpp b/reader.cpp index 08cbe2d..3e2cdf4 100644 --- a/reader.cpp +++ b/reader.cpp @@ -24,6 +24,7 @@ reader::reader(QWidget *parent) : { // Variables global::battery::showLowBatteryDialog = true; + global::battery::showCriticalBatteryAlert = true; ui->setupUi(this); ui->previousBtn->setProperty("type", "borderless"); @@ -218,20 +219,6 @@ reader::reader(QWidget *parent) : get_battery_level(); ui->batteryLabel->setText(batt_level); ui->timeLabel->setText(time); - if(global::battery::showLowBatteryDialog != true) { - // Do nothing, since a dialog should already have been displayed and (probably) dismissed - ; - } - else { - if(global::battery::batteryAlertLock == true) { - ; - } - else { - if(isBatteryLow() == true) { - openLowBatteryDialog(); - } - } - } } ); t->start(); } @@ -243,20 +230,6 @@ reader::reader(QWidget *parent) : get_battery_level(); ui->batteryLabel->setText(batt_level); ui->timeLabel->setText(time); - if(global::battery::showLowBatteryDialog != true) { - // Do nothing, since a dialog should already have been displayed and (probably) dismissed - ; - } - else { - if(global::battery::batteryAlertLock == true) { - ; - } - else { - if(isBatteryLow() == true) { - openLowBatteryDialog(); - } - } - } } ); t->start(); } @@ -301,28 +274,30 @@ reader::reader(QWidget *parent) : select_t->start(); // We have to get the file's path - if(checkconfig("/tmp/suspendBook") == true) { - wakeFromSleep = true; - // Prevent from opening the Reader framework next time unless the condition is reset - string_writeconfig("/tmp/suspendBook", "false"); - book_file = "/inkbox/book/book.txt"; - } if(global::reader::skipOpenDialog == true) { - if(global::reader::bookNumber == 1) { - string_checkconfig(".config/08-recent_books/1"); - book_file = checkconfig_str_val; + if(checkconfig("/tmp/suspendBook") == true) { + wakeFromSleep = true; + // Prevent from opening the Reader framework next time unless the condition is reset + string_writeconfig("/tmp/suspendBook", "false"); + book_file = "/inkbox/book/book.txt"; } - if(global::reader::bookNumber == 2) { - string_checkconfig(".config/08-recent_books/2"); - book_file = checkconfig_str_val; - } - if(global::reader::bookNumber == 3) { - string_checkconfig(".config/08-recent_books/3"); - book_file = checkconfig_str_val; - } - if(global::reader::bookNumber == 4) { - string_checkconfig(".config/08-recent_books/4"); - book_file = checkconfig_str_val; + else { + if(global::reader::bookNumber == 1) { + string_checkconfig(".config/08-recent_books/1"); + book_file = checkconfig_str_val; + } + if(global::reader::bookNumber == 2) { + string_checkconfig(".config/08-recent_books/2"); + book_file = checkconfig_str_val; + } + if(global::reader::bookNumber == 3) { + string_checkconfig(".config/08-recent_books/3"); + book_file = checkconfig_str_val; + } + if(global::reader::bookNumber == 4) { + string_checkconfig(".config/08-recent_books/4"); + book_file = checkconfig_str_val; + } } } else { @@ -471,6 +446,47 @@ reader::reader(QWidget *parent) : string_writeconfig(".config/08-recent_books/3", str_book_2); string_writeconfig(".config/08-recent_books/4", str_book_3); } + + // Battery watchdog + if(global::reader::startBatteryWatchdog == true) { + QTimer *t = new QTimer(this); + t->setInterval(2000); + connect(t, &QTimer::timeout, [&]() { + // Checking if battery level is low + if(global::battery::showCriticalBatteryAlert != true) { + ; + } + else { + if(isBatteryCritical() == true) { + qDebug() << "Warning! Battery is at a critical charge level!"; + openCriticalBatteryAlertWindow(); + } + } + + if(global::battery::showLowBatteryDialog != true) { + // Do nothing, since a dialog should already have been displayed and (probably) dismissed + ; + } + else { + if(isBatteryLow() == true) { + if(global::battery::batteryAlertLock == true) { + ; + } + else { + qDebug() << "Warning! Battery is low!"; + string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status"); + if(checkconfig_str_val == "Charging\n") { + ; + } + else { + openLowBatteryDialog(); + } + } + } + } + } ); + t->start(); + } } reader::~reader() @@ -1065,11 +1081,21 @@ void reader::quit_restart() { } void reader::openLowBatteryDialog() { - global::battery::batteryAlertLock = true; - global::battery::showLowBatteryDialog = false; global::mainwindow::lowBatteryDialog = true; + global::battery::batteryAlertLock = true; generalDialogWindow = new generalDialog(this); generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose); generalDialogWindow->show(); + QApplication::processEvents(); +} + +void reader::openCriticalBatteryAlertWindow() { + global::battery::showCriticalBatteryAlert = true; + global::battery::showLowBatteryDialog = false; + + alertWindow = new alert(); + alertWindow->setAttribute(Qt::WA_DeleteOnClose); + alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size())); + alertWindow->show(); } diff --git a/reader.h b/reader.h index 4ef7ba9..a586818 100644 --- a/reader.h +++ b/reader.h @@ -68,6 +68,7 @@ public: void wordwidget_show(); void wordwidget_hide(); void openLowBatteryDialog(); + void openCriticalBatteryAlertWindow(); private slots: void on_nextBtn_clicked();