You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

287 regels
6.0 KiB

  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://github.com/WiringPi/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. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <stdint.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <time.h>
  44. #include <wiringPi.h>
  45. #include <lcd.h>
  46. #ifndef TRUE
  47. # define TRUE (1==1)
  48. # define FALSE (1==2)
  49. #endif
  50. static unsigned char newChar [8] =
  51. {
  52. 0b11111,
  53. 0b10001,
  54. 0b10001,
  55. 0b10101,
  56. 0b11111,
  57. 0b10001,
  58. 0b10001,
  59. 0b11111,
  60. } ;
  61. // Global lcd handle:
  62. static int lcdHandle ;
  63. /*
  64. * usage:
  65. *********************************************************************************
  66. */
  67. int usage (const char *progName)
  68. {
  69. fprintf (stderr, "Usage: %s bits cols rows\n", progName) ;
  70. return EXIT_FAILURE ;
  71. }
  72. /*
  73. * scrollMessage:
  74. *********************************************************************************
  75. */
  76. static const char *message =
  77. " "
  78. "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
  79. " " ;
  80. void scrollMessage (int line, int width)
  81. {
  82. char buf [32] ;
  83. static int position = 0 ;
  84. static int timer = 0 ;
  85. if (millis () < timer)
  86. return ;
  87. timer = millis () + 200 ;
  88. strncpy (buf, &message [position], width) ;
  89. buf [width] = 0 ;
  90. lcdPosition (lcdHandle, 0, line) ;
  91. lcdPuts (lcdHandle, buf) ;
  92. if (++position == (strlen (message) - width))
  93. position = 0 ;
  94. }
  95. /*
  96. * pingPong:
  97. * Bounce a character - only on 4-line displays
  98. *********************************************************************************
  99. */
  100. static void pingPong (int lcd, int cols)
  101. {
  102. static int position = 0 ;
  103. static int dir = 0 ;
  104. if (dir == 0) // Setup
  105. {
  106. dir = 1 ;
  107. lcdPosition (lcdHandle, 0, 3) ;
  108. lcdPutchar (lcdHandle, '*') ;
  109. return ;
  110. }
  111. lcdPosition (lcdHandle, position, 3) ;
  112. lcdPutchar (lcdHandle, ' ') ;
  113. position += dir ;
  114. if (position == cols)
  115. {
  116. dir = -1 ;
  117. --position ;
  118. }
  119. if (position < 0)
  120. {
  121. dir = 1 ;
  122. ++position ;
  123. }
  124. lcdPosition (lcdHandle, position, 3) ;
  125. lcdPutchar (lcdHandle, '#') ;
  126. }
  127. /*
  128. * waitForEnter:
  129. *********************************************************************************
  130. */
  131. static void waitForEnter (void)
  132. {
  133. printf ("Press ENTER to continue: ") ;
  134. (void)fgetc (stdin) ;
  135. }
  136. /*
  137. * The works
  138. *********************************************************************************
  139. */
  140. int main (int argc, char *argv[])
  141. {
  142. int i ;
  143. int lcd ;
  144. int bits, rows, cols ;
  145. struct tm *t ;
  146. time_t tim ;
  147. char buf [32] ;
  148. if (argc != 4)
  149. return usage (argv [0]) ;
  150. printf ("Raspberry Pi LCD test\n") ;
  151. printf ("=====================\n") ;
  152. bits = atoi (argv [1]) ;
  153. cols = atoi (argv [2]) ;
  154. rows = atoi (argv [3]) ;
  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. if (!((cols == 16) || (cols == 20)))
  161. {
  162. fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
  163. return EXIT_FAILURE ;
  164. }
  165. wiringPiSetup () ;
  166. if (bits == 4)
  167. lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ;
  168. else
  169. lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ;
  170. if (lcdHandle < 0)
  171. {
  172. fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
  173. return -1 ;
  174. }
  175. lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Hello World") ;
  176. lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi ") ;
  177. waitForEnter () ;
  178. if (rows > 1)
  179. {
  180. lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi ") ;
  181. if (rows == 4)
  182. {
  183. lcdPosition (lcdHandle, 0, 2) ;
  184. for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
  185. lcdPuts (lcdHandle, "=-") ;
  186. lcdPuts (lcdHandle, "=3") ;
  187. lcdPosition (lcdHandle, 0, 3) ;
  188. for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
  189. lcdPuts (lcdHandle, "-=") ;
  190. lcdPuts (lcdHandle, "-4") ;
  191. }
  192. }
  193. waitForEnter () ;
  194. lcdCharDef (lcdHandle, 2, newChar) ;
  195. lcdClear (lcdHandle) ;
  196. lcdPosition (lcdHandle, 0, 0) ;
  197. lcdPuts (lcdHandle, "User Char: ") ;
  198. lcdPutchar (lcdHandle, 2) ;
  199. lcdCursor (lcdHandle, TRUE) ;
  200. lcdCursorBlink (lcdHandle, TRUE) ;
  201. waitForEnter () ;
  202. lcdCursor (lcdHandle, FALSE) ;
  203. lcdCursorBlink (lcdHandle, FALSE) ;
  204. lcdClear (lcdHandle) ;
  205. for (;;)
  206. {
  207. scrollMessage (0, cols) ;
  208. if (rows == 1)
  209. continue ;
  210. tim = time (NULL) ;
  211. t = localtime (&tim) ;
  212. sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
  213. lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
  214. lcdPuts (lcdHandle, buf) ;
  215. if (rows == 2)
  216. continue ;
  217. sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;
  218. lcdPosition (lcdHandle, (cols - 10) / 2, 2) ;
  219. lcdPuts (lcdHandle, buf) ;
  220. pingPong (lcd, cols) ;
  221. }
  222. return 0 ;
  223. }