Repository navigation
Expand file tree
/
Copy pathgpioapi.cpp
More file actions
289 lines (240 loc) · 9.68 KB
/
Copy pathgpioapi.cpp
File metadata and controls
289 lines (240 loc) · 9.68 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <ppltasks.h>
#include <collection.h>
#include "python.h"
#include "gpioapi.h"
using namespace concurrency;
using namespace Windows::Foundation;
using namespace Windows::Devices::Gpio;
using namespace Platform::Collections;
void OnPinValueChanged(GpioPin ^sender, GpioPinValueChangedEventArgs ^args);
int InitializeGpioStatics(void(*event_callback)(int, int));
static GpioController^ gpioController;
static Vector<GpioPin^>^ gpioPins;
static void(*gpioPinValueChangedCallback)(int, int);
static TypedEventHandler<GpioPin ^, GpioPinValueChangedEventArgs ^>^ gpioValueChangedEventHandler;
extern "C" {
/*************************************************************************
* init_gpio
************************************************************************/
int init_gpio(PyObject* module, void (*event_callback)(int, int)) {
int pinCount = InitializeGpioStatics(event_callback);
if (pinCount != FAILURE) {
PyModule_AddIntConstant(module, "pincount", pinCount);
} else {
PyErr_SetString(PyExc_TypeError, "Failed to get GPIO controller");
return FAILURE;
}
return SUCCESS;
}
/*************************************************************************
* setup_gpio_channel
************************************************************************/
int setup_gpio_channel(int channel, int direction, int pull_up_down, int initial) {
int ret = FAILURE;
GpioPin^ pin = nullptr;
GpioPinDriveMode driveMode;
if (channel >= 0 && channel < (int)gpioPins->Size) {
pin = gpioPins->GetAt(channel);
if (pin == nullptr) {
pin = gpioController->OpenPin(channel);
gpioPins->SetAt(channel, pin);
}
if (direction == DRIVEMODE_IN) {
switch (pull_up_down) {
case PUD_UP:
driveMode = GpioPinDriveMode::InputPullUp;
break;
case PUD_DOWN:
driveMode = GpioPinDriveMode::InputPullDown;
break;
default:
driveMode = GpioPinDriveMode::Input;
break;
}
} else if (direction == DRIVEMODE_OUT) {
switch (pull_up_down) {
case PUD_UP:
driveMode = GpioPinDriveMode::OutputOpenDrainPullUp;
break;
case PUD_DOWN:
driveMode = GpioPinDriveMode::OutputOpenSourcePullDown;
break;
default:
driveMode = GpioPinDriveMode::Output;
break;
}
} else {
PyErr_Format(PyExc_TypeError, "Invalid direction specified for pin %d", pin->PinNumber);
return FAILURE;
}
if (pin->IsDriveModeSupported(driveMode)) {
ret = SUCCESS;
pin->SetDriveMode(driveMode);
if (pin->GetDriveMode() == GpioPinDriveMode::Output) {
ret = output_gpio_channel(channel, initial);
}
} else {
PyErr_Format(PyExc_RuntimeError, "Invalid mode specified for pin %d", pin->PinNumber);
}
} else {
PyErr_SetString(PyExc_TypeError, "Invalid pin number specified");
}
return ret;
}
/*************************************************************************
* output_gpio_channel
************************************************************************/
int output_gpio_channel(int channel, int value) {
int ret = FAILURE;
GpioPin^ pin = nullptr;
if (channel >= 0 && channel < (int)gpioPins->Size) {
pin = gpioPins->GetAt(channel);
if (pin != nullptr) {
switch (pin->GetDriveMode()) {
case GpioPinDriveMode::Output:
case GpioPinDriveMode::OutputOpenSource:
case GpioPinDriveMode::OutputOpenSourcePullDown:
case GpioPinDriveMode::OutputOpenDrain:
case GpioPinDriveMode::OutputOpenDrainPullUp:
if (value == PINVALUE_HIGH) {
pin->Write(GpioPinValue::High);
ret = SUCCESS;
} else if (value == PINVALUE_LOW) {
pin->Write(GpioPinValue::Low);
ret = SUCCESS;
} else {
PyErr_Format(PyExc_TypeError, "Invalid pin value specified for pin %d", pin->PinNumber);
}
break;
default:
PyErr_SetString(PyExc_TypeError, "Pin not setup for output");
break;
}
} else {
PyErr_SetString(PyExc_TypeError, "Pin not setup");
}
} else {
PyErr_SetString(PyExc_TypeError, "Invalid pin number specified");
}
return ret;
}
/*************************************************************************
* input_gpio_channel
************************************************************************/
int input_gpio_channel(int channel, int* value) {
GpioPin^ pin = nullptr;
int ret = FAILURE;
if (channel >= 0 && channel < (int)gpioPins->Size) {
pin = gpioPins->GetAt(channel);
if (pin != nullptr) {
auto pinValue = pin->Read();
if (pinValue == GpioPinValue::High) {
*value = PINVALUE_HIGH;
}
else if (pinValue == GpioPinValue::Low) {
*value = PINVALUE_LOW;
}
ret = SUCCESS;
}
} else {
PyErr_SetString(PyExc_TypeError, "Invalid pin number specified");
}
return ret;
}
/*************************************************************************
* cleanup_gpio_channel
************************************************************************/
void cleanup_gpio_channel(int channel) {
if (channel >= 0 && channel < (int)gpioPins->Size) {
gpioPins->SetAt(channel, nullptr);
} else {
PyErr_SetString(PyExc_TypeError, "Invalid pin number specified");
}
}
/*************************************************************************
* cleanup_gpio_channels
************************************************************************/
void cleanup_gpio_channels() {
for (auto i = 0; i < (int)gpioPins->Size; i++) {
gpioPins->SetAt(i, nullptr);
}
}
/*************************************************************************
* enable_event_detect_gpio_channel
************************************************************************/
int enable_event_detect_gpio_channel(int channel, int debounce_timeout_ms, long long* event_token) {
auto pin = gpioPins->GetAt(channel);
TimeSpan debounceTimespan;
if (pin == nullptr) {
PyErr_SetString(PyExc_TypeError, "Channel is not setup to enable event detection");
return FAILURE;
}
switch (pin->GetDriveMode())
{
case GpioPinDriveMode::Input:
case GpioPinDriveMode::InputPullUp:
case GpioPinDriveMode::InputPullDown:
// Do nothing because pin is set up for input as expected
break;
default:
PyErr_SetString(PyExc_TypeError, "Channel is not setup for input");
return FAILURE;
}
debounceTimespan.Duration = debounce_timeout_ms * 10000; // Translate to 100-nanoseconds
pin->DebounceTimeout = debounceTimespan;
auto token = pin->ValueChanged += gpioValueChangedEventHandler;
*event_token = token.Value;
return SUCCESS;
}
/*************************************************************************
* disable_event_detect_gpio_channel
************************************************************************/
int disable_event_detect_gpio_channel(int channel, long long token) {
auto pin = gpioPins->GetAt(channel);
if (pin != nullptr) {
EventRegistrationToken eventToken;
eventToken.Value = token;
try {
pin->ValueChanged -= eventToken;
} catch (Platform::Exception^ e) {
PyErr_Format(PyExc_RuntimeError,
"An unexpected error occured during event detection removal: %S",
e->Message->Data());
return FAILURE;
}
}
return SUCCESS;
}
}
int
InitializeGpioStatics(void(*event_callback)(int, int)) {
int pinCount = FAILURE;
gpioController = GpioController::GetDefault();
gpioValueChangedEventHandler =
ref new TypedEventHandler<GpioPin ^, GpioPinValueChangedEventArgs ^>(
OnPinValueChanged);
gpioPinValueChangedCallback = event_callback;
if (gpioController != nullptr) {
pinCount = (int)gpioController->PinCount;
gpioPins = ref new Vector<GpioPin^>(pinCount+1);
}
return pinCount;
}
void
OnPinValueChanged(GpioPin ^sender, GpioPinValueChangedEventArgs ^args) {
if (sender != nullptr && args != nullptr
&& gpioPinValueChangedCallback != nullptr) {
int edge = 0;
switch (args->Edge) {
case GpioPinEdge::FallingEdge:
edge = FALLING_EDGE;
break;
case GpioPinEdge::RisingEdge:
edge = RISING_EDGE;
break;
default:
break;
}
gpioPinValueChangedCallback(sender->PinNumber, edge);
}
}