Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Friends
exploringBB::GPIO Class Reference

GPIO class for input and output functionality on a single GPIO pin. More...

#include <GPIO.h>

Public Types

enum  DIRECTION { INPUT, OUTPUT }
 
enum  VALUE { LOW =0, HIGH =1 }
 
enum  EDGE { NONE, RISING, FALLING, BOTH }
 

Public Member Functions

 GPIO (int number)
 
virtual int getNumber ()
 
virtual int setDirection (GPIO::DIRECTION)
 
virtual GPIO::DIRECTION getDirection ()
 
virtual int setValue (GPIO::VALUE)
 
virtual int toggleOutput ()
 
virtual GPIO::VALUE getValue ()
 
virtual int setActiveLow (bool isLow=true)
 
virtual int setActiveHigh ()
 
virtual void setDebounceTime (int time)
 
virtual int streamOpen ()
 
virtual int streamWrite (GPIO::VALUE)
 
virtual int streamClose ()
 
virtual int toggleOutput (int time)
 
virtual int toggleOutput (int numberOfTimes, int time)
 
virtual void changeToggleTime (int time)
 
virtual void toggleCancel ()
 
virtual int setEdgeType (GPIO::EDGE)
 
virtual GPIO::EDGE getEdgeType ()
 
virtual int waitForEdge ()
 
virtual int waitForEdge (CallbackType callback)
 
virtual void waitForEdgeCancel ()
 
virtual ~GPIO ()
 

Friends

void * threadedPoll (void *value)
 
void * threadedToggle (void *value)
 

Detailed Description

GPIO class for input and output functionality on a single GPIO pin.

Member Enumeration Documentation

Enumerator
INPUT 
OUTPUT 
48 { INPUT, OUTPUT };
Definition: GPIO.h:48
Definition: GPIO.h:48
Enumerator
NONE 
RISING 
FALLING 
BOTH 
50 { NONE, RISING, FALLING, BOTH };
Definition: GPIO.h:50
Definition: GPIO.h:50
Definition: GPIO.h:50
Definition: GPIO.h:50
Enumerator
LOW 
HIGH 
49 { LOW=0, HIGH=1 };
Definition: GPIO.h:49
Definition: GPIO.h:49

Constructor & Destructor Documentation

exploringBB::GPIO::GPIO ( int  number)

The constructor will set up the states and export the pin.

Parameters
numberThe GPIO number to be exported
47  {
48  this->number = number;
49  this->debounceTime = 0;
50  this->togglePeriod=100;
51  this->toggleNumber=-1; //infinite number
52  this->callbackFunction = NULL;
53  this->threadRunning = false;
54 
55  ostringstream s;
56  s << "gpio" << number;
57  this->name = string(s.str());
58  this->path = GPIO_PATH + this->name + "/";
59  this->exportGPIO();
60  // need to give Linux time to set up the sysfs structure
61  usleep(250000); // 250ms delay
62 }
#define GPIO_PATH
Definition: GPIO.h:36
exploringBB::GPIO::~GPIO ( )
virtual
293  {
294  this->unexportGPIO();
295 }

Member Function Documentation

virtual void exploringBB::GPIO::changeToggleTime ( int  time)
inlinevirtual
80 { this->togglePeriod = time; }
GPIO::DIRECTION exploringBB::GPIO::getDirection ( )
virtual
169  {
170  string input = read(this->path, "direction");
171  if (input == "in") return INPUT;
172  else return OUTPUT;
173 }
Definition: GPIO.h:48
Definition: GPIO.h:48
string read(string path, string filename)
Definition: util.cpp:57
GPIO::EDGE exploringBB::GPIO::getEdgeType ( )
virtual
175  {
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;
180  else return NONE;
181 }
Definition: GPIO.h:50
Definition: GPIO.h:50
Definition: GPIO.h:50
Definition: GPIO.h:50
string read(string path, string filename)
Definition: util.cpp:57
virtual int exploringBB::GPIO::getNumber ( )
inlinevirtual

Returns the GPIO number as an int.

