chiark / gitweb /
c01358557e40ba99d3c2377c60959aeef9f9c817
[wiringPi.git] / examples / lcd.c
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  *      This test program assumes the following:
8  *
9  *      8-bit displays:
10  *              GPIO 0-7 is connected to display data pins 0-7.
11  *              GPIO 11 is the RS pin.
12  *              GPIO 10 is the Strobe/E pin.
13  *
14  *      For 4-bit interface:
15  *              GPIO 4-7 is connected to display data pins 4-7.
16  *              GPIO 11 is the RS pin.
17  *              GPIO 10 is the Strobe/E pin.
18  *
19  * Copyright (c) 2012-2013 Gordon Henderson.
20  ***********************************************************************
21  * This file is part of wiringPi:
22  *      https://projects.drogon.net/raspberry-pi/wiringpi/
23  *
24  *    wiringPi is free software: you can redistribute it and/or modify
25  *    it under the terms of the GNU Lesser General Public License as published by
26  *    the Free Software Foundation, either version 3 of the License, or
27  *    (at your option) any later version.
28  *
29  *    wiringPi is distributed in the hope that it will be useful,
30  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
31  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *    GNU Lesser General Public License for more details.
33  *
34  *    You should have received a copy of the GNU Lesser General Public License
35  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
36  ***********************************************************************
37  */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdint.h>
42
43 #include <unistd.h>
44 #include <string.h>
45 #include <time.h>
46
47 #include <wiringPi.h>
48 #include <lcd.h>
49
50 #ifndef TRUE
51 #  define       TRUE    (1==1)
52 #  define       FALSE   (1==2)
53 #endif
54
55 static unsigned char newChar [8] = 
56 {
57   0b11111,
58   0b10001,
59   0b10001,
60   0b10101,
61   0b11111,
62   0b10001,
63   0b10001,
64   0b11111,
65 } ;
66
67
68 int usage (const char *progName)
69 {
70   fprintf (stderr, "Usage: %s bits cols rows\n", progName) ;
71   return EXIT_FAILURE ;
72 }
73
74 static const char *message =
75   "                    "
76   "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
77   "                    " ;
78
79 void scrollMessage (int lcd, int line, int width)
80 {
81   char buf [32] ;
82   static int position = 0 ;
83
84   strncpy (buf, &message [position], width) ;
85   buf [width] = 0 ;
86   lcdPosition (lcd, 0, line) ;
87   lcdPuts (lcd, buf) ;
88
89   if (++position == (strlen (message) - width))
90     position = 0 ;
91 }
92
93 static void pingPong (int lcd, int cols)
94 {
95   static int position = 0 ;
96   static int dir      = 0 ;
97
98   if (dir == 0)         // Setup
99   {
100     dir = 1 ;
101     lcdPosition (lcd, 0, 3) ;
102     lcdPutchar (lcd, '*') ;
103     return ;
104   }
105
106   lcdPosition (lcd, position, 3) ;
107   lcdPutchar (lcd, ' ') ;
108   position += dir ;
109
110   if (position == cols)
111   {
112     dir = -1 ;
113     --position ;
114   }
115   
116   if (position < 0)
117   {
118     dir = 1 ;
119     ++position ;
120   }
121
122   lcdPosition (lcd, position, 3) ;
123   lcdPutchar (lcd, '#') ;
124 }
125
126
127
128 static void waitForEnter (void)
129 {
130   printf ("Press ENTER to continue: ") ;
131   (void)fgetc (stdin) ;
132 }
133
134 int main (int argc, char *argv[])
135 {
136   int i ;
137   int lcd ;
138   int bits, rows, cols ;
139
140   struct tm *t ;
141   time_t tim ;
142
143   char buf [32] ;
144
145   if (argc != 4)
146     return usage (argv [0]) ;
147
148   printf ("Raspberry Pi LCD test\n") ;
149   printf ("=====================\n") ;
150
151   bits = atoi (argv [1]) ;
152   cols = atoi (argv [2]) ;
153   rows = atoi (argv [3]) ;
154
155   if (!((rows == 1) || (rows == 2) || (rows == 4)))
156   {
157     fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ;
158     return EXIT_FAILURE ;
159   }
160
161   if (!((cols == 16) || (cols == 20)))
162   {
163     fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
164     return EXIT_FAILURE ;
165   }
166
167   wiringPiSetup () ;
168
169   if (bits == 4)
170     lcd = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ;
171   else
172     lcd = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ;
173
174   if (lcd < 0)
175   {
176     fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
177     return -1 ;
178   }
179
180   lcdPosition (lcd, 0, 0) ; lcdPuts (lcd, "Gordon Henderson") ;
181
182   if (rows > 1)
183   {
184     lcdPosition (lcd, 0, 1) ;
185     for (i = 0 ; i < (cols - 1) ; ++i)
186       lcdPutchar (lcd, '*') ;
187     lcdPutchar (lcd, '2') ;
188
189     if (rows == 4)
190     {
191       lcdPosition (lcd, 0, 2) ;
192       for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
193         lcdPuts (lcd, "=-") ;
194       lcdPuts (lcd, "=3") ;
195
196       lcdPosition (lcd, 0, 3) ;
197       for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
198         lcdPuts (lcd, "-=") ;
199       lcdPuts (lcd, "-4") ;
200     }
201   }
202
203   sleep (2) ;
204
205   lcdPosition (lcd, 0, 0) ; lcdPuts (lcd, "  wiringpi.com  ") ;
206
207
208   waitForEnter () ;
209
210   lcdCharDef  (lcd, 2, newChar) ;
211
212   lcdClear    (lcd) ;
213   lcdPosition (lcd, 0, 0) ;
214   lcdPuts     (lcd, "User Char: ") ;
215   lcdPutchar  (lcd, 2) ;
216
217   lcdCursor      (lcd, TRUE) ;
218   lcdCursorBlink (lcd, TRUE) ;
219
220   waitForEnter () ;
221
222   lcdCursor      (lcd, FALSE) ;
223   lcdCursorBlink (lcd, FALSE) ;
224
225
226   for (;;)
227   {
228     delay (250) ;
229
230     scrollMessage (lcd, 0, cols) ;
231     
232     if (rows == 1)
233       continue ;
234
235     tim = time (NULL) ;
236     t = localtime (&tim) ;
237
238     sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
239
240     lcdPosition (lcd, (cols - 8) / 2, 1) ;
241     lcdPuts     (lcd, buf) ;
242
243     if (rows == 2)
244       continue ;
245
246     sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;
247
248     lcdPosition (lcd, (cols - 10) / 2, 2) ;
249     lcdPuts     (lcd, buf) ;
250
251     pingPong (lcd, cols) ;
252   }
253
254   return 0 ;
255 }