Skip to content

Commit cf9771a

Browse files
authored
Merge pull request #124 from jpbarraca/master
Timeout support for wait_for_edge (replaces PR #62)
2 parents 8b4f7f2 + 40e5cd3 commit cf9771a

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH::
111111

112112
GPIO.wait_for_edge(channel, GPIO.RISING)
113113

114+
or
115+
116+
GPIO.wait_for_edge(channel, GPIO.RISING, timeout)
117+
114118
Detecting events::
115119

116120
GPIO.add_event_detect("P9_12", GPIO.FALLING)

source/event_gpio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ void event_cleanup(void)
624624
exports_cleanup();
625625
}
626626

627-
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge)
627+
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int timeout)
628628
// standalone from all the event functions above
629629
{
630630
int fd = fd_lookup(gpio);
@@ -661,7 +661,7 @@ int blocking_wait_for_edge(unsigned int gpio, unsigned int edge)
661661

662662
// epoll for event
663663
for (i = 0; i<2; i++) // first time triggers with current state, so ignore
664-
if ((n = epoll_wait(epfd, &events, 1, -1)) == -1)
664+
if ((n = epoll_wait(epfd, &events, 1, timeout)) == -1)
665665
{
666666
gpio_event_remove(gpio);
667667
return 5;
@@ -684,5 +684,5 @@ int blocking_wait_for_edge(unsigned int gpio, unsigned int edge)
684684

685685
gpio_event_remove(gpio);
686686
close(epfd);
687-
return 0;
687+
return (n == 1) ? 0 : -1;
688688
}

source/event_gpio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ int gpio_event_remove(unsigned int gpio);
6868
int gpio_is_evented(unsigned int gpio);
6969
int event_initialise(void);
7070
void event_cleanup(void);
71-
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge);
71+
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int timeout);
7272

7373
#endif

source/py_gpio.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,20 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
435435
Py_RETURN_FALSE;
436436
}
437437

438-
// python function py_wait_for_edge(gpio, edge)
438+
// python function py_wait_for_edge(gpio, edge, timeout = -1)
439439
static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
440440
{
441441
unsigned int gpio;
442-
int edge, result;
442+
int edge, result, timeout;
443443
char *channel;
444444
char error[30];
445445
BBIO_err err;
446446

447-
if (!PyArg_ParseTuple(args, "si", &channel, &edge))
448-
return NULL;
447+
if (!PyArg_ParseTuple(args, "sii", &channel, &edge, &timeout)){
448+
timeout = -1;
449+
if (!PyArg_ParseTuple(args, "si", &channel, &edge))
450+
return NULL;
451+
}
449452

450453
err = get_gpio_number(channel, &gpio);
451454
if (err != BBIO_OK)
@@ -466,12 +469,14 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
466469
}
467470

468471
Py_BEGIN_ALLOW_THREADS // disable GIL
469-
result = blocking_wait_for_edge(gpio, edge);
472+
result = blocking_wait_for_edge(gpio, edge, timeout);
470473
Py_END_ALLOW_THREADS // enable GIL
471474

472475
if (result == 0) {
473476
Py_INCREF(Py_None);
474477
return Py_None;
478+
}else if (result == -1){
479+
Py_RETURN_FALSE;
475480
} else if (result == 2) {
476481
PyErr_SetString(PyExc_RuntimeError, "Edge detection events already enabled for this GPIO channel");
477482
return NULL;
@@ -537,7 +542,7 @@ PyMethodDef gpio_methods[] = {
537542
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\ngpio - gpio channel"},
538543
{"event_detected", py_event_detected, METH_VARARGS, "Returns True if an edge has occured on a given GPIO. You need to enable edge detection using add_event_detect() first.\ngpio - gpio channel"},
539544
{"add_event_callback", (PyCFunction)py_add_event_callback, METH_VARARGS | METH_KEYWORDS, "Add a callback for an event already defined using add_event_detect()\ngpio - gpio channel\ncallback - a callback function\n[bouncetime] - Switch bounce timeout in ms"},
540-
{"wait_for_edge", py_wait_for_edge, METH_VARARGS, "Wait for an edge.\ngpio - gpio channel\nedge - RISING, FALLING or BOTH"},
545+
{"wait_for_edge", py_wait_for_edge, METH_VARARGS, "Wait for an edge.\ngpio - gpio channel\nedge - RISING, FALLING or BOTH\ntimeout (optional) - time to wait in miliseconds. -1 will wait forever (default)"},
541546
{"gpio_function", py_gpio_function, METH_VARARGS, "Return the current GPIO function (IN, OUT, ALT0)\ngpio - gpio channel"},
542547
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
543548
{NULL, NULL, 0, NULL}

0 commit comments

Comments
 (0)