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.
 
 
 
 
 

431 lines
9.1 KiB

  1. /*
  2. * scrollPhat.c:
  3. * Simple driver for the Pimoroni Scroll Phat device
  4. *
  5. * Copyright (c) 2015 Gordon Henderson.
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/wiringpi/
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <wiringPiI2C.h>
  30. #include "scrollPhatFont.h"
  31. #include "scrollPhat.h"
  32. // Size
  33. #define SP_WIDTH 11
  34. #define SP_HEIGHT 5
  35. // I2C
  36. #define PHAT_I2C_ADDR 0x60
  37. // Software copy of the framebuffer
  38. // it's 8-bit deep although the display itself is only 1-bit deep.
  39. static unsigned char frameBuffer [SP_WIDTH * SP_HEIGHT] ;
  40. static int lastX, lastY ;
  41. static int printDelayFactor ;
  42. static int scrollPhatFd ;
  43. static int putcharX ;
  44. #undef DEBUG
  45. /*
  46. * delay:
  47. * Wait for some number of milliseconds.
  48. * This taken from wiringPi as there is no-need to include the whole of
  49. * wiringPi just for the delay function.
  50. *********************************************************************************
  51. */
  52. static void delay (unsigned int howLong)
  53. {
  54. struct timespec sleeper, dummy ;
  55. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  56. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  57. nanosleep (&sleeper, &dummy) ;
  58. }
  59. /*
  60. * scrollPhatUpdate:
  61. * Copy our software version to the real display
  62. *********************************************************************************
  63. */
  64. void scrollPhatUpdate (void)
  65. {
  66. register int x, y ;
  67. register unsigned char data, pixel ;
  68. unsigned char pixels [SP_WIDTH] ;
  69. #ifdef DEBUG
  70. printf ("+-----------+\n") ;
  71. for (y = 0 ; y < SP_HEIGHT ; ++y)
  72. {
  73. putchar ('|') ;
  74. for (x = 0 ; x < SP_WIDTH ; ++x)
  75. {
  76. pixel = frameBuffer [x + y * SP_WIDTH] ;
  77. putchar (pixel == 0 ? ' ' : '*') ;
  78. }
  79. printf ("|\n") ;
  80. }
  81. printf ("+-----------+\n") ;
  82. #endif
  83. for (x = 0 ; x < SP_WIDTH ; ++x)
  84. {
  85. data = 0 ;
  86. for (y = 0 ; y < SP_HEIGHT ; ++y)
  87. {
  88. pixel = frameBuffer [x + y * SP_WIDTH] ;
  89. data = (data << 1) | ((pixel == 0) ? 0 : 1) ;
  90. }
  91. pixels [x] = data ;
  92. }
  93. for (x = 0 ; x < SP_WIDTH ; ++x)
  94. wiringPiI2CWriteReg8 (scrollPhatFd, 1 + x, pixels [x]) ;
  95. wiringPiI2CWriteReg8 (scrollPhatFd, 0x0C, 0) ;
  96. }
  97. /*
  98. *********************************************************************************
  99. * Standard Graphical Functions
  100. *********************************************************************************
  101. */
  102. /*
  103. * scrollPhatPoint:
  104. * Plot a pixel. Crude clipping - speed is not the essence here.
  105. *********************************************************************************
  106. */
  107. void scrollPhatPoint (int x, int y, int colour)
  108. {
  109. lastX = x ;
  110. lastY = y ;
  111. if ((x < 0) || (x >= SP_WIDTH) || (y < 0) || (y >= SP_HEIGHT))
  112. return ;
  113. frameBuffer [x + y * SP_WIDTH] = colour ;
  114. }
  115. /*
  116. * scrollPhatLine: scrollPhatLineTo:
  117. * Classic Bressenham Line code - rely on the point function to do the
  118. * clipping for us here.
  119. *********************************************************************************
  120. */
  121. void scrollPhatLine (int x0, int y0, int x1, int y1, int colour)
  122. {
  123. int dx, dy ;
  124. int sx, sy ;
  125. int err, e2 ;
  126. lastX = x1 ;
  127. lastY = y1 ;
  128. dx = abs (x1 - x0) ;
  129. dy = abs (y1 - y0) ;
  130. sx = (x0 < x1) ? 1 : -1 ;
  131. sy = (y0 < y1) ? 1 : -1 ;
  132. err = dx - dy ;
  133. for (;;)
  134. {
  135. scrollPhatPoint (x0, y0, colour) ;
  136. if ((x0 == x1) && (y0 == y1))
  137. break ;
  138. e2 = 2 * err ;
  139. if (e2 > -dy)
  140. {
  141. err -= dy ;
  142. x0 += sx ;
  143. }
  144. if (e2 < dx)
  145. {
  146. err += dx ;
  147. y0 += sy ;
  148. }
  149. }
  150. }
  151. void scrollPhatLineTo (int x, int y, int colour)
  152. {
  153. scrollPhatLine (lastX, lastY, x, y, colour) ;
  154. }
  155. /*
  156. * scrollPhatRectangle:
  157. * A rectangle is a spoilt days fishing
  158. *********************************************************************************
  159. */
  160. void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled)
  161. {
  162. register int x ;
  163. if (filled)
  164. {
  165. /**/ if (x1 == x2)
  166. scrollPhatLine (x1, y1, x2, y2, colour) ;
  167. else if (x1 < x2)
  168. for (x = x1 ; x <= x2 ; ++x)
  169. scrollPhatLine (x, y1, x, y2, colour) ;
  170. else
  171. for (x = x2 ; x <= x1 ; ++x)
  172. scrollPhatLine (x, y1, x, y2, colour) ;
  173. }
  174. else
  175. {
  176. scrollPhatLine (x1, y1, x2, y1, colour) ;
  177. scrollPhatLineTo (x2, y2, colour) ;
  178. scrollPhatLineTo (x1, y2, colour) ;
  179. scrollPhatLineTo (x1, y1, colour) ;
  180. }
  181. }
  182. /*
  183. * scrollPhatPutchar:
  184. * Print a single character to the screen then advance the pointer by an
  185. * appropriate ammount (variable width font).
  186. * We rely on the clipping done by the pixel plot function to keep us
  187. * out of trouble.
  188. * Return the width + space
  189. *********************************************************************************
  190. */
  191. int scrollPhatPutchar (int c)
  192. {
  193. register int x, y ;
  194. unsigned char line ;
  195. unsigned char *fontPtr ;
  196. unsigned char *p2 ;
  197. int lineWidth, width, mask ;
  198. // The font is printable characters, uppercase only...
  199. // and somewhat varaible width...
  200. c &= 0x7F ;
  201. if (c > 0x60)
  202. c -= 64 ;
  203. else
  204. c -= 32 ;
  205. fontPtr = scrollPhatFont + c * fontHeight ;
  206. // Work out width of this character
  207. // There probably is a more efficient way to do this, but...
  208. p2 = fontPtr ;
  209. width = 0 ;
  210. for (y = 0 ; y < fontHeight ; ++y)
  211. {
  212. mask = 0x80 ;
  213. for (lineWidth = 8 ; lineWidth > 0 ; --lineWidth)
  214. {
  215. if ((*p2 & mask) != 0)
  216. break ;
  217. mask >>= 1 ;
  218. }
  219. if (lineWidth > width)
  220. width = lineWidth ;
  221. ++p2 ;
  222. }
  223. if (width == 0) // Likely to be a blank or space character
  224. width = 3 ;
  225. for (y = fontHeight - 1 ; y >= 0 ; --y)
  226. {
  227. x = 0 ;
  228. line = *fontPtr++ ;
  229. for (mask = 1 << (width - 1) ; mask != 0 ; mask >>= 1)
  230. {
  231. scrollPhatPoint (putcharX + x, y, (line & mask)) ;
  232. ++x ;
  233. }
  234. }
  235. // make a line of space
  236. for (y = fontHeight - 1 ; y >= 0 ; --y)
  237. scrollPhatPoint (putcharX + width, y, 0) ;
  238. putcharX = putcharX + width + 1 ;
  239. return width + 1 ;
  240. }
  241. /*
  242. * scrollPhatPuts:
  243. * Send a string to the display - and scroll it across.
  244. * This is somewhat of a hack in that we print the entire string to the
  245. * display and let the point clipping take care of what's off-screen...
  246. *********************************************************************************
  247. */
  248. void scrollPhatPuts (const char *str)
  249. {
  250. int i ;
  251. int movingX = 0 ;
  252. const char *s ;
  253. int pixelLen ;
  254. // Print it once, then we know the width in pixels...
  255. putcharX = 0 ;
  256. s = str ;
  257. while (*s)
  258. scrollPhatPutchar (*s++) ;
  259. pixelLen = putcharX ;
  260. // Now scroll it by printing it and moving left one pixel
  261. movingX = 0 ;
  262. for (i = 0 ; i < pixelLen ; ++i)
  263. {
  264. putcharX = movingX ;
  265. s = str ;
  266. while (*s)
  267. scrollPhatPutchar (*s++) ;
  268. --movingX ;
  269. scrollPhatUpdate () ;
  270. delay (printDelayFactor) ;
  271. }
  272. }
  273. /*
  274. * scrollPhatPrintf:
  275. * Does what it says
  276. *********************************************************************************
  277. */
  278. void scrollPhatPrintf (const char *message, ...)
  279. {
  280. va_list argp ;
  281. char buffer [1024] ;
  282. va_start (argp, message) ;
  283. vsnprintf (buffer, 1023, message, argp) ;
  284. va_end (argp) ;
  285. scrollPhatPuts (buffer) ;
  286. }
  287. /*
  288. * scrollPhatPrintSpeed:
  289. * Change the print speed - mS per shift by 1 pixel
  290. *********************************************************************************
  291. */
  292. void scrollPhatPrintSpeed (const int pps)
  293. {
  294. if (pps < 0)
  295. printDelayFactor = 0 ;
  296. else
  297. printDelayFactor = pps ;
  298. }
  299. /*
  300. * scrollPhatClear:
  301. * Clear the display
  302. *********************************************************************************
  303. */
  304. void scrollPhatClear (void)
  305. {
  306. register int i ;
  307. register unsigned char *ptr = frameBuffer ;
  308. for (i = 0 ; i < (SP_WIDTH * SP_HEIGHT) ; ++i)
  309. *ptr++ = 0 ;
  310. scrollPhatUpdate () ;
  311. }
  312. /*
  313. * scrollPhatIntensity:
  314. * Set the display brightness - percentage
  315. *********************************************************************************
  316. */
  317. void scrollPhatIntensity (const int percent)
  318. {
  319. wiringPiI2CWriteReg8 (scrollPhatFd, 0x19, (127 * percent) / 100) ;
  320. }
  321. /*
  322. * scrollPhatSetup:
  323. * Initialise the Scroll Phat display
  324. *********************************************************************************
  325. */
  326. int scrollPhatSetup (void)
  327. {
  328. if ((scrollPhatFd = wiringPiI2CSetup (PHAT_I2C_ADDR)) < 0)
  329. return scrollPhatFd ;
  330. wiringPiI2CWriteReg8 (scrollPhatFd, 0x00, 0x03) ; // Enable display, set to 5x11 mode
  331. scrollPhatIntensity (10) ;
  332. scrollPhatClear () ;
  333. scrollPhatPrintSpeed (100) ;
  334. return 0 ;
  335. }