summaryrefslogtreecommitdiff
path: root/src/gpio.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpio.cpp')
-rw-r--r--src/gpio.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/gpio.cpp b/src/gpio.cpp
new file mode 100644
index 0000000..478bcc7
--- /dev/null
+++ b/src/gpio.cpp
@@ -0,0 +1,59 @@
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+
+#include <hwd.h>
+
+
+static PyObject * gpio_set_mode(PyObject * self, PyObject * args)
+{
+ unsigned port;
+ short mode;
+ if (!PyArg_ParseTuple(args, "Ih", &port, &mode))
+ return NULL;
+ hwd::gpio::set_mode(port, mode);
+ Py_RETURN_NONE;
+}
+
+
+static PyObject * gpio_write(PyObject * self, PyObject * args)
+{
+ unsigned port;
+ short value;
+ if (!PyArg_ParseTuple(args, "Ih", &port, &value))
+ return NULL;
+ hwd::gpio::write(port, value);
+ Py_RETURN_NONE;
+}
+
+
+static PyObject * gpio_read(PyObject * self, PyObject * args)
+{
+ unsigned port;
+ if (!PyArg_ParseTuple(args, "I", &port))
+ return NULL;
+ const short value = hwd::gpio::read(port);
+ return PyLong_FromLong(value);
+}
+
+
+static PyMethodDef gpio_methods[] = {
+ {"set_mode", gpio_set_mode, METH_VARARGS, "set_mode(port, mode)\n--\n\nSets *mode* of a *port*."},
+ {"write", gpio_write, METH_VARARGS, "write(port, value)\n--\n\nWrites *value* to a *port*."},
+ {"read", gpio_read, METH_VARARGS, "read(port)\n--\n\nReads a value from a *port*."},
+ {NULL, NULL, 0, NULL}
+};
+
+
+static struct PyModuleDef gpio_module = {
+ PyModuleDef_HEAD_INIT,
+ "gpio",
+ NULL,
+ -1,
+ gpio_methods
+};
+
+
+PyMODINIT_FUNC PyInit_gpio(void)
+{
+ return PyModule_Create(&gpio_module);
+}