// python function add_event_detect(gpio, edge, callback=None, bouncetime=0 static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned int gpio; int channel, edge, result; unsigned int bouncetime = 0; PyObject *cb_func = NULL; char *kwlist[] = {"gpio", "edge", "callback", "bouncetime", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|Oi", kwlist, &channel, &edge, &cb_func, &bouncetime)) return NULL; if (cb_func != NULL && !PyCallable_Check(cb_func)) { PyErr_SetString(PyExc_TypeError, "Parameter must be callable"); return NULL; } if (get_gpio_number(channel, &gpio)) return NULL; // check channel is set up 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 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; if ((result = add_edge_detect(gpio, edge, bouncetime)) != 0) // starts a thread { if (result == 1) { PyErr_SetString(PyExc_RuntimeError, "Edge detection already enabled for this GPIO channel"); return NULL; } else { PyErr_SetString(PyExc_RuntimeError, "Failed to add edge detection"); return NULL; } } if (cb_func != NULL) if (add_py_callback(gpio, cb_func) != 0) return NULL; Py_RETURN_NONE; }
// python function add_event_callback(gpio, callback, bouncetime=0) static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned int gpio; int channel; unsigned int bouncetime = 0; PyObject *cb_func; char *kwlist[] = {"gpio", "callback", "bouncetime", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO|i", kwlist, &channel, &cb_func, &bouncetime)) return NULL; if (!PyCallable_Check(cb_func)) { PyErr_SetString(PyExc_TypeError, "Parameter must be callable"); return NULL; } if (get_gpio_number(channel, &gpio)) return NULL; // check channel is set up as an input if (gpio_direction[gpio] != INPUT) { PyErr_SetString(WrongDirectionException, "You must setup() the GPIO channel as an input first"); return NULL; } if (!gpio_event_added(gpio)) { PyErr_SetString(AddEventException, "Add event detection using add_event_detect first before adding a callback"); return NULL; } if (add_py_callback(gpio, bouncetime, cb_func) != 0) return NULL; Py_INCREF(Py_None); return Py_None; }
// python function add_event_callback(gpio, callback) static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned int gpio; unsigned int sys_gpio; int channel; PyObject *cb_func; char *kwlist[] = {"gpio", "callback", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO|i", kwlist, &channel, &cb_func)) return NULL; if (!PyCallable_Check(cb_func)) { PyErr_SetString(PyExc_TypeError, "Parameter must be callable"); return NULL; } if (get_gpio_number(channel, &gpio, &sys_gpio)) return NULL; // check channel is set up 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; } if (!gpio_event_added(sys_gpio)) { PyErr_SetString(PyExc_RuntimeError, "Add event detection using add_event_detect first before adding a callback"); return NULL; } if (add_py_callback(sys_gpio, cb_func) != 0) return NULL; Py_RETURN_NONE; }