diff --git a/extra-files/draft/icons/batteryIcon.png b/extra-files/draft/icons/batteryIcon.png new file mode 100644 index 0000000..bab563f Binary files /dev/null and b/extra-files/draft/icons/batteryIcon.png differ 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} +} +##^##*/