GPIO::VALUE exploringBB::GPIO::getValue ( )
virtual
163  {
164  string input = read(this->path, "value");
165  if (input == "0") return LOW;
166  else return HIGH;
167 }
Definition: GPIO.h:49
Definition: GPIO.h:49
string read(string path, string filename)
Definition: util.cpp:57
int exploringBB::GPIO::setActiveHigh ( )
virtual
159  {
160  return this->setActiveLow(false);
161 }
virtual int setActiveLow(bool isLow=true)
Definition: GPIO.cpp:154
int exploringBB::GPIO::setActiveLow ( bool  isLow = true)
virtual
154  {
155  if(isLow) return write(this->path, "active_low", "1");
156  else return write(this->path, "active_low", "0");
157 }
int write(string path, string filename, string value)
Definition: util.cpp:40
virtual void exploringBB::GPIO::setDebounceTime ( int  time)
inlinevirtual
71 { this->debounceTime = time; }
int exploringBB::GPIO::setDirection ( GPIO::DIRECTION  dir)
virtual
120  {
121  switch(dir){
122  case INPUT: return write(this->path, "direction", "in");
123  break;
124  case OUTPUT:return write(this->path, "direction", "out");
125  break;
126  }
127  return -1;
128 }
int write(string path, string filename, string value)
Definition: util.cpp:40
Definition: GPIO.h:48
Definition: GPIO.h:48
int exploringBB::GPIO::setEdgeType ( GPIO::EDGE  value)
virtual
140  {
141  switch(value){
142  case NONE: return write(this->path, "edge", "none");
143  break;
144  case RISING: return write(this->path, "edge", "rising");
145  break;
146  case FALLING: return write(this->path, "edge", "falling");
147  break;
148  case BOTH: return write(this->path, "edge", "both");
149  break;
150  }
151  return -1;
152 }
Definition: GPIO.h:50
int write(string path, string filename, string value)
Definition: util.cpp:40
Definition: GPIO.h:50
Definition: GPIO.h:50
Definition: GPIO.h:50
int exploringBB::GPIO::setValue ( GPIO::VALUE  value)
virtual
130  {
131  switch(value){
132  case HIGH: return write(this->path, "value", "1");
133  break;
134  case LOW: return write(this->path, "value", "0");
135  break;
136  }
137  return -1;
138 }
int write(string path, string filename, string value)
Definition: util.cpp:40
Definition: GPIO.h:49
Definition: GPIO.h:49
int exploringBB::GPIO::streamClose ( )
virtual
191  {
192  stream.close();
193  return 0;
194 }
int exploringBB::GPIO::streamOpen ( )
virtual
183  {
184  stream.open((path + "value").c_str());
185  return 0;
186 }
int exploringBB::GPIO::streamWrite ( GPIO::VALUE  value)
virtual
187  {
188  stream << value << std::flush;
189  return 0;
190 }
virtual void exploringBB::GPIO::toggleCancel ( )
inlinevirtual
81 { this->threadRunning = false; }
int exploringBB::GPIO::toggleOutput ( )
virtual
196  {
197  this->setDirection(OUTPUT);
198  if ((bool) this->getValue()) this->setValue(LOW);
199  else this->setValue(HIGH);
200  return 0;
201 }
virtual GPIO::VALUE getValue()
Definition: GPIO.cpp:163
Definition: GPIO.h:49
Definition: GPIO.h:48
Definition: GPIO.h:49
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
203 { return this->toggleOutput(-1, time); }
virtual int toggleOutput()
Definition: GPIO.cpp:196
int exploringBB::GPIO::toggleOutput ( int  numberOfTimes,
int  time 
)
virtual
204  {
205  this->setDirection(OUTPUT);
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;
212  return -1;
213  }
214  return 0;
215 }
Definition: GPIO.h:48
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
friend void * threadedToggle(void *value)
Definition: GPIO.cpp:218
int exploringBB::GPIO::waitForEdge ( )
virtual
233  {
234  this->setDirection(INPUT); // must be an input pin to poll its value
235  int fd, i, epollfd, count=0;
236  struct epoll_event ev;
237  epollfd = epoll_create(1);
238  if (epollfd == -1) {
239  perror("GPIO: Failed to create epollfd");
240  return -1;
241  }
242  if ((fd = open((this->path + "value").c_str(), O_RDONLY | O_NONBLOCK)) == -1) {
243  perror("GPIO: Failed to open file");
244  return -1;
245  }
246 
247  //ev.events = read operation | edge triggered | urgent data
248  ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
249  ev.data.fd = fd; // attach the file file descriptor
250 
251  //Register the file descriptor on the epoll instance, see: man epoll_ctl
252  if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
253  perror("GPIO: Failed to add control interface");
254  return -1;
255  }
256  while(count<=1){ // ignore the first trigger
257  i = epoll_wait(epollfd, &ev, 1, -1);
258  if (i==-1){
259  perror("GPIO: Poll Wait fail");
260  count=5; // terminate loop
261  }
262  else {
263  count++; // count the triggers up
264  }
265  }
266  close(fd);
267  if (count==5) return -1;
268  return 0;
269 }
Definition: GPIO.h:48
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
int exploringBB::GPIO::waitForEdge ( CallbackType  callback)
virtual
281  {
282  this->threadRunning = true;
283  this->callbackFunction = callback;
284  // create the thread, pass the reference, address of the function and data
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;
288  return -1;
289  }
290  return 0;
291 }
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; }

Friends And Related Function Documentation

void* threadedPoll ( void *  value)
friend
272  {
273  GPIO *gpio = static_cast<GPIO*>(value);
274  while(gpio->threadRunning){
275  gpio->callbackFunction(gpio->waitForEdge());
276  usleep(gpio->debounceTime * 1000);
277  }
278  return 0;
279 }
GPIO(int number)
Definition: GPIO.cpp:47
void* threadedToggle ( void *  value)
friend
218  {
219  GPIO *gpio = static_cast<GPIO*>(value);
220  bool isHigh = (bool) gpio->getValue(); //find current value
221  while(gpio->threadRunning){
222  if (isHigh) gpio->setValue(GPIO::HIGH);
223  else gpio->setValue(GPIO::LOW);
224  usleep(gpio->togglePeriod * 500);
225  isHigh=!isHigh;
226  if(gpio->toggleNumber>0) gpio->toggleNumber--;
227  if(gpio->toggleNumber==0) gpio->threadRunning=false;
228  }
229  return 0;
230 }
GPIO(int number)
Definition: GPIO.cpp:47
Definition: GPIO.h:49
Definition: GPIO.h:49

The documentation for this class was generated from the following files: