ST7789_demo.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-05-29 Meco Man port to RTduino
  9. */
  10. /**************************************************************************
  11. This is a library for several Adafruit displays based on ST77* drivers.
  12. Works with the Adafruit 1.8" TFT Breakout w/SD card
  13. ----> http://www.adafruit.com/products/358
  14. The 1.8" TFT shield
  15. ----> https://www.adafruit.com/product/802
  16. The 1.44" TFT breakout
  17. ----> https://www.adafruit.com/product/2088
  18. as well as Adafruit raw 1.8" TFT display
  19. ----> http://www.adafruit.com/products/618
  20. Check out the links above for our tutorials and wiring diagrams.
  21. These displays use SPI to communicate, 4 or 5 pins are required to
  22. interface (RST is optional).
  23. Adafruit invests time and resources providing this open source code,
  24. please support Adafruit and open-source hardware by purchasing
  25. products from Adafruit!
  26. Written by Limor Fried/Ladyada for Adafruit Industries.
  27. MIT license, all text above must be included in any redistribution
  28. **************************************************************************/
  29. #include <RTduino.h>
  30. #include <Adafruit_GFX.h> // Core graphics library
  31. #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
  32. #include <SPI.h> // Arduino SPI library
  33. // ST7789 TFT module connections
  34. #define TFT_CS D36 // define chip select pin
  35. #define TFT_DC D38 // define data/command pin
  36. #define TFT_RST D37 // define reset pin, or set to -1 and connect to Arduino RESET pin
  37. #define TFT_PWR D39 // LCD screen power with PWM control
  38. // Initialize Adafruit ST7789 TFT library
  39. static Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
  40. void testlines(uint16_t color)
  41. {
  42. tft.fillScreen(ST77XX_BLACK);
  43. for (int16_t x=0; x < tft.width(); x+=6)
  44. {
  45. tft.drawLine(0, 0, x, tft.height()-1, color);
  46. }
  47. for (int16_t y=0; y < tft.height(); y+=6)
  48. {
  49. tft.drawLine(0, 0, tft.width()-1, y, color);
  50. }
  51. tft.fillScreen(ST77XX_BLACK);
  52. for (int16_t x=0; x < tft.width(); x+=6)
  53. {
  54. tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  55. }
  56. for (int16_t y=0; y < tft.height(); y+=6)
  57. {
  58. tft.drawLine(tft.width()-1, 0, 0, y, color);
  59. }
  60. tft.fillScreen(ST77XX_BLACK);
  61. for (int16_t x=0; x < tft.width(); x+=6)
  62. {
  63. tft.drawLine(0, tft.height()-1, x, 0, color);
  64. }
  65. for (int16_t y=0; y < tft.height(); y+=6)
  66. {
  67. tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  68. }
  69. tft.fillScreen(ST77XX_BLACK);
  70. for (int16_t x=0; x < tft.width(); x+=6)
  71. {
  72. tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  73. }
  74. for (int16_t y=0; y < tft.height(); y+=6)
  75. {
  76. tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  77. }
  78. }
  79. void testdrawtext(char *text, uint16_t color)
  80. {
  81. tft.setCursor(0, 0);
  82. tft.setTextSize(2);
  83. tft.setTextColor(color);
  84. tft.setTextWrap(true);
  85. tft.print(text);
  86. }
  87. void testfastlines(uint16_t color1, uint16_t color2)
  88. {
  89. tft.fillScreen(ST77XX_BLACK);
  90. for (int16_t y=0; y < tft.height(); y+=5)
  91. {
  92. tft.drawFastHLine(0, y, tft.width(), color1);
  93. }
  94. for (int16_t x=0; x < tft.width(); x+=5)
  95. {
  96. tft.drawFastVLine(x, 0, tft.height(), color2);
  97. }
  98. }
  99. void testdrawrects(uint16_t color)
  100. {
  101. tft.fillScreen(ST77XX_BLACK);
  102. for (int16_t x=0; x < tft.width(); x+=6)
  103. {
  104. tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  105. }
  106. }
  107. void testfillrects(uint16_t color1, uint16_t color2)
  108. {
  109. tft.fillScreen(ST77XX_BLACK);
  110. for (int16_t x=tft.width()-1; x > 6; x-=6)
  111. {
  112. tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  113. tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  114. }
  115. }
  116. void testfillcircles(uint8_t radius, uint16_t color)
  117. {
  118. for (int16_t x=radius; x < tft.width(); x+=radius*2)
  119. {
  120. for (int16_t y=radius; y < tft.height(); y+=radius*2)
  121. {
  122. tft.fillCircle(x, y, radius, color);
  123. }
  124. }
  125. }
  126. void testdrawcircles(uint8_t radius, uint16_t color)
  127. {
  128. for (int16_t x=0; x < tft.width()+radius; x+=radius*2)
  129. {
  130. for (int16_t y=0; y < tft.height()+radius; y+=radius*2)
  131. {
  132. tft.drawCircle(x, y, radius, color);
  133. }
  134. }
  135. }
  136. void testtriangles(void)
  137. {
  138. tft.fillScreen(ST77XX_BLACK);
  139. int color = 0xF800;
  140. int t;
  141. int w = tft.width()/2;
  142. int x = tft.height()-1;
  143. int y = 0;
  144. int z = tft.width();
  145. for(t = 0 ; t <= 15; t++)
  146. {
  147. tft.drawTriangle(w, y, y, x, z, x, color);
  148. x-=4;
  149. y+=4;
  150. z-=4;
  151. color+=100;
  152. }
  153. }
  154. void testroundrects(void)
  155. {
  156. tft.fillScreen(ST77XX_BLACK);
  157. int color = 100;
  158. int i;
  159. int t;
  160. for(t = 0 ; t <= 4; t+=1)
  161. {
  162. int x = 0;
  163. int y = 0;
  164. int w = tft.width()-2;
  165. int h = tft.height()-2;
  166. for(i = 0 ; i <= 16; i+=1)
  167. {
  168. tft.drawRoundRect(x, y, w, h, 5, color);
  169. x+=2;
  170. y+=3;
  171. w-=4;
  172. h-=6;
  173. color+=1100;
  174. }
  175. color+=100;
  176. }
  177. }
  178. void tftPrintTest(void)
  179. {
  180. tft.setTextWrap(false);
  181. tft.fillScreen(ST77XX_BLACK);
  182. tft.setCursor(0, 30);
  183. tft.setTextColor(ST77XX_RED);
  184. tft.setTextSize(1);
  185. tft.println("Hello World!");
  186. tft.setTextColor(ST77XX_YELLOW);
  187. tft.setTextSize(2);
  188. tft.println("Hello RT-Thread!");
  189. tft.setTextColor(ST77XX_GREEN);
  190. tft.setTextSize(3);
  191. tft.println("Hello RTduino!");
  192. tft.setTextColor(ST77XX_BLUE);
  193. tft.setTextSize(4);
  194. tft.print(1234.567);
  195. delay(1500);
  196. tft.setCursor(0, 0);
  197. tft.fillScreen(ST77XX_BLACK);
  198. tft.setTextColor(ST77XX_WHITE);
  199. tft.setTextSize(0);
  200. tft.println("Hello World!");
  201. tft.setTextSize(1);
  202. tft.setTextColor(ST77XX_GREEN);
  203. tft.print(PI, 6);
  204. tft.println(" Want pi?");
  205. tft.println(" ");
  206. tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  207. tft.println(" Print HEX!");
  208. tft.println(" ");
  209. tft.setTextColor(ST77XX_WHITE);
  210. tft.println("Sketch has been");
  211. tft.println("running for: ");
  212. tft.setTextColor(ST77XX_MAGENTA);
  213. tft.print(millis() / 1000);
  214. tft.setTextColor(ST77XX_WHITE);
  215. tft.print(" seconds.");
  216. }
  217. void mediabuttons(void)
  218. {
  219. // play
  220. tft.fillScreen(ST77XX_BLACK);
  221. tft.fillRoundRect(25, 10, 78, 60, 8, ST77XX_WHITE);
  222. tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_RED);
  223. delay(500);
  224. // pause
  225. tft.fillRoundRect(25, 90, 78, 60, 8, ST77XX_WHITE);
  226. tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_GREEN);
  227. tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_GREEN);
  228. delay(500);
  229. // play color
  230. tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_BLUE);
  231. delay(50);
  232. // pause color
  233. tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_RED);
  234. tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_RED);
  235. // play color
  236. tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_GREEN);
  237. }
  238. static void st7789_setup(void)
  239. {
  240. Serial.begin();
  241. Serial.println(F("Hello! ST77xx TFT Test"));
  242. // if the display has CS pin try with SPI_MODE0
  243. tft.init(240, 240, SPI_MODE0); // Init ST7789 display 240x240 pixel
  244. analogWrite(TFT_PWR, 255); // set the PWM background LED as max
  245. // if the screen is flipped, remove this command
  246. tft.setRotation(2);
  247. Serial.println(F("Initialized"));
  248. uint16_t time = millis();
  249. tft.fillScreen(ST77XX_BLACK);
  250. time = millis() - time;
  251. Serial.print("Fill the screen spend: ");
  252. Serial.print(time, DEC);
  253. Serial.println(" ms");
  254. time = millis();
  255. testdrawtext((char *)"RTduino is an open source project which is compatible with Arduino APIs so that RT-Thread beginners can easily get start to use RT-Thread through Arduino APIs, which significantly reduces the difficulty of learning RT-Thread.", ST77XX_WHITE);
  256. time = millis() - time;
  257. Serial.print("write text spend: ");
  258. Serial.print(time, DEC);
  259. Serial.println(" ms");
  260. delay(1500);
  261. // tft print function!
  262. tftPrintTest();
  263. delay(4000);
  264. // a single pixel
  265. tft.drawPixel(tft.width()/2, tft.height()/2, ST77XX_GREEN);
  266. delay(500);
  267. // line draw test
  268. time = millis();
  269. testlines(ST77XX_YELLOW);
  270. time = millis() - time;
  271. Serial.print("testlines spend: ");
  272. Serial.print(time, DEC);
  273. Serial.println(" ms");
  274. delay(500);
  275. // optimized lines
  276. testfastlines(ST77XX_RED, ST77XX_BLUE);
  277. delay(500);
  278. testdrawrects(ST77XX_GREEN);
  279. delay(500);
  280. testfillrects(ST77XX_YELLOW, ST77XX_MAGENTA);
  281. delay(500);
  282. tft.fillScreen(ST77XX_BLACK);
  283. testfillcircles(10, ST77XX_BLUE);
  284. testdrawcircles(10, ST77XX_WHITE);
  285. delay(500);
  286. testroundrects();
  287. delay(500);
  288. testtriangles();
  289. delay(500);
  290. mediabuttons();
  291. delay(500);
  292. Serial.println("done");
  293. delay(1000);
  294. }
  295. static void st7789_loop(void)
  296. {
  297. tft.invertDisplay(true);
  298. delay(500);
  299. tft.invertDisplay(false);
  300. delay(500);
  301. }
  302. RTDUINO_SKETCH_LOADER("ST7789", st7789_setup, st7789_loop);