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 "mash.h" |
|
27 #include "lcd-pcf8574.h" |
|
28 |
|
29 |
|
30 #ifdef HAVE_WIRINGPI_H |
|
31 |
|
32 int lcdHandle; |
|
33 unsigned char lcdbuf[MAX_LCDS][20][4]; |
|
34 |
|
35 struct lcdDataStruct |
|
36 { |
|
37 int bits, rows, cols ; |
|
38 int rsPin, strbPin ; |
|
39 int dataPins [8] ; |
|
40 int cx, cy ; |
|
41 }; |
|
42 extern struct lcdDataStruct *lcds [MAX_LCDS]; |
|
43 |
|
44 |
|
45 /* |
|
46 * Some LCD functions are extended shadow copies of the wiringPi functions. |
|
47 * The difference is that the lcdbuf will be updated with the contents on |
|
48 * the hardware display. This copy can then be used for a remote display |
|
49 */ |
|
50 |
|
51 |
|
52 /* |
|
53 * setBacklight: |
|
54 ********************************************************************************* |
|
55 */ |
|
56 |
|
57 void setBacklight (int value) |
|
58 { |
|
59 pinMode (AF_BACKLIGHT, OUTPUT) ; |
|
60 digitalWrite (AF_BACKLIGHT, (value & 1)) ; |
|
61 } |
|
62 |
|
63 |
|
64 |
|
65 /* |
|
66 * initLCD: |
|
67 ********************************************************************************* |
|
68 */ |
|
69 |
|
70 int initLCD (int cols, int rows) |
|
71 { |
|
72 int x, y; |
|
73 |
|
74 if (!((rows == 1) || (rows == 2) || (rows == 4))) { |
|
75 fprintf (stderr, "rows must be 1, 2 or 4\n") ; |
|
76 return EXIT_FAILURE ; |
|
77 } |
|
78 |
|
79 if (!((cols == 16) || (cols == 20))) { |
|
80 fprintf (stderr, "cols must be 16 or 20\n") ; |
|
81 return EXIT_FAILURE ; |
|
82 } |
|
83 |
|
84 pcf8574Setup(AF_BASE, 0x27) ; |
|
85 pinMode (AF_RW, OUTPUT) ; |
|
86 digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode |
|
87 |
|
88 /* |
|
89 * The other control pins are initialised with lcdInit () |
|
90 */ |
|
91 lcdHandle = lcdInit (rows, cols, 4, AF_RS, AF_E, AF_DB4, AF_DB5, AF_DB6, AF_DB7, 0, 0, 0, 0) ; |
|
92 if (lcdHandle < 0) { |
|
93 fprintf (stderr, "lcdInit failed\n") ; |
|
94 return -1 ; |
|
95 } |
|
96 |
|
97 lcdClear (lcdHandle) ; |
|
98 for (x = 0; x < 20; x++) |
|
99 for (y = 0; y < 4; y++) |
|
100 lcdbuf[lcdHandle][x][y] = ' '; |
|
101 |
|
102 setBacklight (1) ; |
|
103 |
|
104 return 0 ; |
|
105 } |
|
106 |
|
107 |
|
108 |
|
109 void mb_lcdPutchar(const int fd, unsigned char data) |
|
110 { |
|
111 struct lcdDataStruct *lcd = lcds[fd]; |
|
112 |
|
113 /* |
|
114 * Write to our buffer first, then to the wiringPi driver. |
|
115 * Writing to wiringPi updates the cursor position. |
|
116 */ |
|
117 lcdbuf[fd][lcd->cx][lcd->cy] = data; |
|
118 lcdPutchar(fd, data); |
|
119 } |
|
120 |
|
121 |
|
122 |
|
123 void mb_lcdPuts(const int fd, const char *string) |
|
124 { |
|
125 while (*string) |
|
126 mb_lcdPutchar (fd, *string++); |
|
127 } |
|
128 |
|
129 |
|
130 |
|
131 void mb_lcdClear(const int fd) |
|
132 { |
|
133 int x, y; |
|
134 |
|
135 lcdClear(fd); |
|
136 for (x = 0; x < 20; x++) |
|
137 for (y = 0; y < 4; y++) |
|
138 lcdbuf[fd][x][y] = ' '; |
|
139 } |
|
140 |
|
141 |
|
142 |
|
143 #endif |
|
144 |
|