-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.cpp
45 lines (38 loc) · 1.09 KB
/
gpio.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
#include "gpio.h"
using namespace std;
string pastaGpio = "/sys/class/gpio";
int greenID = 66;
int yellowID = 69;
int redID = 45;
int buttonID = 47;
bool getGpioValue(int id){
string result = run("cat "+pastaGpio+"/gpio"+to_string(id)+"/value");
//cout << "run(" << "cat "+pastaGpio+"/gpio"+to_string(id)+"/value" << ") -> " << result << endl;
int res = stoi(result);
//cout << res << endl;
return res != 0;
}
string getGpioDirection(int id){
string result = run("cat "+pastaGpio+"/gpio"+to_string(id)+"/direction");
return result;
}
void setGpioDirection(int id, string val){
if(val == "in" || val == "out"){
run("echo " + val + " > " + pastaGpio + "/gpio"+to_string(id)+"/direction");
}
}
void setGpioValue(int id, bool val){
setGpioDirection(id, "out");
string newVal;
if(val) {
newVal = "1";
}
else{
newVal = "0";
}
//cout << "run(" << "echo " << newVal << " > " << pastaGpio << "/gpio" << to_string(id) << "/value)" << endl;
run("echo " + newVal + " > " + pastaGpio + "/gpio"+to_string(id)+"/value");
}
void exportGpio(int id){
run("echo "+to_string(id)+" > "+pastaGpio+"/export");
}