wiring_lmk 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/ext/wiringpi/WiringPi/devLib/ds1302.c +254 -0
  3. data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
  4. data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
  5. data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
  6. data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
  7. data/ext/wiringpi/WiringPi/devLib/lcd.c +507 -0
  8. data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
  9. data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +684 -0
  10. data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
  11. data/ext/wiringpi/WiringPi/devLib/maxdetect.c +167 -0
  12. data/ext/wiringpi/WiringPi/devLib/maxdetect.h +40 -0
  13. data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
  14. data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
  15. data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +178 -0
  16. data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
  17. data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
  18. data/ext/wiringpi/WiringPi/devLib/piNes.c +117 -0
  19. data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
  20. data/ext/wiringpi/WiringPi/gpio/extensions.c +700 -0
  21. data/ext/wiringpi/WiringPi/gpio/extensions.h +26 -0
  22. data/ext/wiringpi/WiringPi/gpio/gpio.c +1280 -0
  23. data/ext/wiringpi/WiringPi/gpio/pins.c +33 -0
  24. data/ext/wiringpi/WiringPi/gpio/readall.c +337 -0
  25. data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +201 -0
  26. data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
  27. data/ext/wiringpi/WiringPi/wiringPi/max31855.c +99 -0
  28. data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
  29. data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
  30. data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
  31. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
  32. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
  33. data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
  34. data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
  35. data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
  36. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
  37. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
  38. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +189 -0
  39. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
  40. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +234 -0
  41. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
  42. data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
  43. data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
  44. data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
  45. data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
  46. data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
  47. data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
  48. data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +113 -0
  49. data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
  50. data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
  51. data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
  52. data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
  53. data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
  54. data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +92 -0
  55. data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
  56. data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
  57. data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
  58. data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
  59. data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
  60. data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +168 -0
  61. data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
  62. data/ext/wiringpi/WiringPi/wiringPi/softServo.c +247 -0
  63. data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
  64. data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
  65. data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
  66. data/ext/wiringpi/WiringPi/wiringPi/sr595.c +113 -0
  67. data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
  68. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +3688 -0
  69. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +229 -0
  70. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +236 -0
  71. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
  72. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +121 -0
  73. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +35 -0
  74. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +253 -0
  75. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
  76. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
  77. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
  78. data/ext/wiringpi/extconf.rb +8 -0
  79. data/ext/wiringpi/wiringpi_wrap.c +4396 -0
  80. data/lib/wiring_lmk.rb +42 -0
  81. data/lib/wiring_lmk/event.rb +21 -0
  82. data/lib/wiring_lmk/gpio.rb +139 -0
  83. data/lib/wiring_lmk/i2c.rb +26 -0
  84. data/lib/wiring_lmk/mcp23x17.rb +31 -0
  85. data/lib/wiring_lmk/serial.rb +49 -0
  86. data/lib/wiring_lmk/spi.rb +15 -0
  87. metadata +136 -0
