Deokgyu Yang | 3f787a1 | 2020-03-30 15:25:23 +0900 | [diff] [blame] | 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 | // Global lcd handle: |
| 69 | |
| 70 | static int lcdHandle ; |
| 71 | |
| 72 | /* |
| 73 | * usage: |
| 74 | ********************************************************************************* |
| 75 | */ |
| 76 | |
| 77 | int usage (const char *progName) |
| 78 | { |
| 79 | fprintf (stderr, "Usage: %s bits cols rows\n", progName) ; |
| 80 | return EXIT_FAILURE ; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * scrollMessage: |
| 86 | ********************************************************************************* |
| 87 | */ |
| 88 | |
| 89 | static const char *message = |
| 90 | " " |
| 91 | "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/" |
| 92 | " " ; |
| 93 | |
| 94 | void scrollMessage (int line, int width) |
| 95 | { |
| 96 | char buf [32] ; |
| 97 | static int position = 0 ; |
| 98 | static int timer = 0 ; |
| 99 | |
| 100 | if (millis () < timer) |
| 101 | return ; |
| 102 | |
| 103 | timer = millis () + 200 ; |
| 104 | |
| 105 | strncpy (buf, &message [position], width) ; |
| 106 | buf [width] = 0 ; |
| 107 | lcdPosition (lcdHandle, 0, line) ; |
| 108 | lcdPuts (lcdHandle, buf) ; |
| 109 | |
| 110 | if (++position == (strlen (message) - width)) |
| 111 | position = 0 ; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * pingPong: |
| 117 | * Bounce a character - only on 4-line displays |
| 118 | ********************************************************************************* |
| 119 | */ |
| 120 | |
| 121 | static void pingPong (int lcd, int cols) |
| 122 | { |
| 123 | static int position = 0 ; |
| 124 | static int dir = 0 ; |
| 125 | |
| 126 | if (dir == 0) // Setup |
| 127 | { |
| 128 | dir = 1 ; |
| 129 | lcdPosition (lcdHandle, 0, 3) ; |
| 130 | lcdPutchar (lcdHandle, '*') ; |
| 131 | return ; |
| 132 | } |
| 133 | |
| 134 | lcdPosition (lcdHandle, position, 3) ; |
| 135 | lcdPutchar (lcdHandle, ' ') ; |
| 136 | position += dir ; |
| 137 | |
| 138 | if (position == cols) |
| 139 | { |
| 140 | dir = -1 ; |
| 141 | --position ; |
| 142 | } |
| 143 | |
| 144 | if (position < 0) |
| 145 | { |
| 146 | dir = 1 ; |
| 147 | ++position ; |
| 148 | } |
| 149 | |
| 150 | lcdPosition (lcdHandle, position, 3) ; |
| 151 | lcdPutchar (lcdHandle, '#') ; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | * waitForEnter: |
| 157 | ********************************************************************************* |
| 158 | */ |
| 159 | |
| 160 | static void waitForEnter (void) |
| 161 | { |
| 162 | printf ("Press ENTER to continue: ") ; |
| 163 | (void)fgetc (stdin) ; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * The works |
| 169 | ********************************************************************************* |
| 170 | */ |
| 171 | |
| 172 | int main (int argc, char *argv[]) |
| 173 | { |
| 174 | int i ; |
| 175 | int lcd ; |
| 176 | int bits, rows, cols ; |
| 177 | |
| 178 | struct tm *t ; |
| 179 | time_t tim ; |
| 180 | |
| 181 | char buf [32] ; |
| 182 | |
| 183 | if (argc != 4) |
| 184 | return usage (argv [0]) ; |
| 185 | |
| 186 | printf ("Raspberry Pi LCD test\n") ; |
| 187 | printf ("=====================\n") ; |
| 188 | |
| 189 | bits = atoi (argv [1]) ; |
| 190 | cols = atoi (argv [2]) ; |
| 191 | rows = atoi (argv [3]) ; |
| 192 | |
| 193 | if (!((rows == 1) || (rows == 2) || (rows == 4))) |
| 194 | { |
| 195 | fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ; |
| 196 | return EXIT_FAILURE ; |
| 197 | } |
| 198 | |
| 199 | if (!((cols == 16) || (cols == 20))) |
| 200 | { |
| 201 | fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ; |
| 202 | return EXIT_FAILURE ; |
| 203 | } |
| 204 | |
| 205 | wiringPiSetup () ; |
| 206 | |
| 207 | if (bits == 4) |
| 208 | lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ; |
| 209 | else |
| 210 | lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ; |
| 211 | |
| 212 | if (lcdHandle < 0) |
| 213 | { |
| 214 | fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ; |
| 215 | return -1 ; |
| 216 | } |
| 217 | |
| 218 | lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ; |
| 219 | lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; |
| 220 | |
| 221 | waitForEnter () ; |
| 222 | |
| 223 | if (rows > 1) |
| 224 | { |
| 225 | lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; |
| 226 | |
| 227 | if (rows == 4) |
| 228 | { |
| 229 | lcdPosition (lcdHandle, 0, 2) ; |
| 230 | for (i = 0 ; i < ((cols - 1) / 2) ; ++i) |
| 231 | lcdPuts (lcdHandle, "=-") ; |
| 232 | lcdPuts (lcdHandle, "=3") ; |
| 233 | |
| 234 | lcdPosition (lcdHandle, 0, 3) ; |
| 235 | for (i = 0 ; i < ((cols - 1) / 2) ; ++i) |
| 236 | lcdPuts (lcdHandle, "-=") ; |
| 237 | lcdPuts (lcdHandle, "-4") ; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | waitForEnter () ; |
| 242 | |
| 243 | lcdCharDef (lcdHandle, 2, newChar) ; |
| 244 | |
| 245 | lcdClear (lcdHandle) ; |
| 246 | lcdPosition (lcdHandle, 0, 0) ; |
| 247 | lcdPuts (lcdHandle, "User Char: ") ; |
| 248 | lcdPutchar (lcdHandle, 2) ; |
| 249 | |
| 250 | lcdCursor (lcdHandle, TRUE) ; |
| 251 | lcdCursorBlink (lcdHandle, TRUE) ; |
| 252 | |
| 253 | waitForEnter () ; |
| 254 | |
| 255 | lcdCursor (lcdHandle, FALSE) ; |
| 256 | lcdCursorBlink (lcdHandle, FALSE) ; |
| 257 | lcdClear (lcdHandle) ; |
| 258 | |
| 259 | for (;;) |
| 260 | { |
| 261 | scrollMessage (0, cols) ; |
| 262 | |
| 263 | if (rows == 1) |
| 264 | continue ; |
| 265 | |
| 266 | tim = time (NULL) ; |
| 267 | t = localtime (&tim) ; |
| 268 | |
| 269 | sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; |
| 270 | |
| 271 | lcdPosition (lcdHandle, (cols - 8) / 2, 1) ; |
| 272 | lcdPuts (lcdHandle, buf) ; |
| 273 | |
| 274 | if (rows == 2) |
| 275 | continue ; |
| 276 | |
| 277 | sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ; |
| 278 | |
| 279 | lcdPosition (lcdHandle, (cols - 10) / 2, 2) ; |
| 280 | lcdPuts (lcdHandle, buf) ; |
| 281 | |
| 282 | pingPong (lcd, cols) ; |
| 283 | } |
| 284 | |
| 285 | return 0 ; |
| 286 | } |