Skip to content

Commit

Permalink
implementation and close #75 - enabling a timeout for the wait_for_ed…
Browse files Browse the repository at this point in the history
…ge function
  • Loading branch information
xtacocorex committed Sep 4, 2017
1 parent a0b2ac1 commit e3a077b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.6.2
---
* Implementation for #77 - ability to push up binary pypi
* Implementation for #75 - wait_for_edge timeout

0.6.1
---
* Fixing implementation for #76
Expand Down
7 changes: 7 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
chip-io (0.6.2-1) unstable; urgency=low

* Implementation for #77 - ability to push up binary pypi
* Implementation for #75 - wait_for_edge timeout

-- Robert Wolterman <[email protected]> Sun, 03 Sep 2017 21:34:00 -0600

chip-io (0.6.1-1) unstable; urgency=low

* Fixing implementation for #76
Expand Down
4 changes: 3 additions & 1 deletion docs/gpio.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,14 @@ Add callback function to a pin that has been setup for edge detection. Refer to
GPIO.add_event_callback("XIO-P7", mycallback, 45)
```

### wait_for_edge(channel, edge)
### wait_for_edge(channel, edge, timeout=-1)
Wait for an edge to be detected. This is a blocking function. Refer to main table for which pins are able to use edge detection.

* Parameters

channel - GPIO Pin
edge - edge: RISING_EDGE, FALLING_EDGE, BOTH_EDGE
timeout - timeout in milliseconds to wait before exiting function (optional)

* Returns

Expand All @@ -245,6 +246,7 @@ Wait for an edge to be detected. This is a blocking function. Refer to main tab
GPIO.wait_for_edge("XIO-P3", GPIO.RISING_EDGE)
GPIO.wait_for_edge("AP-EINT3", GPIO.BOTH_EDGE)
GPIO.wait_for_edge("I2S-DI", GPIO.FALLING_EDGE)
GPIO.wait_for_edge("XIO-P3", GPIO.RISING_EDGE, 40)
```

### gpio_function(channel)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'Topic :: System :: Hardware']

setup(name = 'CHIP_IO',
version = '0.6.1',
version = '0.6.2',
author = 'Robert Wolterman',
author_email = '[email protected]',
description = 'A module to control CHIP IO channels',
Expand Down
2 changes: 1 addition & 1 deletion source/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ void define_constants(PyObject *module)
bcm = Py_BuildValue("i", BCM);
PyModule_AddObject(module, "BCM", bcm);

version = Py_BuildValue("s", "0.6.1");
version = Py_BuildValue("s", "0.6.2");
PyModule_AddObject(module, "VERSION", version);
}
4 changes: 2 additions & 2 deletions source/event_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ void event_cleanup(void)
}

// blocking_wait_for_edge assumes the caller has ensured the GPIO is already exported.
int blocking_wait_for_edge(int gpio, unsigned int edge)
int blocking_wait_for_edge(int gpio, unsigned int edge, int timeout)
// standalone from all the event functions above
{
int fd = fd_lookup(gpio);
Expand Down Expand Up @@ -1082,7 +1082,7 @@ int blocking_wait_for_edge(int gpio, unsigned int edge)
// epoll for event
for (i = 0; i<2; i++) // first time triggers with current state, so ignore
{
if ((n = epoll_wait(epfd, &events, 1, -1)) == -1)
if ((n = epoll_wait(epfd, &events, 1, timeout)) == -1)
{
gpio_event_remove(gpio);
return 5;
Expand Down
2 changes: 1 addition & 1 deletion source/event_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ int gpio_event_remove(int gpio);
int gpio_is_evented(int gpio);
int event_initialise(void);
void event_cleanup(void);
int blocking_wait_for_edge(int gpio, unsigned int edge);
int blocking_wait_for_edge(int gpio, unsigned int edge, int timeout);
11 changes: 6 additions & 5 deletions source/py_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,18 +817,19 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
Py_RETURN_FALSE;
}

// python function py_wait_for_edge(gpio, edge)
// python function py_wait_for_edge(gpio, edge, timeout = -1)
static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
{
int gpio;
int edge, result;
int edge, result, timeout;
char *channel;
char error[81];
int allowed = -1;

clear_error_msg();

if (!PyArg_ParseTuple(args, "si", &channel, &edge))
timeout = -1;
if (!PyArg_ParseTuple(args, "si|i", &channel, &edge, &timeout))
return NULL;

if (get_gpio_number(channel, &gpio)) {
Expand Down Expand Up @@ -877,7 +878,7 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
}

Py_BEGIN_ALLOW_THREADS // disable GIL
result = blocking_wait_for_edge(gpio, edge);
result = blocking_wait_for_edge(gpio, edge, timeout);
Py_END_ALLOW_THREADS // enable GIL

if (result == 0) {
Expand Down Expand Up @@ -1179,7 +1180,7 @@ PyMethodDef gpio_methods[] = {
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\ngpio - gpio channel"},
{"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"},
{"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"},
{"wait_for_edge", py_wait_for_edge, METH_VARARGS, "Wait for an edge.\ngpio - gpio channel\nedge - RISING, FALLING or BOTH"},
{"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)"},
{"gpio_function", py_gpio_function, METH_VARARGS, "Return the current GPIO function (IN, OUT, ALT0)\ngpio - gpio channel"},
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{"get_gpio_base", py_gpio_base, METH_VARARGS, "Get the XIO base number for sysfs"},
Expand Down

0 comments on commit e3a077b

Please sign in to comment.