-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPIO.h
158 lines (129 loc) · 3.72 KB
/
GPIO.h
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
/*
* GPIO.h Created on: 29 Apr 2014
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring BeagleBone"
* 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<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cstdio>
#include<fcntl.h>
#include<unistd.h>
#include<sys/epoll.h>
#include<pthread.h>
//#ifndef GPIO_H_
//#define GPIO_H_
#include<string>
#include<fstream>
using std::string;
using std::ofstream;
#define GPIO_PATH "/sys/class/gpio/"
namespace exploringBB {
typedef int (*CallbackType)(int);
enum GPIO_DIRECTION{ INPUT, OUTPUT };
enum GPIO_VALUE{ LOW=0, HIGH=1 };
enum GPIO_EDGE{ NONE, RISING, FALLING, BOTH };
class GPIO {
private:
int number, debounceTime;
string name, path;
public:
GPIO(int number); //constructor will export the pin
virtual int getNumber() { return number; }
// General Input and Output Settings
virtual int setDirection(GPIO_DIRECTION);
virtual int setValue(GPIO_VALUE);
virtual ~GPIO(); //destructor will unexport the pin
private:
int write(string path, string filename, string value);
int write(string path, string filename, int value);
string read(string path, string filename);
};
} /* namespace exploringBB */
//#endif /* GPIO_H_ */
namespace exploringBB {
/**
*
* @param number The GPIO number for the BBB
*/
GPIO::GPIO(int number) {
this->number = number;
this->debounceTime = 0;
//perror("object created ");
std::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){
std::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){
std::stringstream s;
s << value;
return this->write(path,filename,s.str());
}
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;
}
GPIO::~GPIO() {
// this->unexportGPIO();
}
} /* namespace exploringBB */