// python function py_wait_for_edge(gpio, edge) static PyObject *py_wait_for_edge(PyObject *self, PyObject *args) { unsigned int gpio; int channel, edge, result; char error[30]; if (!PyArg_ParseTuple(args, "ii", &channel, &edge)) return NULL; if (get_gpio_number(channel, &gpio)) return NULL; // check channel is setup as an input if (gpio_direction[gpio] != INPUT) { PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an input first"); return NULL; } // is edge a valid value? edge -= PY_EVENT_CONST_OFFSET; if (edge != RISING_EDGE && edge != FALLING_EDGE && edge != BOTH_EDGE) { PyErr_SetString(PyExc_ValueError, "The edge must be set to RISING, FALLING or BOTH"); return NULL; } if (check_gpio_priv()) return NULL; Py_BEGIN_ALLOW_THREADS // disable GIL result = blocking_wait_for_edge(gpio, edge); Py_END_ALLOW_THREADS // enable GIL if (result == 0) { Py_INCREF(Py_None); return Py_None; } else if (result == 1) { PyErr_SetString(PyExc_RuntimeError, "Edge detection events already enabled for this GPIO channel"); return NULL; } else { sprintf(error, "Error #%d waiting for edge", result); PyErr_SetString(PyExc_RuntimeError, error); return NULL; } Py_RETURN_NONE; }
// python function py_wait_for_edge(gpio, edge) static PyObject *py_wait_for_edge(PyObject *self, PyObject *args) { unsigned int gpio; unsigned int sys_gpio; int channel, edge, result; char error[30]; if (!PyArg_ParseTuple(args, "ii", &channel, &edge)) return NULL; if (get_gpio_number(channel, &gpio, &sys_gpio)) return NULL; // check channel is setup as an input if (gpio_direction[sys_gpio] != INPUT) { PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an input first"); return NULL; } //check channel whether support the event detected or not if(is_a20_platform()) { if(support_event_detect(gpio, gpioEdge_BP) < 0) { PyErr_SetString(PyExc_RuntimeError, "the pin doesn't support to detect the external event!"); return NULL; } } else if(is_s500_platform()) { if(support_event_detect(gpio, gpioEdge_GT) < 0) { PyErr_SetString(PyExc_RuntimeError, "the pin doesn't support to detect the external event!"); return NULL; } } // is edge a valid value? edge -= PY_EVENT_CONST_OFFSET; if(is_a20_platform()) { if (edge != RISING_EDGE && edge != FALLING_EDGE && edge != BOTH_EDGE) { PyErr_SetString(PyExc_ValueError, "The edge must be set to RISING, FALLING or BOTH"); return NULL; } } else if(is_s500_platform()) //s500 doesn't support the "BOTH" trigger mode. { if (edge != RISING_EDGE && edge != FALLING_EDGE) { PyErr_SetString(PyExc_ValueError, "The edge must be set to RISING and FALLING"); return NULL; } } if (check_gpio_priv()) return NULL; Py_BEGIN_ALLOW_THREADS // disable GIL result = blocking_wait_for_edge(sys_gpio, edge); Py_END_ALLOW_THREADS // enable GIL if (result == 0) { Py_INCREF(Py_None); return Py_None; } else if (result == 1) { PyErr_SetString(PyExc_RuntimeError, "Edge detection events already enabled for this GPIO channel"); return NULL; } else { sprintf(error, "Error #%d waiting for edge", result); PyErr_SetString(PyExc_RuntimeError, error); return NULL; } Py_RETURN_NONE; }