Advertisement
Guest User

Untitled

a guest
May 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. /*********************************************************************
  2. This is an example sketch for our Monochrome Nokia 5110 LCD Displays
  3.  
  4. Pick one up today in the adafruit shop!
  5. ------> http://www.adafruit.com/products/338
  6.  
  7. These displays use SPI to communicate, 4 or 5 pins are required to
  8. interface
  9.  
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit and open-source hardware by purchasing
  12. products from Adafruit!
  13.  
  14. Written by Limor Fried/Ladyada for Adafruit Industries.
  15. BSD license, check license.txt for more information
  16. All text above, and the splash screen must be included in any redistribution
  17. *********************************************************************/
  18.  
  19. #include <SPI.h>
  20. #include <Adafruit_GFX.h>
  21. #include <Adafruit_PCD8544.h>
  22. #include <String.h>
  23. // Software SPI (slower updates, more flexible pin options):
  24. // pin 7 - Serial clock out (SCLK)
  25. // pin 6 - Serial data out (DIN)
  26. // pin 5 - Data/Command select (D/C)
  27. // pin 4 - LCD chip select (CS)
  28. // pin 3 - LCD reset (RST)
  29. Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
  30.  
  31. // Hardware SPI (faster, but must use certain hardware pins):
  32. // SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
  33. // MOSI is LCD DIN - this is pin 11 on an Arduino Uno
  34. // pin 5 - Data/Command select (D/C)
  35. // pin 4 - LCD chip select (CS)
  36. // pin 3 - LCD reset (RST)
  37. // Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
  38. // Note with hardware SPI MISO and SS pins aren't used but will still be read
  39. // and written to during SPI transfer. Be careful sharing these pins!
  40.  
  41. #define NUMFLAKES 10
  42. #define XPOS 0
  43. #define YPOS 1
  44. #define DELTAY 2
  45.  
  46.  
  47. #define LOGO16_GLCD_HEIGHT 16
  48. #define LOGO16_GLCD_WIDTH 16
  49.  
  50. static const unsigned char PROGMEM logo16_glcd_bmp[] =
  51. { B00000000, B11000000,
  52. B00000001, B11000000,
  53. B00000001, B11000000,
  54. B00000011, B11100000,
  55. B11110011, B11100000,
  56. B11111110, B11111000,
  57. B01111110, B11111111,
  58. B00110011, B10011111,
  59. B00011111, B11111100,
  60. B00001101, B01110000,
  61. B00011011, B10100000,
  62. B00111111, B11100000,
  63. B00111111, B11110000,
  64. B01111100, B11110000,
  65. B01110000, B01110000,
  66. B00000000, B00110000
  67. };
  68.  
  69. void setup() {
  70. Serial.begin(9600);
  71.  
  72. display.begin();
  73. // init done
  74.  
  75. // you can change the contrast around to adapt the display
  76. // for the best viewing!
  77. display.setContrast(60);
  78.  
  79. // display.display(); // show splashscreen
  80. // delay(2000);
  81. display.clearDisplay(); // clears the screen and buffer
  82. Serial.println("Baga comanda:");
  83.  
  84. /*if (Serial.read()==1){
  85. testdrawcircle();
  86. display.display();
  87. delay(2000);
  88. display.clearDisplay();
  89. }*/
  90. /* // draw a single pixel
  91. display.drawPixel(10, 10, BLACK);
  92. display.display();
  93. delay(2000);
  94. display.clearDisplay();
  95.  
  96. // draw many lines
  97. testdrawline();
  98. display.display();
  99. delay(2000);
  100. display.clearDisplay();
  101.  
  102. // draw rectangles
  103. testdrawrect();
  104. display.display();
  105. delay(2000);
  106. display.clearDisplay();
  107.  
  108. // draw multiple rectangles
  109. testfillrect();
  110. display.display();
  111. delay(2000);
  112. display.clearDisplay();
  113.  
  114. // draw mulitple circles
  115. testdrawcircle();
  116. display.display();
  117. delay(2000);
  118. display.clearDisplay();
  119.  
  120. // draw a circle, 10 pixel radius
  121. display.fillCircle(display.width()/2, display.height()/2, 10, BLACK);
  122. display.display();
  123. delay(2000);
  124. display.clearDisplay();
  125.  
  126. testdrawroundrect();
  127. delay(2000);
  128. display.clearDisplay();
  129.  
  130. testfillroundrect();
  131. delay(2000);
  132. display.clearDisplay();
  133.  
  134. testdrawtriangle();
  135. delay(2000);
  136. display.clearDisplay();
  137.  
  138. testfilltriangle();
  139. delay(2000);
  140. display.clearDisplay();
  141.  
  142. // draw the first ~12 characters in the font
  143. testdrawchar();
  144. display.display();
  145. delay(2000);
  146. display.clearDisplay();
  147.  
  148. // text display tests
  149. display.setTextSize(1);
  150. display.setTextColor(BLACK);
  151. display.setCursor(0,0);
  152. display.println("Hello, world!");
  153. display.setTextColor(WHITE, BLACK); // 'inverted' text
  154. display.println(3.141592);
  155. display.setTextSize(2);
  156. display.setTextColor(BLACK);
  157. display.print("0x"); display.println(0xDEADBEEF, HEX);
  158. display.display();
  159. delay(2000);
  160.  
  161. // rotation example
  162. display.clearDisplay();
  163. display.setRotation(1); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further.
  164. display.setTextSize(1);
  165. display.setTextColor(BLACK);
  166. display.setCursor(0,0);
  167. display.println("Rotation");
  168. display.setTextSize(2);
  169. display.println("Example!");
  170. display.display();
  171. delay(2000);
  172.  
  173. // revert back to no rotation
  174. display.setRotation(0);
  175.  
  176. // miniature bitmap display
  177. display.clearDisplay();
  178. display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1);
  179. display.display();
  180.  
  181. // invert the display
  182. display.invertDisplay(true);
  183. delay(1000);
  184. display.invertDisplay(false);
  185. delay(1000);
  186.  
  187. // draw a bitmap icon and 'animate' movement
  188. testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT);*/
  189. }
  190.  
  191.  
  192. void loop() {
  193.  
  194. if (Serial.read() == 'x') {
  195. Serial.println("Am primit ceva");
  196. testdrawcircle();
  197. // display.display();
  198. delay(2000);
  199. display.clearDisplay();
  200. }
  201.  
  202. }
  203.  
  204.  
  205. void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  206. uint8_t icons[NUMFLAKES][3];
  207. randomSeed(666); // whatever seed
  208.  
  209. // initialize
  210. for (uint8_t f = 0; f < NUMFLAKES; f++) {
  211. icons[f][XPOS] = random(display.width());
  212. icons[f][YPOS] = 0;
  213. icons[f][DELTAY] = random(5) + 1;
  214.  
  215. Serial.print("x: ");
  216. Serial.print(icons[f][XPOS], DEC);
  217. Serial.print(" y: ");
  218. Serial.print(icons[f][YPOS], DEC);
  219. Serial.print(" dy: ");
  220. Serial.println(icons[f][DELTAY], DEC);
  221. }
  222.  
  223. while (1) {
  224. // draw each icon
  225. for (uint8_t f = 0; f < NUMFLAKES; f++) {
  226. display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, BLACK);
  227. }
  228. display.display();
  229. delay(200);
  230.  
  231. // then erase it + move it
  232. for (uint8_t f = 0; f < NUMFLAKES; f++) {
  233. display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, WHITE);
  234. // move it
  235. icons[f][YPOS] += icons[f][DELTAY];
  236. // if its gone, reinit
  237. if (icons[f][YPOS] > display.height()) {
  238. icons[f][XPOS] = random(display.width());
  239. icons[f][YPOS] = 0;
  240. icons[f][DELTAY] = random(5) + 1;
  241. }
  242. }
  243. }
  244. }
  245.  
  246.  
  247. void testdrawchar(void) {
  248. display.setTextSize(1);
  249. display.setTextColor(BLACK);
  250. display.setCursor(0, 0);
  251.  
  252. for (uint8_t i = 0; i < 168; i++) {
  253. if (i == '\n') continue;
  254. display.write(i);
  255. //if ((i > 0) && (i % 14 == 0))
  256. //display.println();
  257. }
  258. display.display();
  259. }
  260.  
  261. void testdrawcircle(void) {
  262. // for (int16_t i = 0; i < display.height(); i += 2) {
  263. display.drawCircle(display.width() / 2, display.height() / 2, 10, BLACK);
  264. display.display();
  265. // }
  266. }
  267.  
  268. void testfillrect(void) {
  269. uint8_t color = 1;
  270. for (int16_t i = 0; i < display.height() / 2; i += 3) {
  271. // alternate colors
  272. display.fillRect(i, i, display.width() - i * 2, display.height() - i * 2, color % 2);
  273. display.display();
  274. color++;
  275. }
  276. }
  277.  
  278. void testdrawtriangle(void) {
  279. for (int16_t i = 0; i < min(display.width(), display.height()) / 2; i += 5) {
  280. display.drawTriangle(display.width() / 2, display.height() / 2 - i,
  281. display.width() / 2 - i, display.height() / 2 + i,
  282. display.width() / 2 + i, display.height() / 2 + i, BLACK);
  283. display.display();
  284. }
  285. }
  286.  
  287. void testfilltriangle(void) {
  288. uint8_t color = BLACK;
  289. for (int16_t i = min(display.width(), display.height()) / 2; i > 0; i -= 5) {
  290. display.fillTriangle(display.width() / 2, display.height() / 2 - i,
  291. display.width() / 2 - i, display.height() / 2 + i,
  292. display.width() / 2 + i, display.height() / 2 + i, color);
  293. if (color == WHITE) color = BLACK;
  294. else color = WHITE;
  295. display.display();
  296. }
  297. }
  298.  
  299. void testdrawroundrect(void) {
  300. for (int16_t i = 0; i < display.height() / 2 - 2; i += 2) {
  301. display.drawRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i, display.height() / 4, BLACK);
  302. display.display();
  303. }
  304. }
  305.  
  306. void testfillroundrect(void) {
  307. uint8_t color = BLACK;
  308. for (int16_t i = 0; i < display.height() / 2 - 2; i += 2) {
  309. display.fillRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i, display.height() / 4, color);
  310. if (color == WHITE) color = BLACK;
  311. else color = WHITE;
  312. display.display();
  313. }
  314. }
  315.  
  316. void testdrawrect(void) {
  317. for (int16_t i = 0; i < display.height() / 2; i += 2) {
  318. display.drawRect(i, i, display.width() - 2 * i, display.height() - 2 * i, BLACK);
  319. display.display();
  320. }
  321. }
  322.  
  323. void testdrawline() {
  324. for (int16_t i = 0; i < display.width(); i += 4) {
  325. display.drawLine(0, 0, i, display.height() - 1, BLACK);
  326. display.display();
  327. }
  328. for (int16_t i = 0; i < display.height(); i += 4) {
  329. display.drawLine(0, 0, display.width() - 1, i, BLACK);
  330. display.display();
  331. }
  332. delay(250);
  333.  
  334. display.clearDisplay();
  335. for (int16_t i = 0; i < display.width(); i += 4) {
  336. display.drawLine(0, display.height() - 1, i, 0, BLACK);
  337. display.display();
  338. }
  339. for (int8_t i = display.height() - 1; i >= 0; i -= 4) {
  340. display.drawLine(0, display.height() - 1, display.width() - 1, i, BLACK);
  341. display.display();
  342. }
  343. delay(250);
  344.  
  345. display.clearDisplay();
  346. for (int16_t i = display.width() - 1; i >= 0; i -= 4) {
  347. display.drawLine(display.width() - 1, display.height() - 1, i, 0, BLACK);
  348. display.display();
  349. }
  350. for (int16_t i = display.height() - 1; i >= 0; i -= 4) {
  351. display.drawLine(display.width() - 1, display.height() - 1, 0, i, BLACK);
  352. display.display();
  353. }
  354. delay(250);
  355.  
  356. display.clearDisplay();
  357. for (int16_t i = 0; i < display.height(); i += 4) {
  358. display.drawLine(display.width() - 1, 0, 0, i, BLACK);
  359. display.display();
  360. }
  361. for (int16_t i = 0; i < display.width(); i += 4) {
  362. display.drawLine(display.width() - 1, 0, i, display.height() - 1, BLACK);
  363. display.display();
  364. }
  365. delay(250);
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement