Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added extra-files/draft/icons/batteryIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

time_t lastReturn;
int qSize = 1404;
int gotEvents = 0;

Handler::Handler(std::string lT, std::string term, QGuiApplication* app, MainView* mainView, QObject* obj)
: link(lT), term(term), ef(obj), app(app), mainView(mainView){
Expand Down Expand Up @@ -40,6 +41,7 @@ bool Handler::eventFilter(QObject* obj, QEvent* event)
}

void Handler::handleEvent() {
gotEvents = 1;

std::cout << "Setting termfile /etc/draft/.terminate" << std::endl;
std::ofstream termfile;
Expand Down
2 changes: 2 additions & 0 deletions handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <time.h>

extern time_t lastReturn;
extern int gotEvents;
extern int qSize;

class Handler : public QObject
{
Expand Down
141 changes: 135 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,117 @@
#include <QtQuick>
//#include <QtGui>
//#include <QtPlugin>
#include <QTimer>
#include <QDebug>
#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())
);
}


void suspend() {
qDebug() << "Suspending";
system("systemctl suspend");
gotEvents+=1;
}


void restoreSuspendBanner() {
QQuickItem* root = VIEW->rootObject();
root->setProperty(
"suspend_banner_text",
QVariant("")
);
}


void suspendIfInactive() {
qDebug() << "Checking if it should suspend...";
QQuickItem* root = VIEW->rootObject();
if (gotEvents != 0) {
root->setProperty(
"suspend_banner_text",
QVariant("")
);
gotEvents = 0;
qDebug() << "Not suspending!";
return;
}

qDebug() << "Suspending!";
root->setProperty(
"suspend_banner_text",
QVariant("Suspended, press any button to continue.")
);
// Give it time to show the banner
qDebug() << "waiting for the banner to appear...";
qSize = 1-(qSize-1403)+1403;
root->setProperty("width",QVariant(qSize));
QTimer *wait_for_timer = new QTimer();
// Give it time to show the banner
wait_for_timer->singleShot(
1000,
VIEW,
QOverload<>::of(suspend)
);
wait_for_timer->singleShot(
2000,
VIEW,
QOverload<>::of(restoreSuspendBanner)
);
// after returning from sleep, refresh the battery
wait_for_timer->singleShot(
3000,
VIEW,
QOverload<>::of(setBatteryLevel)
);
}


int main(int argc, char *argv[])
{
qputenv("QMLSCENE_DEVICE", "epaper");
Expand All @@ -15,18 +120,42 @@ 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 *battery_timer = new QTimer();
battery_timer->connect(
battery_timer,
&QTimer::timeout,
VIEW,
QOverload<>::of(setBatteryLevel)
);
// update the battery level every 10min
battery_timer->start(600000);

QTimer *suspend_timer = new QTimer();
suspend_timer->connect(
suspend_timer,
&QTimer::timeout,
VIEW,
QOverload<>::of(suspendIfInactive)
);
// check if we should suspend every 20 min
suspend_timer->start(1200000);

view.show();
return app.exec();
}
2 changes: 2 additions & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <QGuiApplication>
#include "mainview.h"

const extern std::string configDir;

struct OptionItem {
std::string name;
std::string desc;
Expand Down
61 changes: 61 additions & 0 deletions qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import QtQuick 2.0
import "../js/Main.js" as MainJS

Rectangle {
property alias battery_level : batteryLevel.text
property alias battery_icon : batteryIcon.source
property alias suspend_banner_text : suspendBanner.text

id: canvas
width: 1404
height: 1872
Expand All @@ -26,6 +30,22 @@ Rectangle {
font.italic:true
}

Text {
id: suspendBanner
x: 390
y: 0
width: 625
height: 24
anchors.rightMargin: 390
anchors.bottomMargin: 1848
font.family: "Noto Serif"
anchors.bottom: parent.bottom
font.pixelSize: 30
font.italic: true
anchors.margins: {bottom:10}
anchors.right: parent.right
}

Text {
id: credit
anchors.right:parent.right
Expand All @@ -37,6 +57,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
Expand Down Expand Up @@ -84,3 +139,9 @@ Rectangle {
}

}

/*##^##
Designer {
D{i:0;formeditorZoom:0.8999999761581421}
}
##^##*/