forked from tshorng/ScratchGPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOboard.c
204 lines (176 loc) · 4.73 KB
/
IOboard.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**** Python module for LCD, Data Flash, A/D D/A converters on IOboard
gcc -shared -I/usr/include/python2.7/ -lpython2.7 -o IOboard.so IOboard.c -I/usr/local/include -L/usr/local/lib -lwiringPi -lwiringPiDev
****/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <Python.h>
#include <errno.h>
#include <wiringPi.h>
#include <lcd.h>
#define I2C_BASE 100
int readPCF8591(int channel)
{
int value = analogRead(I2C_BASE + channel);
printf("I2C PCF8591: channel %d input: %d per 255 * Standard. \n", channel, value);
return value;
}
int writePCF8591(int out)
{
analogWrite(I2C_BASE, out);
printf("I2C PCF8591: analog output: %d per 255 * Standard. \n", out);
}
unsigned char readAT45Buff(void)
{
int ret;
unsigned char rBuf[6] = {0xD4, 0xff, 0x00, 0x02, 0xff, 0xff}; //read data from buffer 1
ret = wiringPiSPIDataRW(0, rBuf, sizeof(rBuf));
if(ret < 0)
{
printf("Read data from the AT45DB041D failed!\n");
return;
}
return rBuf[5];
}
void writeAT45Buff(unsigned char val)
{
int ret;
unsigned char wBuf[5] = {0x84, 0xff, 0x00, 0x02,0xff}; //write data to buffer 1
wBuf[4] = val;
printf("SPI: write data: 0x%02x\n", wBuf[4]);
ret = wiringPiSPIDataRW(0, wBuf, sizeof(wBuf));
if(ret < 0)
{
printf("Write data to the AT45DB041D failed!\n");
return;
}
}
int initLCD(int rows, int cols, int bits)
{
int LCDhandler;
LCDhandler = lcdInit (rows, cols, bits,21 ,23 ,0,1,2,3,4,5,6,7) ;
if(LCDhandler < 0)
{
printf("LCD initialization failed!\n");
return;
}
lcdPosition (LCDhandler, 0, 0) ; lcdPuts (LCDhandler, "Hello World") ;
return LCDhandler;
}
void writeLCD(int LCDhandler, int x, int y, char *argv)
{
lcdPosition (LCDhandler, x, y) ;
lcdPuts (LCDhandler, argv) ;
}
static PyObject *
IOboard_init(PyObject *self, PyObject *args)
{
int address;
if (!PyArg_ParseTuple(args, "i", &address))
return NULL;
int fd = wiringPiSPISetup(0, 5000000); //channel:0 5M
if(fd < 0)
{
printf("Open the SPI device failed!\n");
return NULL;
}
pcf8591Setup(I2C_BASE, address);
return Py_BuildValue("i", wiringPiSetup());
}
static PyObject *
IOBoard_readPCF8591(PyObject *self, PyObject *args)
{
int channel, value;
if (!PyArg_ParseTuple(args, "i", &channel))
return NULL;
value = readPCF8591(channel);
return Py_BuildValue("i", value);
}
static PyObject *
IOBoard_writePCF8591(PyObject *self, PyObject *args)
{
int out;
if (!PyArg_ParseTuple(args, "i", &out))
return NULL;
writePCF8591(out);
return Py_BuildValue("");
}
static PyObject *
IOBoard_readAT45Buff(PyObject *self, PyObject *args)
{
int value = readAT45Buff();
return Py_BuildValue("i", value);
}
static PyObject *
IOBoard_writeAT45Buff(PyObject *self, PyObject *args)
{
int out;
if (!PyArg_ParseTuple(args, "i", &out))
return NULL;
writeAT45Buff(out);
return Py_BuildValue("");
}
static PyObject *
IOBoard_initLCD(PyObject *self, PyObject *args)
{
int rows, cols, bits;
if (!PyArg_ParseTuple(args, "iii", &rows, &cols, &bits))
return NULL;
int LCDhandler;
LCDhandler = initLCD(rows, cols, bits);
return Py_BuildValue("i", LCDhandler);
}
static PyObject *
IOBoard_writeLCD(PyObject *self, PyObject *args)
{
int LCDhandler, x, y;
char *msg;
if (!PyArg_ParseTuple(args, "iiis", &LCDhandler, &x, &y, &msg))
return NULL;
writeLCD(LCDhandler, x, y, msg);
return Py_BuildValue("");
}
static PyObject *
IOBoard_clearLCD(PyObject *self, PyObject *args)
{
int LCDhandler;
if (!PyArg_ParseTuple(args, "i", &LCDhandler))
return NULL;
lcdClear(LCDhandler);
return Py_BuildValue("");
}
static PyMethodDef IOboardMethods[] = {
{"init", IOboard_init, METH_VARARGS,
"initialize IOBoard wiringPi setups"},
{"readPCF8591", IOBoard_readPCF8591, METH_VARARGS,
"read PCF8591 Analog input to Digital value"},
{"writePCF8591", IOBoard_writePCF8591, METH_VARARGS,
"write PCF8591 Digital value to Analog output"},
{"readAT45Buff", IOBoard_readAT45Buff, METH_VARARGS,
"read AT45Buff Data Flash"},
{"writeAT45Buff", IOBoard_writeAT45Buff, METH_VARARGS,
"write AT45Buff Data Flash"},
{"initLCD", IOBoard_initLCD, METH_VARARGS,
"initialize LCD1602 to display Data"},
{"writeLCD", IOBoard_writeLCD, METH_VARARGS,
"write data to LCD1602"},
{"clearLCD", IOBoard_clearLCD, METH_VARARGS,
"clear data on screen LCD1602"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initIOboard(void)
{
PyObject *m;
m = Py_InitModule("IOboard", IOboardMethods);
if (m == NULL)
return;
}