3 * Text-based LCD driver test code
4 * This is designed to drive the Adafruit RGB LCD Plate
5 * with the additional 5 buttons for the Raspberry Pi
7 * Copyright (c) 2012-2013 Gordon Henderson.
8 ***********************************************************************
9 * This file is part of wiringPi:
10 * https://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
43 // Defines for the Adafruit Pi LCD interface board
46 #define AF_RED (AF_BASE + 6)
47 #define AF_GREEN (AF_BASE + 7)
48 #define AF_BLUE (AF_BASE + 8)
50 #define AF_E (AF_BASE + 13)
51 #define AF_RW (AF_BASE + 14)
52 #define AF_RS (AF_BASE + 15)
54 #define AF_DB4 (AF_BASE + 12)
55 #define AF_DB5 (AF_BASE + 11)
56 #define AF_DB6 (AF_BASE + 10)
57 #define AF_DB7 (AF_BASE + 9)
59 #define AF_SELECT (AF_BASE + 0)
60 #define AF_RIGHT (AF_BASE + 1)
61 #define AF_DOWN (AF_BASE + 2)
62 #define AF_UP (AF_BASE + 3)
63 #define AF_LEFT (AF_BASE + 4)
66 // User-Defined character test
68 static unsigned char newChar [8] =
82 static int lcdHandle ;
86 *********************************************************************************
89 int usage (const char *progName)
91 fprintf (stderr, "Usage: %s colour\n", progName) ;
98 *********************************************************************************
101 static const char *message =
103 "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
106 void scrollMessage (int line, int width)
109 static int position = 0 ;
110 static int timer = 0 ;
112 if (millis () < timer)
115 timer = millis () + 200 ;
117 strncpy (buf, &message [position], width) ;
119 lcdPosition (lcdHandle, 0, line) ;
120 lcdPuts (lcdHandle, buf) ;
122 if (++position == (strlen (message) - width))
128 * setBacklightColour:
129 * The colour outputs are inverted.
130 *********************************************************************************
133 static void setBacklightColour (int colour)
137 digitalWrite (AF_RED, !(colour & 1)) ;
138 digitalWrite (AF_GREEN, !(colour & 2)) ;
139 digitalWrite (AF_BLUE, !(colour & 4)) ;
145 * Setup the Adafruit board by making sure the additional pins are
146 * set to the correct modes, etc.
147 *********************************************************************************
150 static void adafruitLCDSetup (int colour)
156 pinMode (AF_RED, OUTPUT) ;
157 pinMode (AF_GREEN, OUTPUT) ;
158 pinMode (AF_BLUE, OUTPUT) ;
159 setBacklightColour (colour) ;
163 for (i = 0 ; i <= 4 ; ++i)
165 pinMode (AF_BASE + i, INPUT) ;
166 pullUpDnControl (AF_BASE + i, PUD_UP) ; // Enable pull-ups, switches close to 0v
171 pinMode (AF_RW, OUTPUT) ; digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode
173 // The other control pins are initialised with lcdInit ()
175 lcdHandle = lcdInit (2, 16, 4, AF_RS, AF_E, AF_DB4,AF_DB5,AF_DB6,AF_DB7, 0,0,0,0) ;
179 fprintf (stderr, "lcdInit failed\n") ;
180 exit (EXIT_FAILURE) ;
187 * On the Adafruit display, wait for the select button
188 *********************************************************************************
191 static void waitForEnter (void)
193 printf ("Press SELECT to continue: ") ; fflush (stdout) ;
195 while (digitalRead (AF_SELECT) == HIGH) // Wait for push
198 while (digitalRead (AF_SELECT) == LOW) // Wait for release
207 * Test the update speed of the display
208 *********************************************************************************
211 static void speedTest (void)
213 unsigned int start, end, taken ;
216 lcdClear (lcdHandle) ;
218 for (times = 0 ; times < 10 ; ++times)
220 lcdPuts (lcdHandle, "0123456789ABCDEF") ;
221 lcdPuts (lcdHandle, "0123456789ABCDEF") ;
224 taken = (end - start) / 10;
226 lcdClear (lcdHandle) ;
227 lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Speed: %dmS", taken) ;
228 lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "For full update") ;
232 lcdClear (lcdHandle) ;
233 lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Time: %dmS", taken / 32) ;
234 lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "Per character") ;
238 lcdClear (lcdHandle) ;
239 lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "%d cps...", 32000 / taken) ;
247 *********************************************************************************
250 int main (int argc, char *argv[])
254 int waitForRelease = FALSE ;
262 return usage (argv [0]) ;
264 printf ("Raspberry Pi Adafruit LCD test\n") ;
265 printf ("==============================\n") ;
267 colour = atoi (argv [1]) ;
269 wiringPiSetupSys () ;
270 mcp23017Setup (AF_BASE, 0x20) ;
272 adafruitLCDSetup (colour) ;
274 lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;
275 lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
279 lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "Adafruit RGB LCD") ;
283 lcdCharDef (lcdHandle, 2, newChar) ;
285 lcdClear (lcdHandle) ;
286 lcdPosition (lcdHandle, 0, 0) ;
287 lcdPuts (lcdHandle, "User Char: ") ;
288 lcdPutchar (lcdHandle, 2) ;
290 lcdCursor (lcdHandle, TRUE) ;
291 lcdCursorBlink (lcdHandle, TRUE) ;
295 lcdCursor (lcdHandle, FALSE) ;
296 lcdCursorBlink (lcdHandle, FALSE) ;
300 lcdClear (lcdHandle) ;
304 scrollMessage (0, cols) ;
307 t = localtime (&tim) ;
309 sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
311 lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
312 lcdPuts (lcdHandle, buf) ;
314 // Check buttons to cycle colour
316 // If Up or Down are still pushed, then skip
320 if ((digitalRead (AF_UP) == LOW) || (digitalRead (AF_DOWN) == LOW))
323 waitForRelease = FALSE ;
326 if (digitalRead (AF_UP) == LOW) // Pushed
328 colour = colour + 1 ;
331 setBacklightColour (colour) ;
332 waitForRelease = TRUE ;
335 if (digitalRead (AF_DOWN) == LOW) // Pushed
337 colour = colour - 1 ;
340 setBacklightColour (colour) ;
341 waitForRelease = TRUE ;