aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2019-12-29 14:47:00 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2019-12-29 14:47:00 +0100
commit3ee478b558307b690b4839c82bb6612cc3c3f3ac (patch)
tree5d06a580dc004a16e387cb995c9772ac1b19abb2
parentoop rework in progress (diff)
downloadlowbat-gnu-3ee478b558307b690b4839c82bb6612cc3c3f3ac.tar.gz
lowbat-gnu-3ee478b558307b690b4839c82bb6612cc3c3f3ac.tar.bz2
lowbat-gnu-3ee478b558307b690b4839c82bb6612cc3c3f3ac.tar.xz
lowbat-gnu-3ee478b558307b690b4839c82bb6612cc3c3f3ac.tar.zst
lowbat-gnu-3ee478b558307b690b4839c82bb6612cc3c3f3ac.zip
work in progress
-rw-r--r--inc/jo_lowbat.hpp4
-rw-r--r--src/jo_exec.cpp15
-rw-r--r--src/jo_lowbat.cpp30
-rw-r--r--src/main.cpp61
4 files changed, 64 insertions, 46 deletions
diff --git a/inc/jo_lowbat.hpp b/inc/jo_lowbat.hpp
index 57da23a..3f70e09 100644
--- a/inc/jo_lowbat.hpp
+++ b/inc/jo_lowbat.hpp
@@ -9,9 +9,11 @@ using namespace std;
class Lowbat {
public:
uint8_t jo_testAcpi(void);
+ string jo_exec(const char*);
+ string jo_fetchBatlvl(void);
int jo_notify(const string);
int jo_speak(const string);
- string jo_exec(const char*);
+
private:
};
diff --git a/src/jo_exec.cpp b/src/jo_exec.cpp
index 04efb66..bbee5bb 100644
--- a/src/jo_exec.cpp
+++ b/src/jo_exec.cpp
@@ -1,17 +1,2 @@
#include <jo_lowbat.hpp>
-#include <array>
-#include <memory>
-string
-jo_exec(const char* cmd) {
- array<char, 128> buffer;
- string result;
- unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
- if (!pipe) {
- throw runtime_error("popen() failed!");
- }
- while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
- result += buffer.data();
- }
- return result;
-}
diff --git a/src/jo_lowbat.cpp b/src/jo_lowbat.cpp
index 56fe655..ab74eaf 100644
--- a/src/jo_lowbat.cpp
+++ b/src/jo_lowbat.cpp
@@ -1,5 +1,8 @@
#include <jo_lowbat.hpp>
#include <iostream>
+#include <array>
+#include <memory>
+#include <algorithm>
using namespace std;
@@ -11,3 +14,30 @@ Lowbat::jo_testAcpi(void) {
}
return 0;
}
+
+string
+jo_exec(const char* cmd) {
+ array<char, 128> buffer;
+ string result;
+ unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
+ if (!pipe) {
+ throw runtime_error("popen() failed!");
+ }
+ while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
+ result += buffer.data();
+ }
+ return result;
+}
+
+string
+Lowbat::jo_fetchBatlvl(void) {
+ string batlvl;
+ uint8_t batlvlint;
+
+ cout << "Fetching batlvl: ";
+ batlvl = this->jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
+ batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end());
+ batlvlint = stoi(batlvl);
+ cout << batlvlint << "%" << endl;
+ return (batlvl);
+}
diff --git a/src/main.cpp b/src/main.cpp
index 62c20b8..c614cca 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,7 +3,6 @@
#include <cstring>
#include <thread>
#include <chrono>
-#include <algorithm>
using namespace this_thread;
using namespace chrono;
@@ -13,40 +12,42 @@ int main(int argc, const char *argv[]) {
string acstat;
string batlvl;
int batlvlint;
+ (void)argc;
+ (void)argv;
+ (void)msg;
+ (void)batlvl;
+ (void)batlvlint;
+ (void)acstat;
Lowbat lowbat;
if (lowbat.jo_testAcpi())
return (1);
while (true) {
- cout << "Fetching batlvl: ";
- batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
- batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end());
- batlvlint = stoi(batlvl);
- cout << batlvlint << "%" << endl;
- while (batlvlint < 15 && !system("acpi | grep -q Discharging")) {
- jo_notify(batlvl);
- if (argc == 1) {
- msg = new string("beep beep - low battery");
- jo_speak(*msg);
- delete msg;
- }
- else if (argc > 1 && strcmp(argv[1], "--silent")) {
- if (argc > 2 && !strcmp(argv[1], "--say")) {
- msg = new string(argv[2]);
- }
- else {
- msg = new string("beep beep - low battery");
- }
- jo_speak(*msg);
- delete msg;
- }
- cout << "Sleep for 20s" << endl;
- sleep_for(seconds(20));
- batlvl.clear();
- batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
- batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end());
- batlvlint = stoi(batlvl);
- }
+ lowbat.jo_fetchBatlvl();
+ // while (batlvlint < 15 && !system("acpi | grep -q Discharging")) {
+ // jo_notify(batlvl);
+ // if (argc == 1) {
+ // msg = new string("beep beep - low battery");
+ // jo_speak(*msg);
+ // delete msg;
+ // }
+ // else if (argc > 1 && strcmp(argv[1], "--silent")) {
+ // if (argc > 2 && !strcmp(argv[1], "--say")) {
+ // msg = new string(argv[2]);
+ // }
+ // else {
+ // msg = new string("beep beep - low battery");
+ // }
+ // jo_speak(*msg);
+ // delete msg;
+ // }
+ // cout << "Sleep for 20s" << endl;
+ // sleep_for(seconds(20));
+ // batlvl.clear();
+ // batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
+ // batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end());
+ // batlvlint = stoi(batlvl);
+ // }
cout << "Sleep for 4m" << endl;
sleep_for(seconds(240));
}