@@ -0,0 +1,164 @@
1
+ /*
2
+ * gertboard.c:
3
+ * Access routines for the SPI devices on the Gertboard
4
+ * Copyright (c) 2012 Gordon Henderson
5
+ *
6
+ * The Gertboard has:
7
+ *
8
+ * An MCP3002 dual-channel A to D convertor connected
9
+ * to the SPI bus, selected by chip-select A, and:
10
+ *
11
+ * An MCP4802 dual-channel D to A convertor connected
12
+ * to the SPI bus, selected via chip-select B.
13
+ *
14
+ ***********************************************************************
15
+ * This file is part of wiringPi:
16
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
17
+ *
18
+ * wiringPi is free software: you can redistribute it and/or modify
19
+ * it under the terms of the GNU Lesser General Public License as
20
+ * published by the Free Software Foundation, either version 3 of the
21
+ * License, or (at your option) any later version.
22
+ *
23
+ * wiringPi is distributed in the hope that it will be useful,
24
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
+ * GNU Lesser General Public License for more details.
27
+ *
28
+ * You should have received a copy of the GNU Lesser General Public
29
+ * License along with wiringPi.
30
+ * If not, see <http://www.gnu.org/licenses/>.
31
+ ***********************************************************************
32
+ */
33
+
34
+
35
+ #include <stdio.h>
36
+ #include <stdint.h>
37
+ #include <fcntl.h>
38
+ #include <sys/ioctl.h>
39
+ #include <linux/spi/spidev.h>
40
+
41
+ #include "wiringPi.h"
42
+ #include "wiringPiSPI.h"
43
+
44
+ #include "gertboard.h"
45
+
46
+ // The A-D convertor won't run at more than 1MHz @ 3.3v
47
+
48
+ #define SPI_ADC_SPEED 1000000
49
+ #define SPI_DAC_SPEED 1000000
50
+ #define SPI_A2D 0
51
+ #define SPI_D2A 1
52
+
53
+
54
+ /*
55
+ * gertboardAnalogWrite:
56
+ * Write an 8-bit data value to the MCP4802 Analog to digital
57
+ * convertor on the Gertboard.
58
+ *********************************************************************************
59
+ */
60
+
61
+ void gertboardAnalogWrite (const int chan, const int value)
62
+ {
63
+ uint8_t spiData [2] ;
64
+ uint8_t chanBits, dataBits ;
65
+
66
+ if (chan == 0)
67
+ chanBits = 0x30 ;
68
+ else
69
+ chanBits = 0xB0 ;
70
+
71
+ chanBits |= ((value >> 4) & 0x0F) ;
72
+ dataBits = ((value << 4) & 0xF0) ;
73
+
74
+ spiData [0] = chanBits ;
75
+ spiData [1] = dataBits ;
76
+
77
+ wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
78
+ }
79
+
80
+
81
+ /*
82
+ * gertboardAnalogRead:
83
+ * Return the analog value of the given channel (0/1).
84
+ * The A/D is a 10-bit device
85
+ *********************************************************************************
86
+ */
87
+
88
+ int gertboardAnalogRead (const int chan)
89
+ {
90
+ uint8_t spiData [2] ;
91
+
92
+ uint8_t chanBits ;
93
+
94
+ if (chan == 0)
95
+ chanBits = 0b11010000 ;
96
+ else
97
+ chanBits = 0b11110000 ;
98
+
99
+ spiData [0] = chanBits ;
100
+ spiData [1] = 0 ;
101
+
102
+ wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
103
+
104
+ return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
105
+ }
106
+
107
+
108
+ /*
109
+ * gertboardSPISetup:
110
+ * Initialise the SPI bus, etc.
111
+ *********************************************************************************
112
+ */
113
+
114
+ int gertboardSPISetup (void)
115
+ {
116
+ if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
117
+ return -1 ;
118
+
119
+ if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
120
+ return -1 ;
121
+
122
+ return 0 ;
123
+ }
124
+
125
+
126
+ /*
127
+ * New wiringPi node extension methods.
128
+ *********************************************************************************
129
+ */
130
+
131
+ static int myAnalogRead (struct wiringPiNodeStruct *node, const int chan)
132
+ {
133
+ return gertboardAnalogRead (chan - node->pinBase) ;
134
+ }
135
+
136
+ static void myAnalogWrite (struct wiringPiNodeStruct *node, const int chan, const int value)
137
+ {
138
+ gertboardAnalogWrite (chan - node->pinBase, value) ;
139
+ }
140
+
141
+
142
+ /*
143
+ * gertboardAnalogSetup:
144
+ * Create a new wiringPi device node for the analog devices on the
145
+ * Gertboard. We create one node with 2 pins - each pin being read
146
+ * and write - although the operations actually go to different
147
+ * hardware devices.
148
+ *********************************************************************************
149
+ */
150
+
151
+ int gertboardAnalogSetup (const int pinBase)
152
+ {
153
+ struct wiringPiNodeStruct *node ;
154
+ int x ;
155
+
156
+ if (( x = gertboardSPISetup ()) != 0)
157
+ return x;
158
+
159
+ node = wiringPiNewNode (pinBase, 2) ;
160
+ node->analogRead = myAnalogRead ;
161
+ node->analogWrite = myAnalogWrite ;
162
+
163
+ return 0 ;
164
+ }
@@ -0,0 +1,45 @@
1
+ /*
2
+ * gertboard.h:
3
+ * Access routines for the SPI devices on the Gertboard
4
+ * Copyright (c) 2012 Gordon Henderson
5
+ *
6
+ * The Gertboard has an MCP4802 dual-channel D to A convertor
7
+ * connected to the SPI bus, selected via chip-select B.
8
+ *
9
+ ***********************************************************************
10
+ * This file is part of wiringPi:
11
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
12
+ *
13
+ * wiringPi is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU Lesser General Public License as
15
+ * published by the Free Software Foundation, either version 3 of the
16
+ * License, or (at your option) any later version.
17
+ *
18
+ * wiringPi is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU Lesser General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU Lesser General Public
24
+ * License along with wiringPi.
25
+ * If not, see <http://www.gnu.org/licenses/>.
26
+ ***********************************************************************
27
+ */
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ // Old routines
34
+
35
+ extern void gertboardAnalogWrite (const int chan, const int value) ;
36
+ extern int gertboardAnalogRead (const int chan) ;
37
+ extern int gertboardSPISetup (void) ;
38
+
39
+ // New
40
+
41
+ extern int gertboardAnalogSetup (const int pinBase) ;
42
+
43
+ #ifdef __cplusplus
44
+ }
45
+ #endif
@@ -0,0 +1,507 @@
1
+ /*
2
+ * lcd.c:
3
+ * Text-based LCD driver.
4
+ * This is designed to drive the parallel interface LCD drivers
5
+ * based in the Hitachi HD44780U controller and compatables.
6
+ *
7
+ * Copyright (c) 2012 Gordon Henderson.
8
+ ***********************************************************************
9
+ * This file is part of wiringPi:
10
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
11
+ *
12
+ * wiringPi is free software: you can redistribute it and/or modify
13
+ * it under the terms of the GNU Lesser General Public License as published by
14
+ * the Free Software Foundation, either version 3 of the License, or
15
+ * (at your option) any later version.
16
+ *
17
+ * wiringPi is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ * GNU Lesser General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU Lesser General Public License
23
+ * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
24
+ ***********************************************************************
25
+ */
26
+
27
+ #include <stdio.h>
28
+ #include <stdlib.h>
29
+ #include <stdarg.h>
30
+
31
+ #include "wiringPi.h"
32
+
33
+ #include "lcd.h"
34
+
35
+ #ifndef TRUE
36
+ # define TRUE (1==1)
37
+ # define FALSE (1==2)
38
+ #endif
39
+
40
+ // HD44780U Commands
41
+
42
+ #define LCD_CLEAR 0x01
43
+ #define LCD_HOME 0x02
44
+ #define LCD_ENTRY 0x04
45
+ #define LCD_CTRL 0x08
46
+ #define LCD_CDSHIFT 0x10
47
+ #define LCD_FUNC 0x20
48
+ #define LCD_CGRAM 0x40
49
+ #define LCD_DGRAM 0x80
50
+
51
+ // Bits in the entry register
52
+
53
+ #define LCD_ENTRY_SH 0x01
54
+ #define LCD_ENTRY_ID 0x02
55
+
56
+ // Bits in the control register
57
+
58
+ #define LCD_BLINK_CTRL 0x01
59
+ #define LCD_CURSOR_CTRL 0x02
60
+ #define LCD_DISPLAY_CTRL 0x04
61
+
62
+ // Bits in the function register
63
+
64
+ #define LCD_FUNC_F 0x04
65
+ #define LCD_FUNC_N 0x08
66
+ #define LCD_FUNC_DL 0x10
67
+
68
+ #define LCD_CDSHIFT_RL 0x04
69
+
70
+ struct lcdDataStruct
71
+ {
72
+ int bits, rows, cols ;
73
+ int rsPin, strbPin ;
74
+ int dataPins [8] ;
75
+ int cx, cy ;
76
+ } ;
77
+
78
+ struct lcdDataStruct *lcds [MAX_LCDS] ;
79
+
80
+ static int lcdControl ;
81
+
82
+ // Row offsets
83
+
84
+ static const int rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
85
+
86
+
87
+ /*
88
+ * strobe:
89
+ * Toggle the strobe (Really the "E") pin to the device.
90
+ * According to the docs, data is latched on the falling edge.
91
+ *********************************************************************************
92
+ */
93
+
94
+ static void strobe (const struct lcdDataStruct *lcd)
95
+ {
96
+
97
+ // Note timing changes for new version of delayMicroseconds ()
98
+
99
+ digitalWrite (lcd->strbPin, 1) ;
100
+ delayMicroseconds (50) ;
101
+ digitalWrite (lcd->strbPin, 0) ;
102
+ delayMicroseconds (50) ;
103
+ }
104
+
105
+
106
+ /*
107
+ * sentDataCmd:
108
+ * Send an data or command byte to the display.
109
+ *********************************************************************************
110
+ */
111
+
112
+ static void sendDataCmd (const struct lcdDataStruct *lcd, unsigned char data)
113
+ {
114
+ register unsigned char myData = data ;
115
+ unsigned char i, d4 ;
116
+
117
+ if (lcd->bits == 4)
118
+ {
119
+ d4 = (myData >> 4) & 0x0F;
120
+ for (i = 0 ; i < 4 ; ++i)
121
+ {
122
+ digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
123
+ d4 >>= 1 ;
124
+ }
125
+ strobe (lcd) ;
126
+
127
+ d4 = myData & 0x0F ;
128
+ for (i = 0 ; i < 4 ; ++i)
129
+ {
130
+ digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
131
+ d4 >>= 1 ;
132
+ }
133
+ }
134
+ else
135
+ {
136
+ for (i = 0 ; i < 8 ; ++i)
137
+ {
138
+ digitalWrite (lcd->dataPins [i], (myData & 1)) ;
139
+ myData >>= 1 ;
140
+ }
141
+ }
142
+ strobe (lcd) ;
143
+ }
144
+
145
+
146
+ /*
147
+ * putCommand:
148
+ * Send a command byte to the display
149
+ *********************************************************************************
150
+ */
151
+
152
+ static void putCommand (const struct lcdDataStruct *lcd, unsigned char command)
153
+ {
154
+ digitalWrite (lcd->rsPin, 0) ;
155
+ sendDataCmd (lcd, command) ;
156
+ delay (2) ;
157
+ }
158
+
159
+ static void put4Command (const struct lcdDataStruct *lcd, unsigned char command)
160
+ {
161
+ register unsigned char myCommand = command ;
162
+ register unsigned char i ;
163
+
164
+ digitalWrite (lcd->rsPin, 0) ;
165
+
166
+ for (i = 0 ; i < 4 ; ++i)
167
+ {
168
+ digitalWrite (lcd->dataPins [i], (myCommand & 1)) ;
169
+ myCommand >>= 1 ;
170
+ }
171
+ strobe (lcd) ;
172
+ }
173
+
174
+
175
+ /*
176
+ *********************************************************************************
177
+ * User Callable code below here
178
+ *********************************************************************************
179
+ */
180
+
181
+ /*
182
+ * lcdHome: lcdClear:
183
+ * Home the cursor or clear the screen.
184
+ *********************************************************************************
185
+ */
186
+
187
+ void lcdHome (const int fd)
188
+ {
189
+ struct lcdDataStruct *lcd = lcds [fd] ;
190
+
191
+ putCommand (lcd, LCD_HOME) ;
192
+ lcd->cx = lcd->cy = 0 ;
193
+ delay (5) ;
194
+ }
195
+
196
+ void lcdClear (const int fd)
197
+ {
198
+ struct lcdDataStruct *lcd = lcds [fd] ;
199
+
200
+ putCommand (lcd, LCD_CLEAR) ;
201
+ putCommand (lcd, LCD_HOME) ;
202
+ lcd->cx = lcd->cy = 0 ;
203
+ delay (5) ;
204
+ }
205
+
206
+
207
+ /*
208
+ * lcdDisplay: lcdCursor: lcdCursorBlink:
209
+ * Turn the display, cursor, cursor blinking on/off
210
+ *********************************************************************************
211
+ */
212
+
213
+ void lcdDisplay (const int fd, int state)
214
+ {
215
+ struct lcdDataStruct *lcd = lcds [fd] ;
216
+
217
+ if (state)
218
+ lcdControl |= LCD_DISPLAY_CTRL ;
219
+ else
220
+ lcdControl &= ~LCD_DISPLAY_CTRL ;
221
+
222
+ putCommand (lcd, LCD_CTRL | lcdControl) ;
223
+ }
224
+
225
+ void lcdCursor (const int fd, int state)
226
+ {
227
+ struct lcdDataStruct *lcd = lcds [fd] ;
228
+
229
+ if (state)
230
+ lcdControl |= LCD_CURSOR_CTRL ;
231
+ else
232
+ lcdControl &= ~LCD_CURSOR_CTRL ;
233
+
234
+ putCommand (lcd, LCD_CTRL | lcdControl) ;
235
+ }
236
+
237
+ void lcdCursorBlink (const int fd, int state)
238
+ {
239
+ struct lcdDataStruct *lcd = lcds [fd] ;
240
+
241
+ if (state)
242
+ lcdControl |= LCD_BLINK_CTRL ;
243
+ else
244
+ lcdControl &= ~LCD_BLINK_CTRL ;
245
+
246
+ putCommand (lcd, LCD_CTRL | lcdControl) ;
247
+ }
248
+
249
+
250
+ /*
251
+ * lcdSendCommand:
252
+ * Send any arbitary command to the display
253
+ *********************************************************************************
254
+ */
255
+
256
+ void lcdSendCommand (const int fd, unsigned char command)
257
+ {
258
+ struct lcdDataStruct *lcd = lcds [fd] ;
259
+ putCommand (lcd, command) ;
260
+ }
261
+
262
+
263
+ /*
264
+ * lcdPosition:
265
+ * Update the position of the cursor on the display.
266
+ * Ignore invalid locations.
267
+ *********************************************************************************
268
+ */
269
+
270
+ void lcdPosition (const int fd, int x, int y)
271
+ {
272
+ struct lcdDataStruct *lcd = lcds [fd] ;
273
+
274
+ if ((x > lcd->cols) || (x < 0))
275
+ return ;
276
+ if ((y > lcd->rows) || (y < 0))
277
+ return ;
278
+
279
+ putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
280
+
281
+ lcd->cx = x ;
282
+ lcd->cy = y ;
283
+ }
284
+
285
+
286
+ /*
287
+ * lcdCharDef:
288
+ * Defines a new character in the CGRAM
289
+ *********************************************************************************
290
+ */
291
+
292
+ void lcdCharDef (const int fd, int index, unsigned char data [8])
293
+ {
294
+ struct lcdDataStruct *lcd = lcds [fd] ;
295
+ int i ;
296
+
297
+ putCommand (lcd, LCD_CGRAM | ((index & 7) << 3)) ;
298
+
299
+ digitalWrite (lcd->rsPin, 1) ;
300
+ for (i = 0 ; i < 8 ; ++i)
301
+ sendDataCmd (lcd, data [i]) ;
302
+ }
303
+
304
+
305
+ /*
306
+ * lcdPutchar:
307
+ * Send a data byte to be displayed on the display. We implement a very
308
+ * simple terminal here - with line wrapping, but no scrolling. Yet.
309
+ *********************************************************************************
310
+ */
311
+
312
+ void lcdPutchar (const int fd, unsigned char data)
313
+ {
314
+ struct lcdDataStruct *lcd = lcds [fd] ;
315
+
316
+ digitalWrite (lcd->rsPin, 1) ;
317
+ sendDataCmd (lcd, data) ;
318
+
319
+ if (++lcd->cx == lcd->cols)
320
+ {
321
+ lcd->cx = 0 ;
322
+ if (++lcd->cy == lcd->rows)
323
+ lcd->cy = 0 ;
324
+
325
+ putCommand (lcd, lcd->cx + (LCD_DGRAM | rowOff [lcd->cy])) ;
326
+ }
327
+ }
328
+
329
+
330
+ /*
331
+ * lcdPuts:
332
+ * Send a string to be displayed on the display
333
+ *********************************************************************************
334
+ */
335
+
336
+ void lcdPuts (const int fd, const char *string)
337
+ {
338
+ while (*string)
339
+ lcdPutchar (fd, *string++) ;
340
+ }
341
+
342
+
343
+ /*
344
+ * lcdPrintf:
345
+ * Printf to an LCD display
346
+ *********************************************************************************
347
+ */
348
+
349
+ void lcdPrintf (const int fd, const char *message, ...)
350
+ {
351
+ va_list argp ;
352
+ char buffer [1024] ;
353
+
354
+ va_start (argp, message) ;
355
+ vsnprintf (buffer, 1023, message, argp) ;
356
+ va_end (argp) ;
357
+
358
+ lcdPuts (fd, buffer) ;
359
+ }
360
+
361
+
362
+ /*
363
+ * lcdInit:
364
+ * Take a lot of parameters and initialise the LCD, and return a handle to
365
+ * that LCD, or -1 if any error.
366
+ *********************************************************************************
367
+ */
368
+
369
+ int lcdInit (const int rows, const int cols, const int bits,
370
+ const int rs, const int strb,
371
+ const int d0, const int d1, const int d2, const int d3, const int d4,
372
+ const int d5, const int d6, const int d7)
373
+ {
374
+ static int initialised = 0 ;
375
+
376
+ unsigned char func ;
377
+ int i ;
378
+ int lcdFd = -1 ;
379
+ struct lcdDataStruct *lcd ;
380
+
381
+ if (initialised == 0)
382
+ {
383
+ initialised = 1 ;
384
+ for (i = 0 ; i < MAX_LCDS ; ++i)
385
+ lcds [i] = NULL ;
386
+ }
387
+
388
+ // Simple sanity checks
389
+
390
+ if (! ((bits == 4) || (bits == 8)))
391
+ return -1 ;
392
+
393
+ if ((rows < 0) || (rows > 20))
394
+ return -1 ;
395
+
396
+ if ((cols < 0) || (cols > 20))
397
+ return -1 ;
398
+
399
+ // Create a new LCD:
400
+
401
+ for (i = 0 ; i < MAX_LCDS ; ++i)
402
+ {
403
+ if (lcds [i] == NULL)
404
+ {
405
+ lcdFd = i ;
406
+ break ;
407
+ }
408
+ }
409
+
410
+ if (lcdFd == -1)
411
+ return -1 ;
412
+
413
+ lcd = (struct lcdDataStruct *)malloc (sizeof (struct lcdDataStruct)) ;
414
+ if (lcd == NULL)
415
+ return -1 ;
416
+
417
+ lcd->rsPin = rs ;
418
+ lcd->strbPin = strb ;
419
+ lcd->bits = 8 ; // For now - we'll set it properly later.
420
+ lcd->rows = rows ;
421
+ lcd->cols = cols ;
422
+ lcd->cx = 0 ;
423
+ lcd->cy = 0 ;
424
+
425
+ lcd->dataPins [0] = d0 ;
426
+ lcd->dataPins [1] = d1 ;
427
+ lcd->dataPins [2] = d2 ;
428
+ lcd->dataPins [3] = d3 ;
429
+ lcd->dataPins [4] = d4 ;
430
+ lcd->dataPins [5] = d5 ;
431
+ lcd->dataPins [6] = d6 ;
432
+ lcd->dataPins [7] = d7 ;
433
+
434
+ lcds [lcdFd] = lcd ;
435
+
436
+ digitalWrite (lcd->rsPin, 0) ;
437
+ pinMode (lcd->rsPin, OUTPUT) ;
438
+ digitalWrite (lcd->strbPin, 0) ;
439
+ pinMode (lcd->strbPin, OUTPUT) ;
440
+
441
+ for (i = 0 ; i < bits ; ++i)
442
+ {
443
+ digitalWrite (lcd->dataPins [i], 0) ;
444
+ pinMode (lcd->dataPins [i], OUTPUT) ;
445
+ }
446
+ delay (35) ; // mS
447
+
448
+
449
+ // 4-bit mode?
450
+ // OK. This is a PIG and it's not at all obvious from the documentation I had,
451
+ // so I guess some others have worked through either with better documentation
452
+ // or more trial and error... Anyway here goes:
453
+ //
454
+ // It seems that the controller needs to see the FUNC command at least 3 times
455
+ // consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
456
+ // that you can get away with one func-set, however I'd not rely on it...
457
+ //
458
+ // So to set 4-bit mode, you need to send the commands one nibble at a time,
459
+ // the same three times, but send the command to set it into 8-bit mode those
460
+ // three times, then send a final 4th command to set it into 4-bit mode, and only
461
+ // then can you flip the switch for the rest of the library to work in 4-bit
462
+ // mode which sends the commands as 2 x 4-bit values.
463
+
464
+ if (bits == 4)
465
+ {
466
+ func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times
467
+ put4Command (lcd, func >> 4) ;
468
+ delay (35) ;
469
+ put4Command (lcd, func >> 4) ;
470
+ delay (35) ;
471
+ put4Command (lcd, func >> 4) ;
472
+ delay (35) ;
473
+ func = LCD_FUNC ; // 4th set: 4-bit mode
474
+ put4Command (lcd, func >> 4) ;
475
+ delay (35) ;
476
+ lcd->bits = 4 ;
477
+ }
478
+ else
479
+ {
480
+ func = LCD_FUNC | LCD_FUNC_DL ;
481
+ putCommand (lcd, func ) ;
482
+ delay (35) ;
483
+ putCommand (lcd, func ) ;
484
+ delay (35) ;
485
+ putCommand (lcd, func ) ;
486
+ delay (35) ;
487
+ }
488
+
489
+ if (lcd->rows > 1)
490
+ {
491
+ func |= LCD_FUNC_N ;
492
+ putCommand (lcd, func) ;
493
+ delay (35) ;
494
+ }
495
+
496
+ // Rest of the initialisation sequence
497
+
498
+ lcdDisplay (lcdFd, TRUE) ;
499
+ lcdCursor (lcdFd, FALSE) ;
500
+ lcdCursorBlink (lcdFd, FALSE) ;
501
+ lcdClear (lcdFd) ;
502
+
503
+ putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ;
504
+ putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ;
505
+
506
+ return lcdFd ;
507
+ }