forked from oxavelar/HighLatencyGPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPIO.hh
More file actions
executable file
·200 lines (158 loc) · 6.6 KB
/
GPIO.hh
File metadata and controls
executable file
·200 lines (158 loc) · 6.6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
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>
#include <fstream>
#include <memory>
// 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:
typedef std::shared_ptr<GPIO> sPtr;
//-----------------------------------------------------------------------------------------------
/// @enum Direction
/// @brief Type used to configure a GPIO as an input or output
//-----------------------------------------------------------------------------------------------
enum Type {
OUT,
IN,
RISING,
FALLING,
BOTH
};
//-----------------------------------------------------------------------------------------------
/// @enum Value
/// @brief Type used to indicate the logic level of a GPIO
//-----------------------------------------------------------------------------------------------
enum Value {
LOW,
HIGH
};
typedef std::function<void(unsigned short, Value)> isr_callback;
//-----------------------------------------------------------------------------------------------
// FUNCTION NAME: Output GPIO (constructor)
///
/// @brief Construt an output GPIO object.
///
/// @param[in] id The GPIO ID. Often referred to as "pin number".
///
//-----------------------------------------------------------------------------------------------
explicit GPIO(
unsigned short id);
//-----------------------------------------------------------------------------------------------
// 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] type The type of GPIO to construct.
/// @param[in] isr The function to call when transitions of type RISING, FALLING or BOTH occurs. can be null.
/// @param[in] usePollValue On some platforms ISR detector is doing bananas, so utilize pollValue routine instead
///
/// @note If function isr throws an exception, IT WILL BE CONSUMED BY THIS CLASS.
///
//-----------------------------------------------------------------------------------------------
explicit GPIO(
unsigned short id,
Type type,
isr_callback isr,
bool usePollValue = true);
//-----------------------------------------------------------------------------------------------
// 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);
//-----------------------------------------------------------------------------------------------
// 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();
unsigned short id() const;
protected:
void initCommon();
void pollIsrLoop();
void pollValueLoop();
void eventDispatcherLoop();
GPIO::Value charToValue(char c);
GPIO::Value getValueFromSysfs();
protected:
std::string _value_filename;
std::fstream sysfs_value;
Value _value;
bool _usePollValue;
static const std::string _sysfsPath;
const unsigned short _id;
const std::string _id_str;
const Type _type;
const isr_callback _isr;
std::thread _pollThread;
int _pollFD;
std::thread _eventDispatcherThread;
std::atomic<bool> _destructing;
int _pipeFD[2];
static constexpr int _default_ownership_wait_timeout = 5000;
static constexpr const char* _default_ownership_user = "root";
static constexpr const char* _default_ownership_group = "gpio";
#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::mutex _sysfsValueMutex;
std::condition_variable _eventCV;
#endif
};
#endif