forked from tweej/HighLatencyGPIO
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGPIO.hh
More file actions
executable file
·186 lines (152 loc) · 6.47 KB
/
GPIO.hh
File metadata and controls
executable file
·186 lines (152 loc) · 6.47 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
The MIT License (MIT)
Copyright (c) 2014 Thomas Mercier Jr.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef GPIO_HH
#define GPIO_HH
#include "Uncopyable.hh"
#include <atomic>
#include <functional>
#include <string>
#include <thread>
// LOCKFREE define specifies the use of a (single producer, single consumer) lockfree container for
// the transfer of transition events from the thread which detects these events, to the thread which
// will call the user-provided callback function. This implementation is EXTREMELY wasteful of CPU
// time!!! However, it provides about 0.5 ms lower latency than the default implementation. If lower
// latency is required, please one of the BeagleBone Black PRUs, or a kernel module.
#ifdef LOCKFREE
#include <boost/lockfree/spsc_queue.hpp>
#else
#include <queue>
#include <mutex>
#include <condition_variable>
#endif
class GPIO : private Uncopyable
{
public:
//-----------------------------------------------------------------------------------------------
/// @enum Direction
/// @brief Type used to configure a GPIO as an input or output
//-----------------------------------------------------------------------------------------------
enum Direction {
IN,
OUT
};
//-----------------------------------------------------------------------------------------------
/// @enum Value
/// @brief Type used to indicate the logic level of a GPIO
//-----------------------------------------------------------------------------------------------
enum Value {
HIGH,
LOW
};
//-----------------------------------------------------------------------------------------------
/// @enum Edge
/// @brief Type used to indicate which logic level transitions on an input GPIO should result in
/// a call to the user-provided callback function.
//-----------------------------------------------------------------------------------------------
enum Edge {
NONE,
RISING,
FALLING,
BOTH
};
//-----------------------------------------------------------------------------------------------
// FUNCTION NAME: GPIO (constructor)
///
/// @brief Construt an input or output GPIO object.
///
/// @param[in] id The GPIO ID. Often referred to as "pin number".
/// @param[in] direction The type (INPUT or OUTPUT) of GPIO to construct.
///
//-----------------------------------------------------------------------------------------------
explicit GPIO(
unsigned short id,
Direction direction);
//-----------------------------------------------------------------------------------------------
// FUNCTION NAME: GPIO (constructor)
///
/// @brief Construt an input GPIO object which will call a callback function every time a
/// transition of type edge occurs.
///
///
/// @param[in] id The GPIO ID. Often referred to as "pin number".
/// @param[in] edge The type (INPUT or OUTPUT) of GPIO to construct.
/// @param[in] isr The function to call when transitions of type edge occur.
///
/// @note If function isr throws an exception, IT WILL NOT BE HANDLED OR IGNORED BY THIS CLASS.
/// Therefore, it is recommended to make this function noexcept.
///
//-----------------------------------------------------------------------------------------------
explicit GPIO(
unsigned short id,
Edge edge,
std::function<void(Value)> isr);
//-----------------------------------------------------------------------------------------------
// FUNCTION NAME: GPIO (destructor)
///
/// @note If a program which uses this class is ungracefully terminated (this destructor is not
/// called), the GPIO will be left in an exported state which will prevent subsequent
/// construction of a GPIO object with the same id.
//-----------------------------------------------------------------------------------------------
~GPIO();
//-----------------------------------------------------------------------------------------------
// FUNCTION NAME: setValue
///
/// @brief Set the logical value (HIGH or LOW) of the GPIO. All GPIOs are active-high.
///
/// @param[in] value The logical value to set.
///
/// @return None
///
//-----------------------------------------------------------------------------------------------
void setValue(const Value value) const;
//-----------------------------------------------------------------------------------------------
// FUNCTION NAME: getValue
///
/// @brief Get the logical value (HIGH or LOW) of the GPIO. All GPIOs are active-high.
///
/// @return The logical value of the GPIO.
///
//-----------------------------------------------------------------------------------------------
Value getValue() const;
private:
void initCommon() const;
void pollLoop();
void isrLoop();
private:
static const std::string _sysfsPath;
const unsigned short _id;
const std::string _id_str;
const Direction _direction;
const Edge _edge;
const std::function<void(Value)> _isr;
std::thread _pollThread;
int _pollFD;
std::thread _isrThread;
std::atomic<bool> _destructing;
int _pipeFD[2];
#ifdef LOCKFREE
boost::lockfree::spsc_queue<Value, boost::lockfree::capacity<64>> _spsc_queue;
#else
std::queue<Value> _eventQueue; // stores values generated by interrupts
std::mutex _eventMutex;
std::condition_variable _eventCV;
#endif
};
#endif