-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadcPythonC.c
114 lines (97 loc) · 2.84 KB
/
adcPythonC.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <Python.h>
#include "mraa_raspberry_pi_pinmap.h"
mraa_gpio_context MCP3208_DIN;
mraa_gpio_context MCP3208_DOUT;
mraa_gpio_context MCP3208_CLK;
mraa_gpio_context MCP3208_CS;
/*
* to compile and create a share object run the following command
* gcc -shared -I /usr/include/python2.7/ -l python2.7 -l mraa -o adcPythonC.so adcPythonC.c
* Function to be called from Python
*/
static PyObject* py_testFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
/*
* Read ADC Function set up for a MCP3208 using bit bang mode.
*/
static PyObject* py_adcRead(PyObject* self, PyObject* args)
{
int channel, i,val;
PyArg_ParseTuple(args, "i", &channel);
mraa_gpio_write (MCP3208_DIN, 0);
mraa_gpio_write (MCP3208_CLK, 0);
mraa_gpio_write (MCP3208_CS, 0);
channel = channel | 0x18;
for (i = 0; i < 5; i ++)
{
if (channel & 0x10)
{
mraa_gpio_write (MCP3208_DIN, 1);
}
else
{
mraa_gpio_write (MCP3208_DIN, 0);
}
channel <<= 1;
mraa_gpio_write (MCP3208_CLK, 0);
mraa_gpio_write (MCP3208_CLK, 1);
}
mraa_gpio_write (MCP3208_CLK, 0);
mraa_gpio_write (MCP3208_CLK, 1);
mraa_gpio_write (MCP3208_CLK, 0);
mraa_gpio_write (MCP3208_CLK, 1);
val = 0;
for (i = 0; i < 12; i ++)
{
mraa_gpio_write (MCP3208_CLK, 0);
mraa_gpio_write (MCP3208_CLK, 1);
val = (val << 1) | ((int) mraa_gpio_read (MCP3208_DOUT));
}
mraa_gpio_write (MCP3208_CS, 1);
mraa_gpio_write (MCP3208_DIN, 0);
mraa_gpio_write (MCP3208_CLK, 0);
return Py_BuildValue("i", val);
}
/*
* Bind Python function names to the C functions
*/
static PyMethodDef adcPythonC_methods[] = {
{"testFunction", py_testFunction, METH_VARARGS},
{"adcRead", py_adcRead, METH_VARARGS},
{NULL, NULL}
};
/*
* This is called on initialization of the module.
* Put set up information here
*
void initadcPythonC()
{
(void) Py_InitModule("adcPythonC", adcPythonC_methods);
MCP3208_DIN = mraa_gpio_init (SPI_MOSI_PIN);
MCP3208_DOUT = mraa_gpio_init (SPI_MISO_PIN);
MCP3208_CLK = mraa_gpio_init (SPI_CLK_PIN);
MCP3208_CS = mraa_gpio_init (SPI_CS0_PIN);
sleep(1); // Need a delay before setting the direction.
mraa_gpio_dir(MCP3208_DIN, MRAA_GPIO_OUT_HIGH);
mraa_gpio_dir(MCP3208_DOUT, MRAA_GPIO_IN);
mraa_gpio_dir(MCP3208_CLK, MRAA_GPIO_OUT);
mraa_gpio_dir(MCP3208_CS, MRAA_GPIO_OUT); */
static struct PyModuleDef adcPythonC =
{
PyModuleDef_HEAD_INIT,
"adcPythonC", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
adcPythonC_methods
};
/*
* This is called on initialization of the module.
* Put set up information here - utilizing Python 3
*/
PyMODINIT_FUNC PyInit_adcPythonC(void)
{
return PyModule_Create(&adcPythonC);
}