summaryrefslogtreecommitdiff
path: root/src/hwdmodule.cpp
blob: 6d3a57403dfc3a9e6087f14d8bf87e88157e5fe7 (plain)
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 HwdMethods[] = {
	{"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 hwdmodule = {
	PyModuleDef_HEAD_INIT,
	"hwd",
	NULL,
	-1,
	HwdMethods
};


PyMODINIT_FUNC PyInit_hwd(void)
{
	return PyModule_Create(&hwdmodule);
}