-
Notifications
You must be signed in to change notification settings - Fork 0
/
Init.cpp
147 lines (121 loc) · 3.19 KB
/
Init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "iostream"
#include "stdio.h"
#include "unistd.h"
#include "string"
#include "vector"
#include "fstream"
#include "boost/asio.hpp"
#include "boost/array.hpp"
#include "boost/regex.hpp"
#include "boost/algorithm/string/regex.hpp"
using namespace std;
int main(int argc,char *argv[]){
block:
try{
string port = "";
int i = 0;
while(i < 2){
FILE *file = popen("bash /.../2.sh","r");
if(file){
i = 0;
char buffer[1024];
while(!feof(file)){
if(fgets(buffer,sizeof(buffer),file) != NULL){
port = "/dev/" + boost::regex_replace(string(buffer),boost::regex("\n"),"");
i = 2;
break;
}
i++;
}
}
fclose(file);
}
boost::asio::io_service io_service;
boost::asio::serial_port serial_port(io_service,port);
boost::system::error_code error;
serial_port.set_option(boost::asio::serial_port_base::baud_rate(115200),error);
if(error){
cerr << port << endl;
throw boost::system::system_error(error);
}
sleep(2);
bool on = false;
while(true){
boost::array<char, 1> buffer;
try{
boost::system::error_code error2;
char ch;
boost::asio::read(serial_port,boost::asio::buffer(&ch,1),error2);
// string read(buffer.begin(),buffer.begin()+1);
if(ch == 'm'){
if(on == false){
system("xset -display :0.0 dpms force on");
on = true;
}
}
if(ch == 'n'){
on = false;
}
if(error2){
throw boost::system::system_error(error2);
}
}catch(exception &exception2){
serial_port.close();
cout << "Read exception" << endl;
cout << exception2.what() << endl;
goto block;
}
FILE *command = popen("/.../power_info","r");
bool charging = false;
if(command){
char buffer[1024];
while(!feof(command)){
if(fgets(buffer,sizeof(buffer),command) != NULL){
if(string(buffer).find(" charging") != string::npos){
charging = true;
}
if(string(buffer).find("percentage") != string::npos){
vector<string> split;
boost::algorithm::split_regex(split,boost::regex_replace(string(buffer),boost::regex("\\s+"),""),boost::regex(":"));
int percentage = atoi(boost::regex_replace(split.at(1),boost::regex("%"),"").c_str());
if(percentage <= 50 && !charging){
try{
boost::system::error_code error3;
boost::asio::write(serial_port,boost::asio::buffer("1"),error3);
}catch(exception &ex3){
cout << ex3.what() << endl;
goto block;
}
}
if(percentage == 100){
try{
boost::system::error_code error3;
boost::asio::write(serial_port,boost::asio::buffer("0"),error3);
}catch(exception &ex3){
cout << ex3.what() << endl;
goto block;
}
}
}
}
}
}
fclose(command);
ifstream file("/.../Arduino_command");
string r;
file >> r;
file.close();
if(strlen(r.c_str()) > 0){
boost::asio::write(serial_port,boost::asio::buffer(r.c_str(),strlen(r.c_str())));
ofstream file2("/.../Arduino_command");
file2 << "";
file2.close();
}
usleep(100000);
}
}catch(exception &ex){
cerr << "1\t" << ex.what() << endl;
goto block;
}
return 0;
}