forked from halherta/RaspberryPi-GPIOClass-v2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPIOClass.cpp
189 lines (147 loc) · 4.85 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
188
189
#include "GPIOClass.h"
#include <exception>
#include <stdexcept>
using namespace std;
GPIOClass::GPIOClass():valuefd(-1),directionfd(-1),exportfd(-1),unexportfd(-1),gpionum("4")
{
//GPIO4 is default
try {
this->unexport_gpio();
}
catch (runtime_error&) {};
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"
try {
this->unexport_gpio();
}
catch (runtime_error&) {};
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){
throw runtime_error("GPIOClass::export_gpio: 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){
throw runtime_error("GPIOClass::export_gpio: could not write to SYSFS GPIO export device");
//exit(1);
}
statusVal = close(this->exportfd);
if (statusVal < 0){
throw runtime_error("GPIOClass::export_gpio: 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){
throw runtime_error("GPIOClass::uexport_gpio: 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){
throw runtime_error("GPIOClass::uexport_gpio: could not write to SYSFS GPIO unexport device");
//exit(1);
}
statusVal = close(this->unexportfd);
if (statusVal < 0){
throw runtime_error("GPIOClass::uexport_gpio: 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){
throw runtime_error("GPIOClass::setdir_gpio: 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");
throw runtime_error("GPIOClass::setdir_gpio: Invalid direction value. Shoudl be \"in\" or \"out\".");
//exit(1);
}
statusVal = write(this->directionfd, dir.c_str(), dir.length());
if (statusVal < 0){
throw runtime_error("GPIOClass::setdir_gpio: could not write to SYSFS GPIO direction device");
//exit(1);
}
statusVal = close(this->directionfd);
if (statusVal < 0){
throw runtime_error("GPIOClass::setdir_gpio: 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){
throw runtime_error("GPIOClass::setval_gpio: could not open SYSFS GPIO value device");
}
if (val.compare("1") != 0 && val.compare("0") != 0 ) {
throw runtime_error("GPIOClass::setval_gpio: invalid value - Should be 1 or 0");
}
statusVal = write(this->valuefd, val.c_str(), val.length());
if (statusVal < 0){
throw runtime_error("GPIOClass::setval_gpio: could not write to SYSFS GPIO value device");
}
statusVal = close(this->valuefd);
if (statusVal < 0){
throw runtime_error("GPIOClass::setval_gpio: could not close SYSFS GPIO value device");
}
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){
throw runtime_error("GPIOClass::getval_gpio: could not open SYSFS GPIO value device");
}
statusVal = read(this->valuefd, &buff, 1);
if (statusVal < 0){
throw runtime_error("GPIOClass::getval_gpio: could not read SYSFS GPIO value device");
}
buff[1]='\0';
val = string(buff);
if (val.compare("1") != 0 && val.compare("0") != 0 ) {
throw runtime_error("GPIOClass::getval_gpio: Ivalid value read");
}
statusVal = close(this->valuefd);
if (statusVal < 0){
throw runtime_error("GPIOClass::getval_gpio: could not close SYSFS GPIO value device");
}
return statusVal;
}
string GPIOClass::get_gpionum(){
return this->gpionum;
}