54 static char*
FUNCTIONS[] = {
"IN",
"OUT",
"ALT5",
"ALT4",
"ALT0",
"ALT1",
"ALT2",
"ALT3",
"PWM"};
55 static char*
PWM_MODES[] = {
"none",
"ratio",
"angle"};
69 PyErr_SetString(
_SetupException,
"No access to /dev/mem. Try running as root!");
88 if (!PyArg_ParseTuple(args,
"i", &channel))
98 return Py_BuildValue(
"i", f);
111 if (!PyArg_ParseTuple(args,
"i", &channel))
122 return Py_BuildValue(
"s", str);
132 int channel,
function;
134 static char *kwlist[] = {
"channel",
"function",
"pull_up_down", NULL};
136 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"ii|i", kwlist, &channel, &
function, &pud))
139 if (
function !=
IN &&
function !=
OUT &&
function !=
PWM)
145 if (
function ==
OUT ||
function ==
PWM)
150 PyErr_SetString(
_InvalidPullException,
"Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN");
167 static PyObject *
py_input(PyObject *
self, PyObject *args)
175 if (!PyArg_ParseTuple(args,
"i", &channel))
191 static PyObject *
py_output(PyObject *
self, PyObject *args, PyObject *kwargs)
198 static char *kwlist[] = {
"channel",
"value", NULL};
200 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"ii", kwlist, &channel, &value))
230 static char *kwlist[] = {
"channel",
"period",
"sequence", NULL};
232 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"iis", kwlist, &channel, &period, &sequence))
254 static PyObject *
py_pulseMilli(PyObject *
self, PyObject *args, PyObject *kwargs)
260 int channel,
function, up, down;
261 static char *kwlist[] = {
"channel",
"up",
"down", NULL};
263 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"iii", kwlist, &channel, &up, &down))
273 if ((
function !=
OUT) && (
function !=
PWM))
292 int channel,
function, width;
294 static char *kwlist[] = {
"channel",
"width",
"ratio", NULL};
296 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"iif", kwlist, &channel, &width, &ratio))
306 if ((
function !=
OUT) && (
function !=
PWM))
319 static PyObject *
py_pulseMicro(PyObject *
self, PyObject *args, PyObject *kwargs)
325 int channel,
function, up, down;
326 static char *kwlist[] = {
"channel",
"up",
"down", NULL};
328 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"iii", kwlist, &channel, &up, &down))
338 if ((
function !=
OUT) && (
function !=
PWM))
356 int channel,
function, width;
358 static char *kwlist[] = {
"channel",
"width",
"ratio", NULL};
360 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"iif", kwlist, &channel, &width, &ratio))
370 if ((
function !=
OUT) && (
function !=
PWM))
382 static PyObject *
py_pulseAngle(PyObject *
self, PyObject *args, PyObject *kwargs)
388 int channel,
function;
390 static char *kwlist[] = {
"channel",
"angle", NULL};
392 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"if", kwlist, &channel, &angle))
402 if ((
function !=
OUT) && (
function !=
PWM))
414 static PyObject *
py_pulseRatio(PyObject *
self, PyObject *args, PyObject *kwargs)
420 int channel,
function;
422 static char *kwlist[] = {
"channel",
"ratio", NULL};
424 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"if", kwlist, &channel, &ratio))
434 if ((
function !=
OUT) && (
function !=
PWM))
446 static PyObject *
py_pulse(PyObject *
self, PyObject *args)
454 if (!PyArg_ParseTuple(args,
"i", &channel))
477 if (!PyArg_ParseTuple(args,
"i", &channel))
489 #if PY_MAJOR_VERSION > 2
490 return PyUnicode_FromString(str);
492 return PyString_FromString(str);
504 if (!PyArg_ParseTuple(args,
"i", &channel))
525 if (!PyArg_ParseTuple(args,
"i", &channel))
548 if (!PyArg_ParseTuple(args,
"i", &channel))
564 {
"getFunction",
py_get_function, METH_VARARGS,
"Return the current GPIO setup (IN, OUT, ALT0)"},
565 {
"getSetup",
py_get_function, METH_VARARGS,
"Return the current GPIO setup (IN, OUT, ALT0)"},
567 {
"getFunctionString",
py_get_function_string, METH_VARARGS,
"Return the current GPIO setup (IN, OUT, ALT0) as string"},
568 {
"getSetupString",
py_get_function_string, METH_VARARGS,
"Return the current GPIO setup (IN, OUT, ALT0) as string"},
570 {
"setFunction", (PyCFunction)
py_set_function, METH_VARARGS | METH_KEYWORDS,
"Setup the GPIO channel, direction and (optional) pull/up down control\nchannel - BCM GPIO number\ndirection - IN or OUT\n[pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN"},
571 {
"setup", (PyCFunction)
py_set_function, METH_VARARGS | METH_KEYWORDS,
"Setup the GPIO channel, direction and (optional) pull/up down control\nchannel - BCM GPIO number\ndirection - IN or OUT\n[pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN"},
573 {
"input",
py_input, METH_VARARGS,
"Input from a GPIO channel - Deprecated, use digitalRead instead"},
574 {
"digitalRead",
py_input, METH_VARARGS,
"Read a GPIO channel"},
576 {
"output", (PyCFunction)
py_output, METH_VARARGS | METH_KEYWORDS,
"Output to a GPIO channel - Deprecated, use digitalWrite instead"},
577 {
"digitalWrite", (PyCFunction)
py_output, METH_VARARGS | METH_KEYWORDS,
"Write to a GPIO channel"},
579 {
"outputSequence", (PyCFunction)
py_output_sequence, METH_VARARGS | METH_KEYWORDS,
"Output a sequence to a GPIO channel"},
581 {
"getPulse",
py_getPulse, METH_VARARGS,
"Read current PWM output"},
582 {
"pwmRead",
py_getPulse, METH_VARARGS,
"Read current PWM output"},
584 {
"pulseMilli", (PyCFunction)
py_pulseMilli, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using milliseconds for both HIGH and LOW state widths"},
585 {
"pulseMilliRatio", (PyCFunction)
py_pulseMilliRatio, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using millisecond for the total width and a ratio (duty cycle) for the HIGH state width"},
586 {
"pulseMicro", (PyCFunction)
py_pulseMicro, METH_VARARGS | METH_KEYWORDS,
"Output a PWM pulse to a GPIO channel using microseconds for both HIGH and LOW state widths"},
587 {
"pulseMicroRatio", (PyCFunction)
py_pulseMicroRatio, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using microseconds for the total width and a ratio (duty cycle) for the HIGH state width"},
589 {
"pulseAngle", (PyCFunction)
py_pulseAngle, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using an angle - Deprecated, use pwmWriteAngle instead"},
590 {
"pwmWriteAngle", (PyCFunction)
py_pulseAngle, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using an angle"},
592 {
"pulseRatio", (PyCFunction)
py_pulseRatio, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using a ratio (duty cycle) with the default 50Hz signal - Deprecated, use pwmWrite instead"},
593 {
"pwmWrite", (PyCFunction)
py_pulseRatio, METH_VARARGS | METH_KEYWORDS,
"Output a PWM to a GPIO channel using a ratio (duty cycle) with the default 50Hz signal"},
595 {
"pulse",
py_pulse, METH_VARARGS,
"Output a PWM to a GPIO channel using a 50% ratio (duty cycle) with the default 50Hz signal"},
597 {
"enablePWM",
py_enablePWM, METH_VARARGS,
"Enable software PWM loop for a GPIO channel"},
598 {
"disablePWM",
py_disablePWM, METH_VARARGS,
"Disable software PWM loop of a GPIO channel"},
599 {
"isPWMEnabled",
py_isPWMEnabled, METH_VARARGS,
"Returns software PWM state"},
601 {NULL, NULL, 0, NULL}
604 #if PY_MAJOR_VERSION > 2
605 static struct PyModuleDef python_module = {
606 PyModuleDef_HEAD_INIT,
615 #if PY_MAJOR_VERSION > 2
616 PyMODINIT_FUNC PyInit_GPIO(
void)
621 PyObject *module = NULL;
624 #if PY_MAJOR_VERSION > 2
625 if ((module = PyModule_Create(&python_module)) == NULL)
628 if ((module = Py_InitModule(
"_webiopi.GPIO",
python_methods)) == NULL)
632 _SetupException = PyErr_NewException(
"_webiopi.GPIO.SetupException", NULL, NULL);
645 PyModule_AddObject(module,
"GPIO_COUNT",
_gpioCount);
647 _low = Py_BuildValue(
"i",
LOW);
648 PyModule_AddObject(module,
"LOW",
_low);
651 PyModule_AddObject(module,
"HIGH",
_high);
653 _in = Py_BuildValue(
"i",
IN);
654 PyModule_AddObject(module,
"IN",
_in);
656 _out = Py_BuildValue(
"i",
OUT);
657 PyModule_AddObject(module,
"OUT",
_out);
660 PyModule_AddObject(module,
"ALT0",
_alt0);
663 PyModule_AddObject(module,
"ALT1",
_alt1);
666 PyModule_AddObject(module,
"ALT2",
_alt2);
669 PyModule_AddObject(module,
"ALT3",
_alt3);
672 PyModule_AddObject(module,
"ALT4",
_alt4);
675 PyModule_AddObject(module,
"ALT5",
_alt5);
677 _pwm = Py_BuildValue(
"i",
PWM);
678 PyModule_AddObject(module,
"PWM",
_pwm);
681 PyModule_AddObject(module,
"PUD_OFF",
_pud_off);
684 PyModule_AddObject(module,
"PUD_UP",
_pud_up);
687 PyModule_AddObject(module,
"PUD_DOWN",
_pud_down);
693 PyErr_SetString(
_SetupException,
"This module can only be run on a Raspberry Pi!");
694 #if PY_MAJOR_VERSION > 2
707 #if PY_MAJOR_VERSION > 2
715 #if PY_MAJOR_VERSION > 2
static PyObject * py_pulseAngle(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * _board_revision
PyMODINIT_FUNC initGPIO(void)
static int module_setup(void)
static char * FUNCTIONS[]
static PyObject * py_pulseMilliRatio(PyObject *self, PyObject *args, PyObject *kwargs)
void output(int gpio, int value)
static PyObject * _pud_up
static PyObject * py_get_function_string(PyObject *self, PyObject *args)
static PyObject * _SetupException
static PyObject * _gpioCount
void pulseMicro(int gpio, int up, int down)
#define SETUP_DEVMEM_FAIL
static PyObject * py_enablePWM(PyObject *self, PyObject *args)
static PyObject * py_pulse(PyObject *self, PyObject *args)
void disablePWM(int gpio)
static char * PWM_MODES[]
static PyObject * py_getPulse(PyObject *self, PyObject *args)
PyMethodDef python_methods[]
struct pulse * getPulse(int gpio)
static PyObject * _InvalidPullException
static PyObject * py_output_sequence(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * py_input(PyObject *self, PyObject *args)
static PyObject * _pud_down
void pulseMicroRatio(int gpio, int width, float ratio)
static PyObject * py_isPWMEnabled(PyObject *self, PyObject *args)
static PyObject * py_get_function(PyObject *self, PyObject *args)
static PyObject * py_pulseRatio(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * py_output(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * _pud_off
static PyObject * py_pulseMicroRatio(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * py_pulseMilli(PyObject *self, PyObject *args, PyObject *kwargs)
int get_rpi_revision(void)
static PyObject * py_disablePWM(PyObject *self, PyObject *args)
#define SETUP_MALLOC_FAIL
void outputSequence(int gpio, int period, char *sequence)
static PyObject * py_set_function(PyObject *self, PyObject *args, PyObject *kwargs)
void pulseMilli(int gpio, int up, int down)
static PyObject * _InvalidDirectionException
int isPWMEnabled(int gpio)
void pulseMilliRatio(int gpio, int width, float ratio)
static PyObject * _InvalidChannelException
static PyObject * py_pulseMicro(PyObject *self, PyObject *args, PyObject *kwargs)
void set_function(int gpio, int function, int pud)
void pulseRatio(int gpio, float ratio)
void pulseAngle(int gpio, float angle)
int get_function(int gpio)