Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.79 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. /* Pin definitions:
  4. Most of these pins can be moved to any digital or analog pin.
  5. DN(MOSI)and SCLK should be left where they are (SPI pins). The
  6. LED (backlight) pin should remain on a PWM-capable pin. */
  7. const int scePin = 7; // SCE - Chip select, pin 3 on LCD.
  8. const int rstPin = 6; // RST - Reset, pin 4 on LCD.
  9. const int dcPin = 5; // DC - Data/Command, pin 5 on LCD.
  10. const int sdinPin = 11; // DN(MOSI) - Serial data, pin 6 on LCD.
  11. const int sclkPin = 13; // SCLK - Serial clock, pin 7 on LCD.
  12. const int blPin = 9; // LED - Backlight LED, pin 8 on LCD.
  13.  
  14. /* PCD8544-specific defines: */
  15. #define LCD_COMMAND 0
  16. #define LCD_DATA 1
  17.  
  18. /* 84x48 LCD Defines: */
  19. #define LCD_WIDTH 84 // Note: x-coordinates go wide
  20. #define LCD_HEIGHT 48 // Note: y-coordinates go high
  21. #define WHITE 0 // For drawing pixels. A 0 draws white.
  22. #define BLACK 1 // A 1 draws black.
  23.  
  24. /* Font table:
  25. This table contains the hex values that represent pixels for a
  26. font that is 5 pixels wide and 8 pixels high. Each byte in a row
  27. represents one, 8-pixel, vertical column of a character. 5 bytes
  28. per character. */
  29. static const byte ASCII[][5] PROGMEM = {
  30. // First 32 characters (0x00-0x19) are ignored. These are
  31. // non-displayable, control characters.
  32. {0x00, 0x00, 0x00, 0x00, 0x00} // 0x20
  33. ,{0x00, 0x00, 0x5f, 0x00, 0x00} // 0x21 !
  34. ,{0x00, 0x07, 0x00, 0x07, 0x00} // 0x22 "
  35. ,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 0x23 #
  36. ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 0x24 $
  37. ,{0x23, 0x13, 0x08, 0x64, 0x62} // 0x25 %
  38. ,{0x36, 0x49, 0x55, 0x22, 0x50} // 0x26 &
  39. ,{0x00, 0x05, 0x03, 0x00, 0x00} // 0x27 '
  40. ,{0x00, 0x1c, 0x22, 0x41, 0x00} // 0x28 (
  41. ,{0x00, 0x41, 0x22, 0x1c, 0x00} // 0x29 )
  42. ,{0x14, 0x08, 0x3e, 0x08, 0x14} // 0x2a *
  43. ,{0x08, 0x08, 0x3e, 0x08, 0x08} // 0x2b +
  44. ,{0x00, 0x50, 0x30, 0x00, 0x00} // 0x2c ,
  45. ,{0x08, 0x08, 0x08, 0x08, 0x08} // 0x2d -
  46. ,{0x00, 0x60, 0x60, 0x00, 0x00} // 0x2e .
  47. ,{0x20, 0x10, 0x08, 0x04, 0x02} // 0x2f /
  48. ,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 0x30 0
  49. ,{0x00, 0x42, 0x7f, 0x40, 0x00} // 0x31 1
  50. ,{0x42, 0x61, 0x51, 0x49, 0x46} // 0x32 2
  51. ,{0x21, 0x41, 0x45, 0x4b, 0x31} // 0x33 3
  52. ,{0x18, 0x14, 0x12, 0x7f, 0x10} // 0x34 4
  53. ,{0x27, 0x45, 0x45, 0x45, 0x39} // 0x35 5
  54. ,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 0x36 6
  55. ,{0x01, 0x71, 0x09, 0x05, 0x03} // 0x37 7
  56. ,{0x36, 0x49, 0x49, 0x49, 0x36} // 0x38 8
  57. ,{0x06, 0x49, 0x49, 0x29, 0x1e} // 0x39 9
  58. ,{0x00, 0x36, 0x36, 0x00, 0x00} // 0x3a :
  59. ,{0x00, 0x56, 0x36, 0x00, 0x00} // 0x3b ;
  60. ,{0x08, 0x14, 0x22, 0x41, 0x00} // 0x3c <
  61. ,{0x14, 0x14, 0x14, 0x14, 0x14} // 0x3d =
  62. ,{0x00, 0x41, 0x22, 0x14, 0x08} // 0x3e >
  63. ,{0x02, 0x01, 0x51, 0x09, 0x06} // 0x3f ?
  64. ,{0x32, 0x49, 0x79, 0x41, 0x3e} // 0x40 @
  65. ,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 0x41 A
  66. ,{0x7f, 0x49, 0x49, 0x49, 0x36} // 0x42 B
  67. ,{0x3e, 0x41, 0x41, 0x41, 0x22} // 0x43 C
  68. ,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 0x44 D
  69. ,{0x7f, 0x49, 0x49, 0x49, 0x41} // 0x45 E
  70. ,{0x7f, 0x09, 0x09, 0x09, 0x01} // 0x46 F
  71. ,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 0x47 G
  72. ,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 0x48 H
  73. ,{0x00, 0x41, 0x7f, 0x41, 0x00} // 0x49 I
  74. ,{0x20, 0x40, 0x41, 0x3f, 0x01} // 0x4a J
  75. ,{0x7f, 0x08, 0x14, 0x22, 0x41} // 0x4b K
  76. ,{0x7f, 0x40, 0x40, 0x40, 0x40} // 0x4c L
  77. ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 0x4d M
  78. ,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 0x4e N
  79. ,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 0x4f O
  80. ,{0x7f, 0x09, 0x09, 0x09, 0x06} // 0x50 P
  81. ,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 0x51 Q
  82. ,{0x7f, 0x09, 0x19, 0x29, 0x46} // 0x52 R
  83. ,{0x46, 0x49, 0x49, 0x49, 0x31} // 0x53 S
  84. ,{0x01, 0x01, 0x7f, 0x01, 0x01} // 0x54 T
  85. ,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 0x55 U
  86. ,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 0x56 V
  87. ,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 0x57 W
  88. ,{0x63, 0x14, 0x08, 0x14, 0x63} // 0x58 X
  89. ,{0x07, 0x08, 0x70, 0x08, 0x07} // 0x59 Y
  90. ,{0x61, 0x51, 0x49, 0x45, 0x43} // 0x5a Z
  91. ,{0x00, 0x7f, 0x41, 0x41, 0x00} // 0x5b [
  92. ,{0x02, 0x04, 0x08, 0x10, 0x20} // 0x5c \ (keep this to escape the backslash)
  93. ,{0x00, 0x41, 0x41, 0x7f, 0x00} // 0x5d ]
  94. ,{0x04, 0x02, 0x01, 0x02, 0x04} // 0x5e ^
  95. ,{0x40, 0x40, 0x40, 0x40, 0x40} // 0x5f _
  96. ,{0x00, 0x01, 0x02, 0x04, 0x00} // 0x60 `
  97. ,{0x20, 0x54, 0x54, 0x54, 0x78} // 0x61 a
  98. ,{0x7f, 0x48, 0x44, 0x44, 0x38} // 0x62 b
  99. ,{0x38, 0x44, 0x44, 0x44, 0x20} // 0x63 c
  100. ,{0x38, 0x44, 0x44, 0x48, 0x7f} // 0x64 d
  101. ,{0x38, 0x54, 0x54, 0x54, 0x18} // 0x65 e
  102. ,{0x08, 0x7e, 0x09, 0x01, 0x02} // 0x66 f
  103. ,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 0x67 g
  104. ,{0x7f, 0x08, 0x04, 0x04, 0x78} // 0x68 h
  105. ,{0x00, 0x44, 0x7d, 0x40, 0x00} // 0x69 i
  106. ,{0x20, 0x40, 0x44, 0x3d, 0x00} // 0x6a j
  107. ,{0x7f, 0x10, 0x28, 0x44, 0x00} // 0x6b k
  108. ,{0x00, 0x41, 0x7f, 0x40, 0x00} // 0x6c l
  109. ,{0x7c, 0x04, 0x18, 0x04, 0x78} // 0x6d m
  110. ,{0x7c, 0x08, 0x04, 0x04, 0x78} // 0x6e n
  111. ,{0x38, 0x44, 0x44, 0x44, 0x38} // 0x6f o
  112. ,{0x7c, 0x14, 0x14, 0x14, 0x08} // 0x70 p
  113. ,{0x08, 0x14, 0x14, 0x18, 0x7c} // 0x71 q
  114. ,{0x7c, 0x08, 0x04, 0x04, 0x08} // 0x72 r
  115. ,{0x48, 0x54, 0x54, 0x54, 0x20} // 0x73 s
  116. ,{0x04, 0x3f, 0x44, 0x40, 0x20} // 0x74 t
  117. ,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 0x75 u
  118. ,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 0x76 v
  119. ,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 0x77 w
  120. ,{0x44, 0x28, 0x10, 0x28, 0x44} // 0x78 x
  121. ,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 0x79 y
  122. ,{0x44, 0x64, 0x54, 0x4c, 0x44} // 0x7a z
  123. ,{0x00, 0x08, 0x36, 0x41, 0x00} // 0x7b {
  124. ,{0x00, 0x00, 0x7f, 0x00, 0x00} // 0x7c |
  125. ,{0x00, 0x41, 0x36, 0x08, 0x00} // 0x7d }
  126. ,{0x10, 0x08, 0x08, 0x10, 0x08} // 0x7e ~
  127. ,{0x78, 0x46, 0x41, 0x46, 0x78} // 0x7f DEL
  128. };
  129.  
  130. /* The displayMap variable stores a buffer representation of the
  131. pixels on our display. There are 504 total bits in this array,
  132. same as how many pixels there are on a 84 x 48 display.
  133.  
  134. Each byte in this array covers a 8-pixel vertical block on the
  135. display. Each successive byte covers the next 8-pixel column over
  136. until you reach the right-edge of the display and step down 8 rows.
  137.  
  138. To update the display, we first have to write to this array, then
  139. call the updateDisplay() function, which sends this whole array
  140. to the PCD8544.
  141.  
  142. Because the PCD8544 won't let us write individual pixels at a
  143. time, this is how we can make targeted changes to the display. */
  144. byte displayMap[LCD_WIDTH * LCD_HEIGHT / 8] = {
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (0,0)->(11,7) ~ These 12 bytes cover an 8x12 block in the left corner of the display
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (12,0)->(23,7)
  147. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, // (24,0)->(35,7)
  148. 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFE, 0x1E, 0x0E, 0x02, 0x00, // (36,0)->(47,7)
  149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (48,0)->(59,7)
  150. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (60,0)->(71,7)
  151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (72,0)->(83,7)
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (0,8)->(11,15)
  153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (12,8)->(23,15)
  154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // (24,8)->(35,15)
  155. 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0xF8, // (36,8)->(47,15)
  156. 0xF8, 0xF0, 0xF8, 0xFE, 0xFE, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, // (48,8)->(59,15)
  157. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (60,8)->(71,15)
  158. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (72,8)->(83,15)
  159. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (0,16)->(11,23)
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (12,16)->(23,23)
  161. 0x00, 0x00, 0xF8, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xF3, 0xE0, 0xE0, 0xC0, // (24,16)->(35,23)
  162. 0xC0, 0xC0, 0xE0, 0xE0, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // (36,16)->(47,23)
  163. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0x00, 0x00, 0x00, // (48,16)->(59,23)
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (60,16)->(71,23)
  165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (72,16)->(83,23)
  166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (0,24)->(11,31)
  167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (12,24)->(23,31)
  168. 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // (24,24)->(35,31)
  169. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // (36,24)->(47,31)
  170. 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, // (48,24)->(59,31)
  171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (60,24)->(71,31)
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (72,24)->(83,31)
  173. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (0,32)->(11,39)
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (12,32)->(23,39)
  175. 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, // (24,32)->(35,39)
  176. 0x0F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03, 0x03, // (36,32)->(47,39)
  177. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (48,32)->(59,39)
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (60,32)->(71,39)
  179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (72,32)->(83,39)
  180. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (0,40)->(11,47)
  181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (12,40)->(23,47)
  182. 0x00, 0x00, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, // (24,40)->(35,47)
  183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (36,40)->(47,47)
  184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (48,40)->(59,47)
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (60,40)->(71,47)
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // (72,40)->(83,47) !!! The bottom right pixel!
  187. };
  188.  
  189. // There are two memory banks in the LCD, data/RAM and commands.
  190. // This function sets the DC pin high or low depending, and then
  191. // sends the data byte
  192. void LCDWrite(byte data_or_command, byte data)
  193. {
  194. //Tell the LCD that we are writing either to data or a command
  195. digitalWrite(dcPin, data_or_command);
  196.  
  197. //Send the data
  198. digitalWrite(scePin, LOW);
  199. SPI.transfer(data); //shiftOut(sdinPin, sclkPin, MSBFIRST, data);
  200. digitalWrite(scePin, HIGH);
  201. }
  202.  
  203.  
  204. // This function sets a pixel on displayMap to your preferred
  205. // color. 1=Black, 0= white.
  206. void setPixel(int x, int y, boolean bw)
  207. {
  208. // First, double check that the coordinate is in range.
  209. if ((x >= 0) && (x < LCD_WIDTH) && (y >= 0) && (y < LCD_HEIGHT))
  210. {
  211. byte shift = y % 8;
  212.  
  213. if (bw) // If black, set the bit.
  214. displayMap[x + (y/8)*LCD_WIDTH] |= 1<<shift;
  215. else // If white clear the bit.
  216. displayMap[x + (y/8)*LCD_WIDTH] &= ~(1<<shift);
  217. }
  218. }
  219.  
  220. // Because I keep forgetting to put bw variable in when setting...
  221. void setPixel(int x, int y)
  222. {
  223. setPixel(x, y, BLACK); // Call setPixel with bw set to Black
  224. }
  225.  
  226. void clearPixel(int x, int y)
  227. {
  228. setPixel(x, y, WHITE); // call setPixel with bw set to white
  229. }
  230.  
  231. // setLine draws a line from x0,y0 to x1,y1 with the set color.
  232. // This function was grabbed from the SparkFun ColorLCDShield
  233. // library.
  234. void setLine(int x0, int y0, int x1, int y1, boolean bw)
  235. {
  236. int dy = y1 - y0; // Difference between y0 and y1
  237. int dx = x1 - x0; // Difference between x0 and x1
  238. int stepx, stepy;
  239.  
  240. if (dy < 0)
  241. {
  242. dy = -dy;
  243. stepy = -1;
  244. }
  245. else
  246. stepy = 1;
  247.  
  248. if (dx < 0)
  249. {
  250. dx = -dx;
  251. stepx = -1;
  252. }
  253. else
  254. stepx = 1;
  255.  
  256. dy <<= 1; // dy is now 2*dy
  257. dx <<= 1; // dx is now 2*dx
  258. setPixel(x0, y0, bw); // Draw the first pixel.
  259.  
  260. if (dx > dy)
  261. {
  262. int fraction = dy - (dx >> 1);
  263. while (x0 != x1)
  264. {
  265. if (fraction >= 0)
  266. {
  267. y0 += stepy;
  268. fraction -= dx;
  269. }
  270. x0 += stepx;
  271. fraction += dy;
  272. setPixel(x0, y0, bw);
  273. }
  274. }
  275. else
  276. {
  277. int fraction = dx - (dy >> 1);
  278. while (y0 != y1)
  279. {
  280. if (fraction >= 0)
  281. {
  282. x0 += stepx;
  283. fraction -= dy;
  284. }
  285. y0 += stepy;
  286. fraction += dx;
  287. setPixel(x0, y0, bw);
  288. }
  289. }
  290. }
  291.  
  292. // setRect will draw a rectangle from x0,y0 top-left corner to
  293. // a x1,y1 bottom-right corner. Can be filled with the fill
  294. // parameter, and colored with bw.
  295. // This function was grabbed from the SparkFun ColorLCDShield
  296. // library.
  297. void setRect(int x0, int y0, int x1, int y1, boolean fill, boolean bw)
  298. {
  299. // check if the rectangle is to be filled
  300. if (fill == 1)
  301. {
  302. int xDiff;
  303.  
  304. if(x0 > x1)
  305. xDiff = x0 - x1; //Find the difference between the x vars
  306. else
  307. xDiff = x1 - x0;
  308.  
  309. while(xDiff > 0)
  310. {
  311. setLine(x0, y0, x0, y1, bw);
  312.  
  313. if(x0 > x1)
  314. x0--;
  315. else
  316. x0++;
  317.  
  318. xDiff--;
  319. }
  320. }
  321. else
  322. {
  323. // best way to draw an unfilled rectangle is to draw four lines
  324. setLine(x0, y0, x1, y0, bw);
  325. setLine(x0, y1, x1, y1, bw);
  326. setLine(x0, y0, x0, y1, bw);
  327. setLine(x1, y0, x1, y1, bw);
  328. }
  329. }
  330.  
  331. // setCircle draws a circle centered around x0,y0 with a defined
  332. // radius. The circle can be black or white. And have a line
  333. // thickness ranging from 1 to the radius of the circle.
  334. // This function was grabbed from the SparkFun ColorLCDShield
  335. // library.
  336. void setCircle (int x0, int y0, int radius, boolean bw, int lineThickness)
  337. {
  338. for(int r = 0; r < lineThickness; r++)
  339. {
  340. int f = 1 - radius;
  341. int ddF_x = 0;
  342. int ddF_y = -2 * radius;
  343. int x = 0;
  344. int y = radius;
  345.  
  346. setPixel(x0, y0 + radius, bw);
  347. setPixel(x0, y0 - radius, bw);
  348. setPixel(x0 + radius, y0, bw);
  349. setPixel(x0 - radius, y0, bw);
  350.  
  351. while(x < y)
  352. {
  353. if(f >= 0)
  354. {
  355. y--;
  356. ddF_y += 2;
  357. f += ddF_y;
  358. }
  359. x++;
  360. ddF_x += 2;
  361. f += ddF_x + 1;
  362.  
  363. setPixel(x0 + x, y0 + y, bw);
  364. setPixel(x0 - x, y0 + y, bw);
  365. setPixel(x0 + x, y0 - y, bw);
  366. setPixel(x0 - x, y0 - y, bw);
  367. setPixel(x0 + y, y0 + x, bw);
  368. setPixel(x0 - y, y0 + x, bw);
  369. setPixel(x0 + y, y0 - x, bw);
  370. setPixel(x0 - y, y0 - x, bw);
  371. }
  372. radius--;
  373. }
  374. }
  375.  
  376. // This function will draw a char (defined in the ASCII table
  377. // near the beginning of this sketch) at a defined x and y).
  378. // The color can be either black (1) or white (0).
  379. void setChar(char character, int x, int y, boolean bw)
  380. {
  381. byte column; // temp byte to store character's column bitmap
  382. for (int i=0; i<5; i++) // 5 columns (x) per character
  383. {
  384. column = pgm_read_byte(&ASCII[character - 0x20][i]);
  385. for (int j=0; j<8; j++) // 8 rows (y) per character
  386. {
  387. if (column & (0x01 << j)) // test bits to set pixels
  388. setPixel(x+i, y+j, bw);
  389. else
  390. setPixel(x+i, y+j, !bw);
  391. }
  392. }
  393. }
  394.  
  395. // setStr draws a string of characters, calling setChar with
  396. // progressive coordinates until it's done.
  397. // This function was grabbed from the SparkFun ColorLCDShield
  398. // library.
  399. void setStr(char * dString, int x, int y, boolean bw)
  400. {
  401. while (*dString != 0x00) // loop until null terminator
  402. {
  403. setChar(*dString++, x, y, bw);
  404. x+=5;
  405. for (int i=y; i<y+8; i++)
  406. {
  407. setPixel(x, i, !bw);
  408. }
  409. x++;
  410. if (x > (LCD_WIDTH - 5)) // Enables wrap around
  411. {
  412. x = 0;
  413. y += 8;
  414. }
  415. }
  416. }
  417.  
  418. // This function will draw an array over the screen. (For now) the
  419. // array must be the same size as the screen, covering the entirety
  420. // of the display.
  421. // Also, the array must reside in FLASH and declared with PROGMEM.
  422. void setBitmap(const char * bitArray)
  423. {
  424. for (int i=0; i<(LCD_WIDTH * LCD_HEIGHT / 8); i++)
  425. {
  426. char c = pgm_read_byte(&bitArray[i]);
  427. displayMap[i] = c;
  428. }
  429. }
  430.  
  431. // This function clears the entire display either white (0) or
  432. // black (1).
  433. // The screen won't actually clear until you call updateDisplay()!
  434. void clearDisplay(boolean bw)
  435. {
  436. for (int i=0; i<(LCD_WIDTH * LCD_HEIGHT / 8); i++)
  437. {
  438. if (bw)
  439. displayMap[i] = 0xFF;
  440. else
  441. displayMap[i] = 0;
  442. }
  443. }
  444.  
  445. // Helpful function to directly command the LCD to go to a
  446. // specific x,y coordinate.
  447. void gotoXY(int x, int y)
  448. {
  449. LCDWrite(0, 0x80 | x); // Column.
  450. LCDWrite(0, 0x40 | y); // Row. ?
  451. }
  452.  
  453. // This will actually draw on the display, whatever is currently
  454. // in the displayMap array.
  455. void updateDisplay()
  456. {
  457. gotoXY(0, 0);
  458. for (int i=0; i < (LCD_WIDTH * LCD_HEIGHT / 8); i++)
  459. {
  460. LCDWrite(LCD_DATA, displayMap[i]);
  461. }
  462. }
  463.  
  464. // Set contrast can set the LCD Vop to a value between 0 and 127.
  465. // 40-60 is usually a pretty good range.
  466. void setContrast(byte contrast)
  467. {
  468. LCDWrite(LCD_COMMAND, 0x21); //Tell LCD that extended commands follow
  469. LCDWrite(LCD_COMMAND, 0x80 | contrast); //Set LCD Vop (Contrast): Try 0xB1(good @ 3.3V) or 0xBF if your display is too dark
  470. LCDWrite(LCD_COMMAND, 0x20); //Set display mode
  471. }
  472.  
  473. /* There are two ways to do this. Either through direct commands
  474. to the display, or by swapping each bit in the displayMap array.
  475. We'll leave both methods here, comment one or the other out if
  476. you please. */
  477. void invertDisplay()
  478. {
  479. /* Direct LCD Command option
  480. LCDWrite(LCD_COMMAND, 0x20); //Tell LCD that extended commands follow
  481. LCDWrite(LCD_COMMAND, 0x08 | 0x05); //Set LCD Vop (Contrast): Try 0xB1(good @ 3.3V) or 0xBF if your display is too dark
  482. LCDWrite(LCD_COMMAND, 0x20); //Set display mode */
  483.  
  484. /* Indirect, swap bits in displayMap option: */
  485. for (int i=0; i < (LCD_WIDTH * LCD_HEIGHT / 8); i++)
  486. {
  487. displayMap[i] = ~displayMap[i] & 0xFF;
  488. }
  489. updateDisplay();
  490. }
  491.  
  492. //This sends the magical commands to the PCD8544
  493. void lcdBegin(void)
  494. {
  495. //Configure control pins
  496. pinMode(scePin, OUTPUT);
  497. pinMode(rstPin, OUTPUT);
  498. pinMode(dcPin, OUTPUT);
  499. pinMode(sdinPin, OUTPUT);
  500. pinMode(sclkPin, OUTPUT);
  501. pinMode(blPin, OUTPUT);
  502. analogWrite(blPin, 255);
  503.  
  504. SPI.begin();
  505. SPI.setDataMode(SPI_MODE0);
  506. SPI.setBitOrder(MSBFIRST);
  507.  
  508. //Reset the LCD to a known state
  509. digitalWrite(rstPin, LOW);
  510. digitalWrite(rstPin, HIGH);
  511.  
  512. LCDWrite(LCD_COMMAND, 0x21); //Tell LCD extended commands follow
  513. LCDWrite(LCD_COMMAND, 0xB0); //Set LCD Vop (Contrast)
  514. LCDWrite(LCD_COMMAND, 0x04); //Set Temp coefficent
  515. LCDWrite(LCD_COMMAND, 0x14); //LCD bias mode 1:48 (try 0x13)
  516. //We must send 0x20 before modifying the display control mode
  517. LCDWrite(LCD_COMMAND, 0x20);
  518. LCDWrite(LCD_COMMAND, 0x0C); //Set display control, normal mode.
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement