1 /* |
|
2 * lcd-pcf8574.c: |
|
3 * Text-based LCD driver library code |
|
4 * This is designed to drive the HD44780U LCD display connected via |
|
5 * a "LCM1602 IIC A0 A1 A2" board with a PCF8574 I2C controller. |
|
6 * |
|
7 * Copyright (c) 2012-2013 Gordon Henderson. |
|
8 * Copyright (c) 2014 Michiel Broek. |
|
9 *********************************************************************** |
|
10 * |
|
11 * mbsePi is free software: you can redistribute it and/or modify |
|
12 * it under the terms of the GNU Lesser General Public License as published by |
|
13 * the Free Software Foundation, either version 3 of the License, or |
|
14 * (at your option) any later version. |
|
15 * |
|
16 * mbsePi is distributed in the hope that it will be useful, |
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 * GNU Lesser General Public License for more details. |
|
20 * |
|
21 * You should have received a copy of the GNU Lesser General Public License |
|
22 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. |
|
23 *********************************************************************** |
|
24 */ |
|
25 |
|
26 #include "thermometers.h" |
|
27 |
|
28 #ifdef HAVE_WIRINGPI_H |
|
29 |
|
30 int lcdHandle; |
|
31 unsigned char lcdbuf[MAX_LCDS][20][4]; |
|
32 |
|
33 struct lcdDataStruct |
|
34 { |
|
35 int bits, rows, cols ; |
|
36 int rsPin, strbPin ; |
|
37 int dataPins [8] ; |
|
38 int cx, cy ; |
|
39 }; |
|
40 extern struct lcdDataStruct *lcds [MAX_LCDS]; |
|
41 |
|
42 |
|
43 /* |
|
44 * Some LCD functions are extended shadow copies of the wiringPi functions. |
|
45 * The difference is that the lcdbuf will be updated with the contents on |
|
46 * the hardware display. This copy can then be used for a remote display |
|
47 */ |
|
48 |
|
49 |
|
50 /* |
|
51 * setBacklight: |
|
52 ********************************************************************************* |
|
53 */ |
|
54 |
|
55 void setBacklight (int value) |
|
56 { |
|
57 pinMode (AF_BACKLIGHT, OUTPUT) ; |
|
58 digitalWrite (AF_BACKLIGHT, (value & 1)) ; |
|
59 } |
|
60 |
|
61 |
|
62 |
|
63 /* |
|
64 * initLCD: |
|
65 ********************************************************************************* |
|
66 */ |
|
67 |
|
68 int initLCD (int cols, int rows) |
|
69 { |
|
70 int x, y; |
|
71 |
|
72 if (!((rows == 1) || (rows == 2) || (rows == 4))) { |
|
73 fprintf (stderr, "rows must be 1, 2 or 4\n") ; |
|
74 return EXIT_FAILURE ; |
|
75 } |
|
76 |
|
77 if (!((cols == 16) || (cols == 20))) { |
|
78 fprintf (stderr, "cols must be 16 or 20\n") ; |
|
79 return EXIT_FAILURE ; |
|
80 } |
|
81 |
|
82 pcf8574Setup(AF_BASE, 0x27) ; |
|
83 pinMode (AF_RW, OUTPUT) ; |
|
84 digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode |
|
85 |
|
86 /* |
|
87 * The other control pins are initialised with lcdInit () |
|
88 */ |
|
89 lcdHandle = lcdInit (rows, cols, 4, AF_RS, AF_E, AF_DB4, AF_DB5, AF_DB6, AF_DB7, 0, 0, 0, 0) ; |
|
90 if (lcdHandle < 0) { |
|
91 fprintf (stderr, "lcdInit failed\n") ; |
|
92 return -1 ; |
|
93 } |
|
94 |
|
95 lcdClear (lcdHandle) ; |
|
96 for (x = 0; x < 20; x++) |
|
97 for (y = 0; y < 4; y++) |
|
98 lcdbuf[lcdHandle][x][y] = ' '; |
|
99 |
|
100 setBacklight (1) ; |
|
101 |
|
102 return 0 ; |
|
103 } |
|
104 |
|
105 |
|
106 |
|
107 void mb_lcdPutchar(const int fd, unsigned char data) |
|
108 { |
|
109 struct lcdDataStruct *lcd = lcds[fd]; |
|
110 |
|
111 /* |
|
112 * Write to our buffer first, then to the wiringPi driver. |
|
113 * Writing to wiringPi updates the cursor position. |
|
114 */ |
|
115 lcdbuf[fd][lcd->cx][lcd->cy] = data; |
|
116 lcdPutchar(fd, data); |
|
117 } |
|
118 |
|
119 |
|
120 |
|
121 void mb_lcdPuts(const int fd, const char *string) |
|
122 { |
|
123 while (*string) |
|
124 mb_lcdPutchar (fd, *string++); |
|
125 } |
|
126 |
|
127 |
|
128 |
|
129 void mb_lcdClear(const int fd) |
|
130 { |
|
131 int x, y; |
|
132 |
|
133 lcdClear(fd); |
|
134 for (x = 0; x < 20; x++) |
|
135 for (y = 0; y < 4; y++) |
|
136 lcdbuf[fd][x][y] = ' '; |
|
137 } |
|
138 |
|
139 |
|
140 |
|
141 #endif |
|
142 |
|