lib/lcd-pcf8574.c

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

mercurial