-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPIO.h
executable file
·64 lines (49 loc) · 1.44 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
/* Revised by: Zongyao Jin
*
* Created on: 29 Apr 2014
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
*/
#ifndef GPIO_H_
#define GPIO_H_
#include <string>
#include <fstream>
#include <iostream>
using std::string;
using std::ofstream;
#define GPIO_PATH "/sys/class/gpio/"
namespace ZJ {
/**
* @class GPIO
* @brief GPIO class for input and output functionality on a single GPIO pin
*/
class GPIO {
public:
enum DIRECTION{ INPUT, OUTPUT };
enum VALUE{ LOW=0, HIGH=1 };
enum EDGE{ NONE, RISING, FALLING, BOTH };
private:
int number; /**< The GPIO number of the object */
string name; /**< The name of the GPIO e.g. gpio50 */
string path; /**< The full path to the GPIO e.g. /sys/class/gpio/gpio50/ */
public:
GPIO(int number);
virtual int getNumber() { return number; } /**< Returns the GPIO number as an int. */
// General Input and Output Settings
virtual int setDirection(GPIO::DIRECTION);
virtual GPIO::DIRECTION getDirection();
virtual int setValue(GPIO::VALUE);
virtual GPIO::VALUE getValue();
virtual int setActiveLow(bool isLow=true); //low=1, high=0
virtual int setActiveHigh(); //default
// Advanced OUTPUT: Faster write by keeping the stream alive (~20X)
virtual int streamOpen();
virtual int streamWrite(GPIO::VALUE);
virtual int streamClose();
virtual ~GPIO(); //destructor will unexport the pin
private:
int exportGPIO();
int unexportGPIO();
ofstream stream;
};
} /* namespace ZJ */
#endif /* GPIO_H_ */