-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPIO.cpp
More file actions
279 lines (251 loc) · 7.45 KB
/
Copy pathGPIO.cpp
File metadata and controls
279 lines (251 loc) · 7.45 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* GPIO.cpp Created on: 29 Apr 2014
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring BeagleBone"
* If you use this code in your work please cite:
* Derek Molloy, "Exploring BeagleBone: Tools and Techniques for Building
* with Embedded Linux", Wiley, 2014, ISBN:9781118935125.
* See: www.exploringbeaglebone.com
* Licensed under the EUPL V.1.1
*
* This Software is provided to You under the terms of the European
* Union Public License (the "EUPL") version 1.1 as published by the
* European Union. Any use of this Software, other than as authorized
* under this License is strictly prohibited (to the extent such use
* is covered by a right of the copyright holder of this Software).
*
* This Software is provided under the License on an "AS IS" basis and
* without warranties of any kind concerning the Software, including
* without limitation merchantability, fitness for a particular purpose,
* absence of defects or errors, accuracy, and non-infringement of
* intellectual property rights other than copyright. This disclaimer
* of warranty is an essential part of the License and a condition for
* the grant of any rights to this Software.
*
* For more details, see http://www.derekmolloy.ie/
*/
#include "GPIO.h"
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cstdio>
#include<fcntl.h>
#include<unistd.h>
#include<sys/epoll.h>
#include<pthread.h>
using namespace std;
namespace exploringBB {
/**
*
* @param number The GPIO number for the BBB
*/
GPIO::GPIO(int number) {
this->number = number;
this->debounceTime = 0;
this->togglePeriod=100;
this->toggleNumber=-1; //infinite number
this->callbackFunction = NULL;
this->threadRunning = false;
ostringstream s;
s << "gpio" << number;
this->name = string(s.str());
this->path = GPIO_PATH + this->name + "/";
this->exportGPIO();
// need to give Linux time to set up the sysfs structure
usleep(250000); // 250ms delay
}
int GPIO::write(string path, string filename, string value){
ofstream fs;
fs.open((path + filename).c_str());
if (!fs.is_open()){
perror("GPIO: write failed to open file ");
return -1;
}
fs << value;
fs.close();
return 0;
}
string GPIO::read(string path, string filename){
ifstream fs;
fs.open((path + filename).c_str());
if (!fs.is_open()){
perror("GPIO: read failed to open file ");
}
string input;
getline(fs,input);
fs.close();
return input;
}
int GPIO::write(string path, string filename, int value){
stringstream s;
s << value;
return this->write(path,filename,s.str());
}
int GPIO::exportGPIO(){
return this->write(GPIO_PATH, "export", this->number);
}
int GPIO::unexportGPIO(){
return this->write(GPIO_PATH, "unexport", this->number);
}
int GPIO::setDirection(GPIO_DIRECTION dir){
switch(dir){
case INPUT: return this->write(this->path, "direction", "in");
break;
case OUTPUT:return this->write(this->path, "direction", "out");
break;
}
return -1;
}
int GPIO::setValue(GPIO_VALUE value){
switch(value){
case HIGH: return this->write(this->path, "value", "1");
break;
case LOW: return this->write(this->path, "value", "0");
break;
}
return -1;
}
int GPIO::setEdgeType(GPIO_EDGE value){
switch(value){
case NONE: return this->write(this->path, "edge", "none");
break;
case RISING: return this->write(this->path, "edge", "rising");
break;
case FALLING: return this->write(this->path, "edge", "falling");
break;
case BOTH: return this->write(this->path, "edge", "both");
break;
}
return -1;
}
int GPIO::setActiveLow(bool isLow){
if(isLow) return this->write(this->path, "active_low", "1");
else return this->write(this->path, "active_low", "0");
}
int GPIO::setActiveHigh(){
return this->setActiveLow(false);
}
GPIO_VALUE GPIO::getValue(){
string input = this->read(this->path, "value");
if (input == "0") return LOW;
else return HIGH;
}
GPIO_DIRECTION GPIO::getDirection(){
string input = this->read(this->path, "direction");
if (input == "in") return INPUT;
else return OUTPUT;
}
GPIO_EDGE GPIO::getEdgeType(){
string input = this->read(this->path, "edge");
if (input == "rising") return RISING;
else if (input == "falling") return FALLING;
else if (input == "both") return BOTH;
else return NONE;
}
int GPIO::streamOpen(){
stream.open((path + "value").c_str());
return 0;
}
int GPIO::streamWrite(GPIO_VALUE value){
stream << value << std::flush;
return 0;
}
int GPIO::streamClose(){
stream.close();
return 0;
}
int GPIO::toggleOutput(){
this->setDirection(OUTPUT);
if ((bool) this->getValue()) this->setValue(LOW);
else this->setValue(HIGH);
return 0;
}
int GPIO::toggleOutput(int time){ return this->toggleOutput(-1, time); }
int GPIO::toggleOutput(int numberOfTimes, int time){
this->setDirection(OUTPUT);
this->toggleNumber = numberOfTimes;
this->togglePeriod = time;
this->threadRunning = true;
if(pthread_create(&this->thread, NULL, &threadedToggle, static_cast<void*>(this))){
perror("GPIO: Failed to create the toggle thread");
this->threadRunning = false;
return -1;
}
return 0;
}
// This thread function is a friend function of the class
void* threadedToggle(void *value){
GPIO *gpio = static_cast<GPIO*>(value);
bool isHigh = (bool) gpio->getValue(); //find current value
while(gpio->threadRunning){
if (isHigh) gpio->setValue(HIGH);
else gpio->setValue(LOW);
usleep(gpio->togglePeriod * 500);
isHigh=!isHigh;
if(gpio->toggleNumber>0) gpio->toggleNumber--;
if(gpio->toggleNumber==0) gpio->threadRunning=false;
}
return 0;
}
// Blocking Poll - based on the epoll socket code in the epoll man page
int GPIO::waitForEdge(){
this->setDirection(INPUT); // must be an input pin to poll its value
int fd, i, epollfd, count=0;
struct epoll_event ev;
epollfd = epoll_create(1);
if (epollfd == -1) {
perror("GPIO: Failed to create epollfd");
return -1;
}
if ((fd = open((this->path + "value").c_str(), O_RDONLY | O_NONBLOCK)) == -1) {
perror("GPIO: Failed to open file");
return -1;
}
//ev.events = read operation | edge triggered | urgent data
ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
ev.data.fd = fd; // attach the file file descriptor
//Register the file descriptor on the epoll instance, see: man epoll_ctl
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
perror("GPIO: Failed to add control interface");
return -1;
}
while(count<=1){ // ignore the first trigger
i = epoll_wait(epollfd, &ev, 1, -1);
if (i==-1){
perror("GPIO: Poll Wait fail");
count=5; // terminate loop
}
else {
count++; // count the triggers up
}
}
close(fd);
if (count==5) return -1;
return 0;
}
// This thread function is a friend function of the class
void* threadedPoll(void *value){
GPIO *gpio = static_cast<GPIO*>(value);
while(gpio->threadRunning){
gpio->callbackFunction(gpio->waitForEdge());
usleep(gpio->debounceTime * 1000);
}
return 0;
}
int GPIO::waitForEdge(CallbackType callback){
this->threadRunning = true;
this->callbackFunction = callback;
// create the thread, pass the reference, address of the function and data
if(pthread_create(&this->thread, NULL, &threadedPoll, static_cast<void*>(this))){
perror("GPIO: Failed to create the poll thread");
this->threadRunning = false;
return -1;
}
return 0;
}
GPIO::~GPIO() {
this->unexportGPIO();
}
} /* namespace exploringBB */