Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GPIO.h
Go to the documentation of this file.
1 /*
2  * @file GPIO.h
3  * @author Derek Molloy
4  * @version 0.1
5  *
6  * Created on: 29 Apr 2014
7  * Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
8  * Made available for the book "Exploring BeagleBone"
9  * See: www.exploringbeaglebone.com
10  * Licensed under the EUPL V.1.1
11  *
12  * This Software is provided to You under the terms of the European
13  * Union Public License (the "EUPL") version 1.1 as published by the
14  * European Union. Any use of this Software, other than as authorized
15  * under this License is strictly prohibited (to the extent such use
16  * is covered by a right of the copyright holder of this Software).
17  *
18  * This Software is provided under the License on an "AS IS" basis and
19  * without warranties of any kind concerning the Software, including
20  * without limitation merchantability, fitness for a particular purpose,
21  * absence of defects or errors, accuracy, and non-infringement of
22  * intellectual property rights other than copyright. This disclaimer
23  * of warranty is an essential part of the License and a condition for
24  * the grant of any rights to this Software.
25  *
26  * For more details, see http://www.derekmolloy.ie/
27  */
28 
29 #ifndef GPIO_H_
30 #define GPIO_H_
31 #include<string>
32 #include<fstream>
33 using std::string;
34 using std::ofstream;
35 
36 #define GPIO_PATH "/sys/class/gpio/"
37 
38 namespace exploringBB {
39 
40 typedef int (*CallbackType)(int);
41 
46 class GPIO {
47 public:
49  enum VALUE{ LOW=0, HIGH=1 };
50  enum EDGE{ NONE, RISING, FALLING, BOTH };
51 
52 private:
53  int number;
54  int debounceTime;
55  string name;
56  string path;
58 public:
59  GPIO(int number);
60  virtual int getNumber() { return number; }
62  // General Input and Output Settings
63  virtual int setDirection(GPIO::DIRECTION);
64  virtual GPIO::DIRECTION getDirection();
65  virtual int setValue(GPIO::VALUE);
66  virtual int toggleOutput();
67  virtual GPIO::VALUE getValue();
68  virtual int setActiveLow(bool isLow=true); //low=1, high=0
69  virtual int setActiveHigh(); //default
70  //software debounce input (ms) - default 0
71  virtual void setDebounceTime(int time) { this->debounceTime = time; }
72 
73  // Advanced OUTPUT: Faster write by keeping the stream alive (~20X)
74  virtual int streamOpen();
75  virtual int streamWrite(GPIO::VALUE);
76  virtual int streamClose();
77 
78  virtual int toggleOutput(int time); //threaded invert output every X ms.
79  virtual int toggleOutput(int numberOfTimes, int time);
80  virtual void changeToggleTime(int time) { this->togglePeriod = time; }
81  virtual void toggleCancel() { this->threadRunning = false; }
82 
83  // Advanced INPUT: Detect input edges; threaded and non-threaded
84  virtual int setEdgeType(GPIO::EDGE);
85  virtual GPIO::EDGE getEdgeType();
86  virtual int waitForEdge(); // waits until button is pressed
87  virtual int waitForEdge(CallbackType callback); // threaded with callback
88  virtual void waitForEdgeCancel() { this->threadRunning = false; }
89 
90  virtual ~GPIO(); //destructor will unexport the pin
91 
92 private:
93  //int write(string path, string filename, string value);
94  //int write(string path, string filename, int value);
95  //string read(string path, string filename);
96  int exportGPIO();
97  int unexportGPIO();
98  ofstream stream;
99  pthread_t thread;
100  CallbackType callbackFunction;
101  bool threadRunning;
102  int togglePeriod; //default 100ms
103  int toggleNumber; //default -1 (infinite)
104  friend void* threadedPoll(void *value);
105  friend void* threadedToggle(void *value);
106 };
107 
108 void* threadedPoll(void *value);
109 void* threadedToggle(void *value);
110 
111 } /* namespace exploringBB */
112 
113 #endif /* GPIO_H_ */
Definition: GPIO.h:50
virtual int streamOpen()
Definition: GPIO.cpp:183
int(* CallbackType)(int)
Definition: GPIO.h:40
virtual GPIO::EDGE getEdgeType()
Definition: GPIO.cpp:175
void * threadedToggle(void *value)
Definition: GPIO.cpp:218
virtual void setDebounceTime(int time)
Definition: GPIO.h:71
GPIO class for input and output functionality on a single GPIO pin.
Definition: GPIO.h:46
virtual void toggleCancel()
Definition: GPIO.h:81
DIRECTION
Definition: GPIO.h:48
virtual int setEdgeType(GPIO::EDGE)
Definition: GPIO.cpp:140
GPIO(int number)
Definition: GPIO.cpp:47
Definition: GPIO.h:50
void * threadedPoll(void *value)
Definition: GPIO.cpp:272
Definition: GPIO.h:48
virtual GPIO::VALUE getValue()
Definition: GPIO.cpp:163
virtual int setActiveLow(bool isLow=true)
Definition: GPIO.cpp:154
Definition: GPIO.h:49
virtual int streamWrite(GPIO::VALUE)
Definition: GPIO.cpp:187
Definition: GPIO.h:50
Definition: GPIO.h:48
Definition: GPIO.h:49
virtual void waitForEdgeCancel()
Definition: GPIO.h:88
virtual void changeToggleTime(int time)
Definition: GPIO.h:80
Definition: BusDevice.cpp:27
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130
virtual GPIO::DIRECTION getDirection()
Definition: GPIO.cpp:169
friend void * threadedPoll(void *value)
Definition: GPIO.cpp:272
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
VALUE
Definition: GPIO.h:49
virtual int getNumber()
Definition: GPIO.h:60
EDGE
Definition: GPIO.h:50
Definition: GPIO.h:50
virtual int waitForEdge()
Definition: GPIO.cpp:233
friend void * threadedToggle(void *value)
Definition: GPIO.cpp:218
virtual int streamClose()
Definition: GPIO.cpp:191
virtual int setActiveHigh()
Definition: GPIO.cpp:159
virtual int toggleOutput()
Definition: GPIO.cpp:196
virtual ~GPIO()
Definition: GPIO.cpp:293