GPIO class for input and output functionality on a single GPIO pin.
More...
#include <GPIO.h>
GPIO class for input and output functionality on a single GPIO pin.
Enumerator |
---|
NONE |
|
RISING |
|
FALLING |
|
BOTH |
|
exploringBB::GPIO::GPIO |
( |
int |
number | ) |
|
The constructor will set up the states and export the pin.
- Parameters
-
number | The GPIO number to be exported |
48 this->number = number;
49 this->debounceTime = 0;
50 this->togglePeriod=100;
51 this->toggleNumber=-1;
52 this->callbackFunction = NULL;
53 this->threadRunning =
false;
56 s <<
"gpio" << number;
57 this->name = string(s.str());
58 this->path =
GPIO_PATH + this->name +
"/";
#define GPIO_PATH
Definition: GPIO.h:36
exploringBB::GPIO::~GPIO |
( |
| ) |
|
|
virtual |
294 this->unexportGPIO();
virtual void exploringBB::GPIO::changeToggleTime |
( |
int |
time | ) |
|
|
inlinevirtual |
80 { this->togglePeriod = time; }
170 string input =
read(this->path,
"direction");
171 if (input ==
"in")
return INPUT;
string read(string path, string filename)
Definition: util.cpp:57
176 string input =
read(this->path,
"edge");
177 if (input ==
"rising")
return RISING;
178 else if (input ==
"falling")
return FALLING;
179 else if (input ==
"both")
return BOTH;
string read(string path, string filename)
Definition: util.cpp:57
virtual int exploringBB::GPIO::getNumber |
( |
| ) |
|
|
inlinevirtual |
Returns the GPIO number as an int.
164 string input =
read(this->path,
"value");
165 if (input ==
"0")
return LOW;
string read(string path, string filename)
Definition: util.cpp:57
int exploringBB::GPIO::setActiveHigh |
( |
| ) |
|
|
virtual |
virtual int setActiveLow(bool isLow=true)
Definition: GPIO.cpp:154
int exploringBB::GPIO::setActiveLow |
( |
bool |
isLow = true | ) |
|
|
virtual |
155 if(isLow)
return write(this->path,
"active_low",
"1");
156 else return write(this->path,
"active_low",
"0");
int write(string path, string filename, string value)
Definition: util.cpp:40
virtual void exploringBB::GPIO::setDebounceTime |
( |
int |
time | ) |
|
|
inlinevirtual |
71 { this->debounceTime = time; }
122 case INPUT:
return write(this->path,
"direction",
"in");
124 case OUTPUT:
return write(this->path,
"direction",
"out");
int write(string path, string filename, string value)
Definition: util.cpp:40
int exploringBB::GPIO::setEdgeType |
( |
GPIO::EDGE |
value | ) |
|
|
virtual |
142 case NONE:
return write(this->path,
"edge",
"none");
144 case RISING:
return write(this->path,
"edge",
"rising");
146 case FALLING:
return write(this->path,
"edge",
"falling");
148 case BOTH:
return write(this->path,
"edge",
"both");
int write(string path, string filename, string value)
Definition: util.cpp:40
132 case HIGH:
return write(this->path,
"value",
"1");
134 case LOW:
return write(this->path,
"value",
"0");
int write(string path, string filename, string value)
Definition: util.cpp:40
int exploringBB::GPIO::streamClose |
( |
| ) |
|
|
virtual |
int exploringBB::GPIO::streamOpen |
( |
| ) |
|
|
virtual |
184 stream.open((path +
"value").c_str());
int exploringBB::GPIO::streamWrite |
( |
GPIO::VALUE |
value | ) |
|
|
virtual |
188 stream << value << std::flush;
virtual void exploringBB::GPIO::toggleCancel |
( |
| ) |
|
|
inlinevirtual |
81 { this->threadRunning =
false; }
int exploringBB::GPIO::toggleOutput |
( |
| ) |
|
|
virtual |
virtual GPIO::VALUE getValue()
Definition: GPIO.cpp:163
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
int exploringBB::GPIO::toggleOutput |
( |
int |
time | ) |
|
|
virtual |
virtual int toggleOutput()
Definition: GPIO.cpp:196
int exploringBB::GPIO::toggleOutput |
( |
int |
numberOfTimes, |
|
|
int |
time |
|
) |
| |
|
virtual |
206 this->toggleNumber = numberOfTimes;
207 this->togglePeriod = time;
208 this->threadRunning =
true;
209 if(pthread_create(&this->thread, NULL, &
threadedToggle, static_cast<void*>(
this))){
210 perror(
"GPIO: Failed to create the toggle thread");
211 this->threadRunning =
false;
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
friend void * threadedToggle(void *value)
Definition: GPIO.cpp:218
int exploringBB::GPIO::waitForEdge |
( |
| ) |
|
|
virtual |
235 int fd, i, epollfd, count=0;
236 struct epoll_event ev;
237 epollfd = epoll_create(1);
239 perror(
"GPIO: Failed to create epollfd");
242 if ((fd = open((this->path +
"value").c_str(), O_RDONLY | O_NONBLOCK)) == -1) {
243 perror(
"GPIO: Failed to open file");
248 ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
252 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
253 perror(
"GPIO: Failed to add control interface");
257 i = epoll_wait(epollfd, &ev, 1, -1);
259 perror(
"GPIO: Poll Wait fail");
267 if (count==5)
return -1;
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
282 this->threadRunning =
true;
285 if(pthread_create(&this->thread, NULL, &
threadedPoll, static_cast<void*>(
this))){
286 perror(
"GPIO: Failed to create the poll thread");
287 this->threadRunning =
false;
friend void * threadedPoll(void *value)
Definition: GPIO.cpp:272
int callbackFunction(int var)
Definition: TestCode.cxx:46
virtual void exploringBB::GPIO::waitForEdgeCancel |
( |
| ) |
|
|
inlinevirtual |
88 { this->threadRunning =
false; }
void* threadedPoll |
( |
void * |
value | ) |
|
|
friend |
273 GPIO *gpio =
static_cast<GPIO*
>(value);
274 while(gpio->threadRunning){
275 gpio->callbackFunction(gpio->waitForEdge());
276 usleep(gpio->debounceTime * 1000);
GPIO(int number)
Definition: GPIO.cpp:47
void* threadedToggle |
( |
void * |
value | ) |
|
|
friend |
219 GPIO *gpio =
static_cast<GPIO*
>(value);
220 bool isHigh = (bool) gpio->getValue();
221 while(gpio->threadRunning){
224 usleep(gpio->togglePeriod * 500);
226 if(gpio->toggleNumber>0) gpio->toggleNumber--;
227 if(gpio->toggleNumber==0) gpio->threadRunning=
false;
GPIO(int number)
Definition: GPIO.cpp:47
The documentation for this class was generated from the following files:
- /home/molloyd/exploringBB/library/gpio/GPIO.h
- /home/molloyd/exploringBB/library/gpio/GPIO.cpp