// python function setup(channel, direction, pull_up_down=PUD_OFF, initial=None) static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned int gpio; int channel, direction; int pud = PUD_OFF + PY_PUD_CONST_OFFSET; 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; // check module has been imported cleanly if (setup_error) { PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!"); return NULL; } if (get_gpio_number(channel, &gpio)) return NULL; if (direction != INPUT && direction != OUTPUT) { PyErr_SetString(PyExc_ValueError, "An invalid direction was passed to setup()"); return NULL; } if (direction == OUTPUT) pud = PUD_OFF + PY_PUD_CONST_OFFSET; pud -= PY_PUD_CONST_OFFSET; if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP) { PyErr_SetString(PyExc_ValueError, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN"); return NULL; } if (mmap_gpio_mem()) 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 GPIO.setwarnings(False) to disable warnings.", 1); } if (direction == OUTPUT && (initial == LOW || initial == HIGH)) { output_gpio(gpio, initial); } setup_gpio(gpio, direction, pud); gpio_direction[gpio] = direction; Py_RETURN_NONE; }
// python function value = gpio_function(channel) static PyObject *py_gpio_function(PyObject *self, PyObject *args) { unsigned int gpio; int channel; int f; PyObject *func; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if (get_gpio_number(channel, &gpio)) return NULL; if (mmap_gpio_mem()) return NULL; f = gpio_function(gpio); switch (f) { case 0 : f = INPUT; break; case 1 : f = OUTPUT; break; // ALT 0 case 4 : switch (gpio) { case 0 : case 1 : case 2 : case 3 : f = I2C; break; case 7 : case 8 : case 9 : case 10 : case 11 : f = SPI; break; case 12 : case 13 : f = PWM; break; case 14 : case 15 : f = SERIAL; break; case 28 : case 29 : f = I2C; break; default : f = MODE_UNKNOWN; break; } break; // ALT 5 case 2 : if (gpio == 18 || gpio == 19) f = PWM; else f = MODE_UNKNOWN; break; // ALT 4 case 3 : switch (gpio) { case 16 : case 17 : case 18 : case 19 : case 20 : case 21 : f = SPI; break; default : f = MODE_UNKNOWN; break; } break; default : f = MODE_UNKNOWN; break; } func = Py_BuildValue("i", f); return func; }
PyMODINIT_FUNC initGPIO(void) #endif { int i; PyObject *module = NULL; char cpuRev[1024] = {'\0'}; #if PY_MAJOR_VERSION > 2 if ((module = PyModule_Create(&lmkgpiomodule)) == NULL) return NULL; #else if ((module = Py_InitModule3("LMK.GPIO", lmk_gpio_methods, moduledocstring)) == NULL) return; #endif if (get_cpuinfo_revision(cpuRev) == NULL) #if PY_MAJOR_VERSION > 2 return NULL; #else return; #endif revision = get_lmk_revision(); debug("BOARD: revision(%d)\n",revision); define_constants(module); for (i=0; i<256; i++) gpio_direction[i] = -1; if (revision == -1) { PyErr_SetString(PyExc_RuntimeError, "This module can only be run on a Raspberry Pi!"); setup_error = 1; #if PY_MAJOR_VERSION > 2 return NULL; #else return; #endif } lmk_revision = Py_BuildValue("i", revision); PyModule_AddObject(module, "LMK_REVISION", lmk_revision); // Add PWM class if (PWM_init_PWMType() == NULL){ #if PY_MAJOR_VERSION > 2 return NULL; #else return; #endif } Py_INCREF(&PWMType); PyModule_AddObject(module, "PWM", (PyObject*)&PWMType); if (!PyEval_ThreadsInitialized()){ PyEval_InitThreads(); } //Initialized only once if (mmap_gpio_mem()) #if PY_MAJOR_VERSION > 2 return NULL; #else return; #endif // register exit functions - last declared is called first if (Py_AtExit(cleanup) != 0) { setup_error = 1; cleanup(); #if PY_MAJOR_VERSION > 2 return NULL; #else return; #endif } if (Py_AtExit(event_cleanup_all) != 0) { setup_error = 1; cleanup(); #if PY_MAJOR_VERSION > 2 return NULL; #else return; #endif } #if PY_MAJOR_VERSION > 2 return module; #else return; #endif }