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.
 
 
 
 
 

348 rivejä
7.6 KiB

  1. /*
  2. * lcd-adafruit.c:
  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
  6. *
  7. * Copyright (c) 2012-2013 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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <wiringPi.h>
  32. #include <mcp23017.h>
  33. #include <lcd.h>
  34. #ifndef TRUE
  35. # define TRUE (1==1)
  36. # define FALSE (1==2)
  37. #endif
  38. // Defines for the Adafruit Pi LCD interface board
  39. #define AF_BASE 100
  40. #define AF_RED (AF_BASE + 6)
  41. #define AF_GREEN (AF_BASE + 7)
  42. #define AF_BLUE (AF_BASE + 8)
  43. #define AF_E (AF_BASE + 13)
  44. #define AF_RW (AF_BASE + 14)
  45. #define AF_RS (AF_BASE + 15)
  46. #define AF_DB4 (AF_BASE + 12)
  47. #define AF_DB5 (AF_BASE + 11)
  48. #define AF_DB6 (AF_BASE + 10)
  49. #define AF_DB7 (AF_BASE + 9)
  50. #define AF_SELECT (AF_BASE + 0)
  51. #define AF_RIGHT (AF_BASE + 1)
  52. #define AF_DOWN (AF_BASE + 2)
  53. #define AF_UP (AF_BASE + 3)
  54. #define AF_LEFT (AF_BASE + 4)
  55. // User-Defined character test
  56. static unsigned char newChar [8] =
  57. {
  58. 0b00100,
  59. 0b00100,
  60. 0b00000,
  61. 0b00100,
  62. 0b01110,
  63. 0b11011,
  64. 0b11011,
  65. 0b10001,
  66. } ;
  67. // Global lcd handle:
  68. static int lcdHandle ;
  69. /*
  70. * usage:
  71. *********************************************************************************
  72. */
  73. int usage (const char *progName)
  74. {
  75. fprintf (stderr, "Usage: %s colour\n", progName) ;
  76. return EXIT_FAILURE ;
  77. }
  78. /*
  79. * scrollMessage:
  80. *********************************************************************************
  81. */
  82. static const char *message =
  83. " "
  84. "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
  85. " " ;
  86. void scrollMessage (int line, int width)
  87. {
  88. char buf [32] ;
  89. static int position = 0 ;
  90. static int timer = 0 ;
  91. if (millis () < timer)
  92. return ;
  93. timer = millis () + 200 ;
  94. strncpy (buf, &message [position], width) ;
  95. buf [width] = 0 ;
  96. lcdPosition (lcdHandle, 0, line) ;
  97. lcdPuts (lcdHandle, buf) ;
  98. if (++position == (strlen (message) - width))
  99. position = 0 ;
  100. }
  101. /*
  102. * setBacklightColour:
  103. * The colour outputs are inverted.
  104. *********************************************************************************
  105. */
  106. static void setBacklightColour (int colour)
  107. {
  108. colour &= 7 ;
  109. digitalWrite (AF_RED, !(colour & 1)) ;
  110. digitalWrite (AF_GREEN, !(colour & 2)) ;
  111. digitalWrite (AF_BLUE, !(colour & 4)) ;
  112. }
  113. /*
  114. * adafruitLCDSetup:
  115. * Setup the Adafruit board by making sure the additional pins are
  116. * set to the correct modes, etc.
  117. *********************************************************************************
  118. */
  119. static void adafruitLCDSetup (int colour)
  120. {
  121. int i ;
  122. // Backlight LEDs
  123. pinMode (AF_RED, OUTPUT) ;
  124. pinMode (AF_GREEN, OUTPUT) ;
  125. pinMode (AF_BLUE, OUTPUT) ;
  126. setBacklightColour (colour) ;
  127. // Input buttons
  128. for (i = 0 ; i <= 4 ; ++i)
  129. {
  130. pinMode (AF_BASE + i, INPUT) ;
  131. pullUpDnControl (AF_BASE + i, PUD_UP) ; // Enable pull-ups, switches close to 0v
  132. }
  133. // Control signals
  134. pinMode (AF_RW, OUTPUT) ; digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode
  135. // The other control pins are initialised with lcdInit ()
  136. lcdHandle = lcdInit (2, 16, 4, AF_RS, AF_E, AF_DB4,AF_DB5,AF_DB6,AF_DB7, 0,0,0,0) ;
  137. if (lcdHandle < 0)
  138. {
  139. fprintf (stderr, "lcdInit failed\n") ;
  140. exit (EXIT_FAILURE) ;
  141. }
  142. }
  143. /*
  144. * waitForEnter:
  145. * On the Adafruit display, wait for the select button
  146. *********************************************************************************
  147. */
  148. static void waitForEnter (void)
  149. {
  150. printf ("Press SELECT to continue: ") ; fflush (stdout) ;
  151. while (digitalRead (AF_SELECT) == HIGH) // Wait for push
  152. delay (1) ;
  153. while (digitalRead (AF_SELECT) == LOW) // Wait for release
  154. delay (1) ;
  155. printf ("OK\n") ;
  156. }
  157. /*
  158. * speedTest:
  159. * Test the update speed of the display
  160. *********************************************************************************
  161. */
  162. static void speedTest (void)
  163. {
  164. unsigned int start, end, taken ;
  165. int times ;
  166. lcdClear (lcdHandle) ;
  167. start = millis () ;
  168. for (times = 0 ; times < 10 ; ++times)
  169. {
  170. lcdPuts (lcdHandle, "0123456789ABCDEF") ;
  171. lcdPuts (lcdHandle, "0123456789ABCDEF") ;
  172. }
  173. end = millis () ;
  174. taken = (end - start) / 10;
  175. lcdClear (lcdHandle) ;
  176. lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Speed: %dmS", taken) ;
  177. lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "For full update") ;
  178. waitForEnter () ;
  179. lcdClear (lcdHandle) ;
  180. lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Time: %dmS", taken / 32) ;
  181. lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "Per character") ;
  182. waitForEnter () ;
  183. lcdClear (lcdHandle) ;
  184. lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "%d cps...", 32000 / taken) ;
  185. waitForEnter () ;
  186. }
  187. /*
  188. * The works
  189. *********************************************************************************
  190. */
  191. int main (int argc, char *argv[])
  192. {
  193. int colour ;
  194. int cols = 16 ;
  195. int waitForRelease = FALSE ;
  196. struct tm *t ;
  197. time_t tim ;
  198. char buf [32] ;
  199. if (argc != 2)
  200. return usage (argv [0]) ;
  201. printf ("Raspberry Pi Adafruit LCD test\n") ;
  202. printf ("==============================\n") ;
  203. colour = atoi (argv [1]) ;
  204. wiringPiSetupSys () ;
  205. mcp23017Setup (AF_BASE, 0x20) ;
  206. adafruitLCDSetup (colour) ;
  207. lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;
  208. lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
  209. waitForEnter () ;
  210. lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "Adafruit RGB LCD") ;
  211. waitForEnter () ;
  212. lcdCharDef (lcdHandle, 2, newChar) ;
  213. lcdClear (lcdHandle) ;
  214. lcdPosition (lcdHandle, 0, 0) ;
  215. lcdPuts (lcdHandle, "User Char: ") ;
  216. lcdPutchar (lcdHandle, 2) ;
  217. lcdCursor (lcdHandle, TRUE) ;
  218. lcdCursorBlink (lcdHandle, TRUE) ;
  219. waitForEnter () ;
  220. lcdCursor (lcdHandle, FALSE) ;
  221. lcdCursorBlink (lcdHandle, FALSE) ;
  222. speedTest () ;
  223. lcdClear (lcdHandle) ;
  224. for (;;)
  225. {
  226. scrollMessage (0, cols) ;
  227. tim = time (NULL) ;
  228. t = localtime (&tim) ;
  229. sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
  230. lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
  231. lcdPuts (lcdHandle, buf) ;
  232. // Check buttons to cycle colour
  233. // If Up or Down are still pushed, then skip
  234. if (waitForRelease)
  235. {
  236. if ((digitalRead (AF_UP) == LOW) || (digitalRead (AF_DOWN) == LOW))
  237. continue ;
  238. else
  239. waitForRelease = FALSE ;
  240. }
  241. if (digitalRead (AF_UP) == LOW) // Pushed
  242. {
  243. colour = colour + 1 ;
  244. if (colour == 8)
  245. colour = 0 ;
  246. setBacklightColour (colour) ;
  247. waitForRelease = TRUE ;
  248. }
  249. if (digitalRead (AF_DOWN) == LOW) // Pushed
  250. {
  251. colour = colour - 1 ;
  252. if (colour == -1)
  253. colour = 7 ;
  254. setBacklightColour (colour) ;
  255. waitForRelease = TRUE ;
  256. }
  257. }
  258. return 0 ;
  259. }