From 4946e1f44e89b07714797291f3de78c2fdda1754 Mon Sep 17 00:00:00 2001 From: David Caro Date: Sun, 19 Jul 2020 21:35:53 +0200 Subject: [PATCH] [main] Add battery indicator and refresh This adds a simple text battery indicator that is refreshed every 10 minutes to keep track of the battery level of the device. Signed-off-by: David Caro --- extra-files/draft/icons/batteryIcon.png | Bin 0 -> 3087 bytes main.cpp | 72 ++++++++++++++++++++++-- options.h | 2 + qml/Main.qml | 44 +++++++++++++++ 4 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 extra-files/draft/icons/batteryIcon.png diff --git a/extra-files/draft/icons/batteryIcon.png b/extra-files/draft/icons/batteryIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..bab563f77d023b4c211bc2e93f70d2be8610a723 GIT binary patch literal 3087 zcmcgu`8U)H8~=Xi%P?dMg9*uUZI!*OS#C0@WRPtzaR%=X|JVPTPqHVT1qx5V6M| z#{mGi3qe2-xm)n0s=(cXB4Rxw06_HlKMCe&i|zve;ST%b)~>MypC&Kf!n=XjKUY5c zt#N3uM*5xu6CIFn07=dFN&#$?q(vkIRnbO*65HqA$QpJj9Z1_q);zS_yKIl!~_GOn@eVIryl^Vy%I$%(}V3%-J1Gd6F~D9NzO-Jd;?5!D}~B_9N|pB(}%i?{p=?~SX5WkEsu z$3wZr)-fl!SZ)yfN^QuRrm7>rK^8*G%9PIAY$0+j9L+USQ}Cm?R+C-G?obP)bFe8w z4rJyKACriv?p)GkL!K9R{-7Q!WCA2=P4CRo^!B^6UwflkP%#7ZBtikm`-OX!D;;27 zakni1Rf<|!{+2g`K^a36V0+@p?mLmNIT2xDLy*5JK!e?R@AKIDA_-ZFc~dhiz7Yie zREC|Fy(HI-lDo0CEau{eI4Mcn=~g?ISvK1w7$dMV_+9B+%a_gFIYtQze_}YUsDWQs zWCGQGXj)HIq*7^*)EF4z4X97Q(s4TlZQ$omG0;|$EPi7P=kGH|*!s?H0qQlTLfk$W zq9X_v{#U{oFNC}(Q6pZI_<}Z6D7|m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m$M2d z@@WTWm%dTj1oZkxRTIyJl7qL7>_>3 zmYhd&sgzkqOkYc|*r=t(v~?lmi;~ad$=*(Dlo>g}YAS4B*b;fi5y}2j-+kb%zSAgz ztg93(O>}rSgY}xSL}l%a4}VvR0)Q=)aSAW;fc>n7pd@v9WrryLcs_zMo7FCr&5khQ zRlnGuS;`OaRC!{Z`aV+)ce*|v;i^O$ZJTvT13TswX470Av>N zR`2k0ePj{13uY(<-p@}h&x|3!#_qWfrSd@mzVmZAenPmQOJ6%O=Q08f?-#st?Q{Uo zF4-Q_E85xGw1k~#Fnuq?4dhx-x|JQhS`n;t9-$z=IzqikVuL?q&b+O4w!uQQ;90ic zq8kgDjA2Q{@)=`ffSfdEU>FJ_5=NP0H1M#>4lVW4r8}%iYY(H~cU?nad{3@gbLX04 z#|5_T>h?sxP)~Zh@}eK`P6o<3e@LrDD(A0ulf literal 0 HcmV?d00001 diff --git a/main.cpp b/main.cpp index afb5ae0..1954948 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,62 @@ #include +#include +#include //#include //#include +#include +#include #include "mainview.h" #include "options.h" #include "handler.h" Q_IMPORT_PLUGIN(QsgEpaperPlugin) +MainView *VIEW; +int _qSize = 1404; + + +int readInt(std::string path) { + + QFile file(path.c_str()); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qDebug() << "Couldn't find the file " << path.c_str(); + return 0; + } + QTextStream in(&file); + std::string text = in.readLine().toStdString(); + int number = std::stoi(text); + + return number; +} + + +std::string getBatteryLevel() { + int charge_now = readInt( + "/sys/class/power_supply/bq27441-0/charge_now" + ); + int charge_full = readInt( + "/sys/class/power_supply/bq27441-0/charge_full_design" + ); + + int battery_level = 100; + if (charge_now < charge_full) { + battery_level = (charge_now * 100/ charge_full); + } + qDebug() << "Got battery level " << battery_level; + return std::to_string(battery_level) + "%"; +} + + +void setBatteryLevel() { + std::string battery_level = getBatteryLevel(); + QQuickItem* root = VIEW->rootObject(); + root->setProperty( + "battery_level", + QVariant(getBatteryLevel().c_str()) + ); +} + + int main(int argc, char *argv[]) { qputenv("QMLSCENE_DEVICE", "epaper"); @@ -15,18 +65,32 @@ int main(int argc, char *argv[]) system("/usr/bin/button-capture &"); - QGuiApplication app(argc, argv); - MainView view; srand(time(NULL)); + QGuiApplication app(argc, argv); + MainView view; + VIEW = &view; view.rootContext()->setContextProperty("screenGeometry", app.primaryScreen()->geometry()); view.engine()->addImportPath(QStringLiteral(DEPLOYMENT_PATH)); view.setSource(QDir(DEPLOYMENT_PATH).filePath("qml/Main.qml")); - Options options(&view, &app); + Options options(VIEW, &app); - view.show(); + QQuickItem* root = view.rootObject(); + root->setProperty("battery_icon",QVariant(("file://"+configDir+"/icons/batteryIcon.png").c_str())); + setBatteryLevel(); + QTimer *timer = new QTimer(); + timer->connect( + timer, + &QTimer::timeout, + VIEW, + QOverload<>::of(setBatteryLevel) + ); + // update the battery level every 10min + timer->start(5000); + + view.show(); return app.exec(); } diff --git a/options.h b/options.h index 2e21372..8ad9c5a 100644 --- a/options.h +++ b/options.h @@ -6,6 +6,8 @@ #include #include "mainview.h" +const extern std::string configDir; + struct OptionItem { std::string name; std::string desc; diff --git a/qml/Main.qml b/qml/Main.qml index 32e6864..bf9ba05 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -2,6 +2,9 @@ import QtQuick 2.0 import "../js/Main.js" as MainJS Rectangle { + property alias battery_level : batteryLevel.text + property alias battery_icon : batteryIcon.source + id: canvas width: 1404 height: 1872 @@ -37,6 +40,41 @@ Rectangle { font.italic:true } + Image { + x: 19 + y: 1837 + id: batteryIcon + fillMode: Image.PreserveAspectFit + height: 10 + width: 50 + anchors.rightMargin: 1335 + anchors.bottomMargin: 5 + anchors.topMargin: 1841 + anchors.top: parent.top + anchors.right: parent.right + anchors.bottom:parent.bottom + anchors.margins:15 + source: "../extra-files/draft/icons/batteryIcon.png" + } + + Text { + id: batteryLevel + x: 75 + y: 1828 + width: 428 + height: 36 + text: "Unable to read battery level" + anchors.rightMargin: 901 + anchors.bottomMargin: 8 + font.family: "Noto Serif" + anchors.bottom: parent.bottom + font.pixelSize: 30 + font.italic: true + anchors.margins: {bottom:10} + anchors.right: parent.right + } + + Text { id: optionsHeading anchors.left:parent.left @@ -84,3 +122,9 @@ Rectangle { } } + +/*##^## +Designer { + D{i:0;formeditorZoom:0.8999999761581421} +} +##^##*/