Skip to content

Commit 7b1f0d4

Browse files
committed
Added support for timeouts in wait_for_edge
1 parent 63b26ac commit 7b1f0d4

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

README.rst

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

9090
GPIO.wait_for_edge(channel, GPIO.RISING)
9191

92+
or
93+
94+
GPIO.wait_for_edge(channel, GPIO.RISING, timeout)
95+
9296
Detecting events::
9397

9498
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
@@ -587,7 +587,7 @@ void event_cleanup(void)
587587
exports_cleanup();
588588
}
589589

590-
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge)
590+
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int timeout)
591591
// standalone from all the event functions above
592592
{
593593
int fd = fd_lookup(gpio);
@@ -624,7 +624,7 @@ int blocking_wait_for_edge(unsigned int gpio, unsigned int edge)
624624

625625
// epoll for event
626626
for (i = 0; i<2; i++) // first time triggers with current state, so ignore
627-
if ((n = epoll_wait(epfd, &events, 1, -1)) == -1)
627+
if ((n = epoll_wait(epfd, &events, 1, timeout)) == -1)
628628
{
629629
gpio_event_remove(gpio);
630630
return 5;
@@ -647,5 +647,5 @@ int blocking_wait_for_edge(unsigned int gpio, unsigned int edge)
647647

648648
gpio_event_remove(gpio);
649649
close(epfd);
650-
return 0;
650+
return (n == 1) ? 0 : -1;
651651
}

source/event_gpio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ int gpio_event_remove(unsigned int gpio);
6161
int gpio_is_evented(unsigned int gpio);
6262
int event_initialise(void);
6363
void event_cleanup(void);
64-
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge);
64+
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int timeout);

source/py_gpio.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,14 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
388388
static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
389389
{
390390
unsigned int gpio;
391-
int edge, result;
391+
int edge, result, timeout;
392392
char *channel;
393393
char error[30];
394394

395-
if (!PyArg_ParseTuple(args, "si", &channel, &edge))
396-
return NULL;
395+
if (!PyArg_ParseTuple(args, "sii", &channel, &edge, &timeout))
396+
timeout = -1
397+
if (!PyArg_ParseTuple(args, "si", &channel, &edge))
398+
return NULL;
397399

398400
if (get_gpio_number(channel, &gpio))
399401
return NULL;
@@ -413,12 +415,14 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
413415
}
414416

415417
Py_BEGIN_ALLOW_THREADS // disable GIL
416-
result = blocking_wait_for_edge(gpio, edge);
418+
result = blocking_wait_for_edge(gpio, edge, timeout);
417419
Py_END_ALLOW_THREADS // enable GIL
418420

419421
if (result == 0) {
420422
Py_INCREF(Py_None);
421423
return Py_None;
424+
}else if (result == -1){
425+
Py_RETURN_FALSE
422426
} else if (result == 2) {
423427
PyErr_SetString(PyExc_RuntimeError, "Edge detection events already enabled for this GPIO channel");
424428
return NULL;
@@ -483,7 +487,7 @@ PyMethodDef gpio_methods[] = {
483487
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\ngpio - gpio channel"},
484488
{"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"},
485489
{"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"},
486-
{"wait_for_edge", py_wait_for_edge, METH_VARARGS, "Wait for an edge.\ngpio - gpio channel\nedge - RISING, FALLING or BOTH"},
490+
{"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)"},
487491
{"gpio_function", py_gpio_function, METH_VARARGS, "Return the current GPIO function (IN, OUT, ALT0)\ngpio - gpio channel"},
488492
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
489493
{NULL, NULL, 0, NULL}

0 commit comments

Comments
 (0)