chiark / gitweb /
6024917f897d6e6151c341179c7002800c488c2b
[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  * 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 <stdint.h>
30
31 #include <unistd.h>
32 #include <string.h>
33 #include <time.h>
34
35 #include <wiringPi.h>
36 #include <lcd.h>
37
38 int main (void)
39 {
40   int i, j ;
41   int fd1, fd2 ; 
42
43   char message1 [256] ;
44   char message2 [256] ;
45   char buf1 [30] ;
46   char buf2 [30] ;
47
48   struct tm *t ;
49   time_t tim ;
50
51   printf ("Raspberry Pi LCD test program\n") ;
52
53   if (wiringPiSetup () == -1)
54     exit (1) ;
55
56   fd1 = lcdInit (4, 20, 4, 8,  9, 4,5,6,7,0,0,0,0) ;
57   fd2 = lcdInit (2, 16, 4, 8, 10, 4,5,6,7,0,0,0,0) ;
58
59 //fd1 = lcdInit (4, 20, 8, 8,  9, 0,1,2,3,4,5,6,7) ;
60 //fd2 = lcdInit (2, 16, 8, 8, 10, 0,1,2,3,4,5,6,7) ;
61
62   if (fd1 == -1)
63   {
64     printf ("lcdInit 1 failed\n") ;
65     return 1 ;
66   }
67
68   if (fd2 == -1)
69   {
70     printf ("lcdInit 2 failed\n") ;
71     return 1 ;
72   }
73
74   sleep (1) ;
75
76   lcdPosition (fd1, 0, 0) ; lcdPuts (fd1, " Gordon Henderson") ;
77   lcdPosition (fd1, 0, 1) ; lcdPuts (fd1, "  --------------") ;
78 /*
79   lcdPosition (fd1, 0, 2) ; lcdPuts (fd1, "   00:00:00") ;
80   lcdPosition (fd1, 0, 3) ; lcdPuts (fd1, "   DD:MM:YY") ;
81 */
82
83   lcdPosition (fd2, 0, 0) ; lcdPuts (fd2, "Gordon Henderson") ;
84   lcdPosition (fd2, 0, 1) ; lcdPuts (fd2, "----------------") ;
85
86   sleep (2) ;
87
88   sprintf (message1, "%s", "                  http://projects.drogon.net/                    ") ;
89   sprintf (message2, "%s", "                This is a long message to go into the smaller display just for a demonstration of what we can do.                ") ;
90
91   for (;;)
92   {
93     i = 0 ;
94     j = 0 ;
95     for (;;)
96     {
97       strncpy (buf1, &message1 [i], 20) ;
98       buf1 [20] = 0 ;
99       lcdPosition (fd1, 0, 1) ;
100       lcdPuts (fd1, buf1) ;
101       ++i ;
102       if (i == strlen (message1) - 20)
103         i = 0 ;
104
105       strncpy (buf2, &message2 [j], 16) ;
106       buf2 [16] = 0 ;
107       lcdPosition (fd2, 0, 1) ;
108       lcdPuts (fd2, buf2) ;
109       ++j ;
110       if (j == strlen (message2) - 16)
111         j = 0 ;
112
113       tim = time (NULL) ;
114       t = localtime (&tim) ;
115
116       sprintf (buf1, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
117       lcdPosition (fd1, 5, 2) ;
118       lcdPuts (fd1, buf1) ;
119
120       sprintf (buf1, "%02d/%02d/%02d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;
121       lcdPosition (fd1, 4, 3) ;
122       lcdPuts (fd1, buf1) ;
123
124       delay (250) ;
125     }
126   }
127
128   return 0 ;
129 }