// python function value = gpio_function(gpio) static PyObject* py_gpio_function(PyObject *self, PyObject *args) { int gpio, channel, f; PyObject *func; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if ((gpio = channel_to_gpio(channel)) < 0) return NULL; f = gpio_function(gpio); switch (f) { case 0 : f = INPUT; break; case 1 : f = OUTPUT; break; } func = Py_BuildValue("i", f); return func; }
static int verify_input(int channel, int *gpio) { if ((*gpio = channel_to_gpio(channel)) == -1) return 0; if ((gpio_direction[*gpio] != INPUT) && (gpio_direction[*gpio] != OUTPUT)) { PyErr_SetString(WrongDirectionException, "GPIO channel has not been set up"); return 0; } return 1; }
// channel2gpio binding static PyObject* py_channel_to_gpio(PyObject *self, PyObject *args) { int channel, gpio; PyObject *func; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if ((gpio = channel_to_gpio(channel)) < 0) return NULL; func = Py_BuildValue("i", gpio); return func; }
// python function setup(channel, direction, pull_up_down=PUD_OFF, initial=None) static PyObject* py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs) { int gpio, channel, direction; int pud = PUD_OFF; int initial = -1; static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL}; int func; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &channel, &direction, &pud, &initial)) return NULL; if (direction != INPUT && direction != OUTPUT) { PyErr_SetString(InvalidDirectionException, "An invalid direction was passed to setup()"); return NULL; } if (direction == OUTPUT) pud = PUD_OFF; if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP) { PyErr_SetString(InvalidPullException, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN"); return NULL; } if ((gpio = channel_to_gpio(channel)) < 0) return NULL; func = gpio_function(gpio); if (gpio_warnings && // warnings enabled and ((func != 0 && func != 1) || // (already one of the alt functions or (gpio_direction[gpio] == -1 && func == 1))) // already an output not set from this program) { PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use RPIO.setwarnings(False) to disable warnings.", 1); } // printf("Setup GPIO %d direction %d pud %d\n", gpio, direction, pud); if (direction == OUTPUT && (initial == LOW || initial == HIGH)) { // printf("Writing intial value %d\n",initial); output_gpio(gpio, initial); } setup_gpio(gpio, direction, pud); gpio_direction[gpio] = direction; Py_INCREF(Py_None); return Py_None; }
// python function value = input(channel) without direction check static PyObject* py_forceinput_gpio(PyObject *self, PyObject *args) { int gpio, channel; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if ((gpio = channel_to_gpio(channel)) < 0) return NULL; //printf("Input GPIO %d\n", gpio); if (input_gpio(gpio)) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
// python function output(channel, value) without direction check static PyObject* py_forceoutput_gpio(PyObject *self, PyObject *args) { int gpio, channel, value; if (!PyArg_ParseTuple(args, "ii", &channel, &value)) return NULL; // printf("Output GPIO %d value %d\n", gpio, value); if ((gpio = channel_to_gpio(channel)) < 0) return NULL; output_gpio(gpio, value); Py_INCREF(Py_None); return Py_None; }
// python function output(channel, value) without direction check static PyObject* py_set_pullupdn(PyObject *self, PyObject *args, PyObject *kwargs) { int gpio, channel; int pud = PUD_OFF; static char *kwlist[] = {"channel", "pull_up_down", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &channel, &pud)) return NULL; if ((gpio = channel_to_gpio(channel)) < 0) return NULL; // printf("Setting gpio %d PULLUPDN to %d", gpio, pud); set_pullupdn(gpio, pud); Py_INCREF(Py_None); return Py_None; }
// python function output(channel, value) static PyObject* py_output_gpio(PyObject *self, PyObject *args) { int gpio, channel, value; if (!PyArg_ParseTuple(args, "ii", &channel, &value)) return NULL; if ((gpio = channel_to_gpio(channel)) < 0) return NULL; if (gpio_direction[gpio] != OUTPUT) { PyErr_SetString(WrongDirectionException, "The GPIO channel has not been set up as an OUTPUT"); return NULL; } // printf("Output GPIO %d value %d\n", gpio, value); output_gpio(gpio, value); Py_INCREF(Py_None); return Py_None; }