-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIOClass.cpp
187 lines (145 loc) · 4.53 KB
/
GPIOClass.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Source: https://github.com/halherta/RaspberryPi-GPIOClass-v2
#include "GPIOClass.h"
using namespace std;
GPIOClass::GPIOClass():valuefd(-1),directionfd(-1),exportfd(-1),unexportfd(-1),gpionum("4")
{
//GPIO4 is default
this->export_gpio();
}
GPIOClass::GPIOClass(string gnum):valuefd(-1),directionfd(-1),exportfd(-1),unexportfd(-1),gpionum(gnum)
{
//Instatiate GPIOClass object for GPIO pin number "gnum"
this->export_gpio();
}
GPIOClass::~GPIOClass()
{
this->unexport_gpio();
}
int GPIOClass::export_gpio()
{
int statusVal = -1;
string exportStr = "/sys/class/gpio/export";
this->exportfd = statusVal = open(exportStr.c_str(), O_WRONLY|O_SYNC);
if (statusVal < 0){
perror("could not open SYSFS GPIO export device");
exit(1);
}
stringstream ss;
ss << this->gpionum;
string numStr = ss.str();
statusVal = write(this->exportfd, numStr.c_str(), numStr.length());
if (statusVal < 0){
perror("could not write to SYSFS GPIO export device");
exit(1);
}
statusVal = close(this->exportfd);
if (statusVal < 0){
perror("could not close SYSFS GPIO export device");
exit(1);
}
return statusVal;
}
int GPIOClass::unexport_gpio()
{
int statusVal = -1;
string unexportStr = "/sys/class/gpio/unexport";
this->unexportfd = statusVal = open(unexportStr.c_str(), O_WRONLY|O_SYNC);
if (statusVal < 0){
perror("could not open SYSFS GPIO unexport device");
exit(1);
}
stringstream ss;
ss << this->gpionum;
string numStr = ss.str();
statusVal = write(this->unexportfd, numStr.c_str(), numStr.length());
if (statusVal < 0){
perror("could not write to SYSFS GPIO unexport device");
exit(1);
}
statusVal = close(this->unexportfd);
if (statusVal < 0){
perror("could not close SYSFS GPIO unexport device");
exit(1);
}
return statusVal;
}
int GPIOClass::setdir_gpio(string dir)
{
int statusVal = -1;
string setdirStr ="/sys/class/gpio/gpio" + this->gpionum + "/direction";
this->directionfd = statusVal = open(setdirStr.c_str(), O_WRONLY|O_SYNC); // open direction file for gpio
if (statusVal < 0){
perror("could not open SYSFS GPIO direction device");
exit(1);
}
if (dir.compare("in") != 0 && dir.compare("out") != 0 ) {
fprintf(stderr, "Invalid direction value. Should be \"in\" or \"out\". \n");
exit(1);
}
statusVal = write(this->directionfd, dir.c_str(), dir.length());
if (statusVal < 0){
perror("could not write to SYSFS GPIO direction device");
exit(1);
}
statusVal = close(this->directionfd);
if (statusVal < 0){
perror("could not close SYSFS GPIO direction device");
exit(1);
}
return statusVal;
}
int GPIOClass::setval_gpio(string val)
{
int statusVal = -1;
string setValStr = "/sys/class/gpio/gpio" + this->gpionum + "/value";
this->valuefd = statusVal = open(setValStr.c_str(), O_WRONLY|O_SYNC);
if (statusVal < 0){
perror("could not open SYSFS GPIO value device");
exit(1);
}
if (val.compare("1") != 0 && val.compare("0") != 0 ) {
fprintf(stderr, "Invalid value. Should be \"1\" or \"0\". \n");
exit(1);
}
statusVal = write(this->valuefd, val.c_str(), val.length());
if (statusVal < 0){
perror("could not write to SYSFS GPIO value device");
exit(1);
}
statusVal = close(this->valuefd);
if (statusVal < 0){
perror("could not close SYSFS GPIO value device");
exit(1);
}
return statusVal;
}
int GPIOClass::getval_gpio(string& val){
string getValStr = "/sys/class/gpio/gpio" + this->gpionum + "/value";
char buff[10];
int statusVal = -1;
this->valuefd = statusVal = open(getValStr.c_str(), O_RDONLY|O_SYNC);
if (statusVal < 0){
perror("could not open SYSFS GPIO value device");
exit(1);
}
statusVal = read(this->valuefd, &buff, 1);
if (statusVal < 0){
perror("could not read SYSFS GPIO value device");
exit(1);
}
buff[1]='\0';
val = string(buff);
if (val.compare("1") != 0 && val.compare("0") != 0 ) {
fprintf(stderr, "Invalid value read. Should be \"1\" or \"0\". \n");
exit(1);
}
statusVal = close(this->valuefd);
if (statusVal < 0){
perror("could not close SYSFS GPIO value device");
exit(1);
}
return statusVal;
}
string GPIOClass::get_gpionum(){
return this->gpionum;
}