1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
}
|