Menu

[5108d3]: / source / py_gpio.c  Maximize  Restore  History

Download this file

341 lines (282 with data), 10.1 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
Copyright (c) 2012 Ben Croston
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.
*/
#include "Python.h"
#include "c_gpio.h"
static PyObject *WrongDirectionException;
static PyObject *InvalidModeException;
static PyObject *InvalidDirectionException;
static PyObject *InvalidChannelException;
static PyObject *ModeNotSetException;
static PyObject *SetupException;
static PyObject *high;
static PyObject *low;
static PyObject *input;
static PyObject *output;
static PyObject *board;
static PyObject *bcm;
static int gpio_direction[54];
static const int pin_to_gpio[27] = {-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7};
#define MODE_UNKNOWN -1
#define BOARD 10
#define BCM 11
static int gpio_mode = MODE_UNKNOWN;
// setup function run on import of the RPi.GPIO module
static int module_setup(void)
{
int i, result;
// printf("Setup module (mmap)\n");
for (i=0; i<54; i++)
gpio_direction[i] = -1;
result = setup();
if (result == SETUP_DEVMEM_FAIL)
{
PyErr_SetString(SetupException, "No access to /dev/mem. Try running as root!");
return SETUP_DEVMEM_FAIL;
} else if (result == SETUP_MALLOC_FAIL) {
PyErr_NoMemory();
return SETUP_MALLOC_FAIL;
} else if (result == SETUP_MMAP_FAIL) {
PyErr_SetString(SetupException, "Mmap failed on module import");
return SETUP_MALLOC_FAIL;
} else { // result == SETUP_OK
return SETUP_OK;
}
}
// function run on exit of python
static void gpio_cleanup(void)
{
int i;
// printf("GPIO cleanup\n");
for (i=0; i<54; i++)
if (gpio_direction[i] != -1)
{
// printf("GPIO %d --> INPUT\n", i);
setup_gpio(i, INPUT);
}
cleanup();
}
// python function setup(channel, direction)
static PyObject *py_setup_channel(PyObject *self, PyObject *args)
{
int gpio, channel, direction;
if (!PyArg_ParseTuple(args, "ii", &channel, &direction))
return NULL;
if (direction != INPUT && direction != OUTPUT)
{
PyErr_SetString(InvalidDirectionException, "An invalid direction was passed to setup()");
return NULL;
}
if (gpio_mode != BCM && gpio_mode != BOARD)
{
PyErr_SetString(ModeNotSetException, "Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)");
return NULL;
}
if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
|| (gpio_mode == BOARD && (channel < 1 || channel > 26)) )
{
PyErr_SetString(InvalidChannelException, "The channel sent is invalid on a Raspberry Pi");
return NULL;
}
if (gpio_mode == BOARD)
{
gpio = pin_to_gpio[channel];
if (gpio == -1)
{
PyErr_SetString(InvalidChannelException, "The channel sent is invalid on a Raspberry Pi");
return NULL;
}
}
else // gpio_mode == BCM
{
gpio = channel;
}
// printf("Setup GPIO %d direction %d\n", gpio, direction);
setup_gpio(gpio, direction);
gpio_direction[gpio] = direction;
Py_INCREF(Py_None);
return Py_None;
}
// python function output(channel, value)
static PyObject *py_output_gpio(PyObject *self, PyObject *args)
{
int gpio, channel, value;
if (!PyArg_ParseTuple(args, "ii", &channel, &value))
return NULL;
if (gpio_mode != BOARD && gpio_mode != BCM)
{
PyErr_SetString(ModeNotSetException, "Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)");
return NULL;
}
if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
|| (gpio_mode == BOARD && (channel < 1 || channel > 26)) )
{
PyErr_SetString(InvalidChannelException, "The channel sent is invalid on a Raspberry Pi");
return NULL;
}
if (gpio_mode == BOARD)
{
gpio = pin_to_gpio[channel];
if (gpio == -1)
{
PyErr_SetString(InvalidChannelException, "The channel sent is invalid on a Raspberry Pi");
return NULL;
}
}
else // gpio_mode == BCM
{
gpio = channel;
}
if (gpio_direction[gpio] != OUTPUT)
{
PyErr_SetString(WrongDirectionException, "The GPIO channel has not been set up or is set up in the wrong direction");
return NULL;
}
// printf("Output GPIO %d value %d\n", gpio, value);
output_gpio(gpio, value);
Py_INCREF(Py_None);
return Py_None;
}
// python function value = input(channel)
static PyObject *py_input_gpio(PyObject *self, PyObject *args)
{
int gpio, channel, value;
if (!PyArg_ParseTuple(args, "i", &channel))
return NULL;
if (gpio_mode != BOARD && gpio_mode != BCM)
{
PyErr_SetString(ModeNotSetException, "Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)");
return NULL;
}
if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
|| (gpio_mode == BOARD && (channel < 1 || channel > 26)) )
{
PyErr_SetString(InvalidChannelException, "The channel sent is invalid on a Raspberry Pi");
return NULL;
}
if (gpio_mode == BOARD)
{
gpio = pin_to_gpio[channel];
if (gpio == -1)
{
PyErr_SetString(InvalidChannelException, "The channel sent is invalid on a Raspberry Pi");
return NULL;
}
}
else // gpio_mode == BCM
{
gpio = channel;
}
if (gpio_direction[gpio] != INPUT)
{
PyErr_SetString(WrongDirectionException, "The GPIO channel has not been set up or is set up in the wrong direction");
return NULL;
}
// printf("Input GPIO %d\n", gpio);
value = input_gpio(gpio);
if (value)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
// python function setmode(mode)
static PyObject *setmode(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, "i", &gpio_mode))
return NULL;
if (gpio_mode != BOARD && gpio_mode != BCM)
{
PyErr_SetString(InvalidModeException, "An invalid mode was passed to setmode()");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
PyMethodDef rpi_gpio_methods[] = {
{"setup", py_setup_channel, METH_VARARGS, "Set up the GPIO channel and direction\nchannel - Either: RPi board pin number (not BCM GPIO 00..nn number). Pins start from 1\n or : BCM GPIO number\ndirection - INPUT or OUTPUT"},
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel"},
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel"},
{"setmode", setmode, METH_VARARGS, "Set up numbering mode to use for channels.\nBOARD - Use Raspberry Pi board numbers\nBCM - Use Broadcom GPIO 00..nn numbers"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION > 2
static struct PyModuleDef rpigpiomodule = {
PyModuleDef_HEAD_INIT,
"RPi.GPIO", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
rpi_gpio_methods
};
#endif
#if PY_MAJOR_VERSION > 2
PyMODINIT_FUNC PyInit_GPIO(void)
#else
PyMODINIT_FUNC initGPIO(void)
#endif
{
PyObject *module = NULL;
#if PY_MAJOR_VERSION > 2
if ((module = PyModule_Create(&rpigpiomodule)) == NULL)
goto exit;
#else
if ((module = Py_InitModule("RPi.GPIO", rpi_gpio_methods)) == NULL)
goto exit;
#endif
WrongDirectionException = PyErr_NewException("RPi.GPIO.WrongDirectionException", NULL, NULL);
PyModule_AddObject(module, "WrongDirectionException", WrongDirectionException);
InvalidModeException = PyErr_NewException("RPi.GPIO.InvalidModeException", NULL, NULL);
PyModule_AddObject(module, "InvalidModeException", InvalidModeException);
InvalidDirectionException = PyErr_NewException("RPi.GPIO.InvalidDirectionException", NULL, NULL);
PyModule_AddObject(module, "InvalidDirectionException", InvalidDirectionException);
InvalidChannelException = PyErr_NewException("RPi.GPIO.InvalidChannelException", NULL, NULL);
PyModule_AddObject(module, "InvalidChannelException", InvalidChannelException);
ModeNotSetException = PyErr_NewException("RPi.GPIO.ModeNotSetException", NULL, NULL);
PyModule_AddObject(module, "ModeNotSetException", ModeNotSetException);
SetupException = PyErr_NewException("RPi.GPIO.SetupException", NULL, NULL);
PyModule_AddObject(module, "SetupException", SetupException);
high = Py_BuildValue("i", HIGH);
PyModule_AddObject(module, "HIGH", high);
low = Py_BuildValue("i", LOW);
PyModule_AddObject(module, "LOW", low);
output = Py_BuildValue("i", OUTPUT);
PyModule_AddObject(module, "OUT", output);
input = Py_BuildValue("i", INPUT);
PyModule_AddObject(module, "IN", input);
board = Py_BuildValue("i", BOARD);
PyModule_AddObject(module, "BOARD", board);
bcm = Py_BuildValue("i", BCM);
PyModule_AddObject(module, "BCM", bcm);
if (module_setup() != SETUP_OK)
{
#if PY_MAJOR_VERSION > 2
return NULL;
#else
return;
#endif
}
if (Py_AtExit(gpio_cleanup) != 0)
gpio_cleanup();
goto exit;
exit:
#if PY_MAJOR_VERSION > 2
return module;
#else
return;
#endif
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.