PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /***************************************************
  2. This is a library for several Adafruit displays based on ST77* drivers.
  3. Works with the Adafruit 1.8" TFT Breakout w/SD card
  4. ----> http://www.adafruit.com/products/358
  5. The 1.8" TFT shield
  6. ----> https://www.adafruit.com/product/802
  7. The 1.44" TFT breakout
  8. ----> https://www.adafruit.com/product/2088
  9. The 1.54" TFT breakout
  10. ----> https://www.adafruit.com/product/3787
  11. The 2.0" TFT breakout
  12. ----> https://www.adafruit.com/product/4311
  13. as well as Adafruit raw 1.8" TFT display
  14. ----> http://www.adafruit.com/products/618
  15. Check out the links above for our tutorials and wiring diagrams
  16. These displays use SPI to communicate, 4 or 5 pins are required to
  17. interface (RST is optional)
  18. Adafruit invests time and resources providing this open source code,
  19. please support Adafruit and open-source hardware by purchasing
  20. products from Adafruit!
  21. Written by Limor Fried/Ladyada for Adafruit Industries.
  22. MIT license, all text above must be included in any redistribution
  23. ****************************************************/
  24. // This Teensy3 and 4 native optimized and extended version
  25. // requires specific pins.
  26. // If you use the short version of the constructor and the DC
  27. // pin is hardware CS pin, then it will be slower.
  28. #define TFT_MISO 12
  29. #define TFT_MOSI 11 //a12
  30. #define TFT_SCK 13 //a13
  31. #define TFT_DC 9
  32. #define TFT_CS 10
  33. #define TFT_RST 8
  34. // Note the above pins are for the SPI object. For those Teensy boards which have
  35. // more than one SPI object, such as T3.5, T3.6, T4 which have at SPI1 and SPI2
  36. // LC with SPI1, look at the cards that come with the teensy or the web page
  37. // https://www.pjrc.com/teensy/pinout.html to select the appropriate IO pins.
  38. //#include <Adafruit_GFX.h> // Core graphics library
  39. #include <ST7735_t3.h> // Hardware-specific library
  40. #include <ST7789_t3.h> // Hardware-specific library
  41. #include <SPI.h>
  42. // Option 1: use any pins but a little slower
  43. // Note: code will detect if specified pins are the hardware SPI pins
  44. // and will use hardware SPI if appropriate
  45. // For 1.44" and 1.8" TFT with ST7735 use
  46. ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST);
  47. // For 1.54" or other TFT with ST7789, This has worked with some ST7789
  48. // displays without CS pins, for those you can pass in -1 or 0xff for CS
  49. // More notes by the tft.init call
  50. //ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  51. // Option 2: must use the hardware SPI pins
  52. // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
  53. // an output. This is much faster - also required if you want
  54. // to use the microSD card (see the image drawing example)
  55. // For 1.44" and 1.8" TFT with ST7735 use
  56. //ST7735_t3 tft = ST7735_t3(cs, dc, rst);
  57. // For 1.54" TFT with ST7789
  58. //ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_RST);
  59. float p = 3.1415926;
  60. void setup(void) {
  61. //pinMode(SD_CS, INPUT_PULLUP); // don't touch the SD card
  62. Serial.begin(9600);
  63. Serial.print("hello!");
  64. // Use this initializer if you're using a 1.8" TFT 128x160 displays
  65. //tft.initR(INITR_BLACKTAB);
  66. // Or use this initializer (uncomment) if you're using a 1.44" TFT (128x128)
  67. //tft.initR(INITR_144GREENTAB);
  68. // Or use this initializer (uncomment) if you're using a .96" TFT(160x80)
  69. //tft.initR(INITR_MINI160x80);
  70. // Or use this initializer (uncomment) for Some 1.44" displays use different memory offsets
  71. // Try it if yours is not working properly
  72. // May need to tweek the offsets
  73. //tft.setRowColStart(32,0);
  74. // Or use this initializer (uncomment) if you're using a 1.54" 240x240 TFT
  75. //tft.init(240, 240); // initialize a ST7789 chip, 240x240 pixels
  76. // OR use this initializer (uncomment) if using a 2.0" 320x240 TFT:
  77. tft.init(240, 320); // Init ST7789 320x240
  78. // OR use this initializer (uncomment) if using a 240x240 clone
  79. // that does not have a CS pin2.0" 320x240 TFT:
  80. //tft.init(240, 240, SPI_MODE2); // Init ST7789 240x240 no CS
  81. Serial.println("init");
  82. uint16_t time = millis();
  83. tft.fillScreen(ST7735_BLACK);
  84. time = millis() - time;
  85. Serial.println(time, DEC);
  86. delay(500);
  87. // large block of text
  88. tft.fillScreen(ST7735_BLACK);
  89. testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", ST7735_WHITE);
  90. delay(1000);
  91. // tft print function!
  92. tftPrintTest();
  93. delay(4000);
  94. // a single pixel
  95. tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
  96. delay(500);
  97. // line draw test
  98. testlines(ST7735_YELLOW);
  99. delay(1000);
  100. // optimized lines
  101. testfastlines(ST7735_RED, ST7735_BLUE);
  102. delay(1000);
  103. testdrawrects(ST7735_GREEN);
  104. delay(1000);
  105. testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
  106. delay(1000);
  107. tft.fillScreen(ST7735_BLACK);
  108. testfillcircles(10, ST7735_BLUE);
  109. testdrawcircles(10, ST7735_WHITE);
  110. delay(500);
  111. testroundrects();
  112. delay(1000);
  113. testtriangles();
  114. delay(1000);
  115. mediabuttons();
  116. delay(500);
  117. Serial.println("done");
  118. delay(1000);
  119. }
  120. void loop() {
  121. tft.invertDisplay(true);
  122. delay(500);
  123. tft.invertDisplay(false);
  124. delay(500);
  125. }
  126. void testlines(uint16_t color) {
  127. tft.fillScreen(ST7735_BLACK);
  128. for (int16_t x=0; x < tft.width(); x+=6) {
  129. tft.drawLine(0, 0, x, tft.height()-1, color);
  130. delay(200);
  131. }
  132. for (int16_t y=0; y < tft.height(); y+=6) {
  133. tft.drawLine(0, 0, tft.width()-1, y, color);
  134. }
  135. tft.fillScreen(ST7735_BLACK);
  136. for (int16_t x=0; x < tft.width(); x+=6) {
  137. tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  138. }
  139. for (int16_t y=0; y < tft.height(); y+=6) {
  140. tft.drawLine(tft.width()-1, 0, 0, y, color);
  141. }
  142. tft.fillScreen(ST7735_BLACK);
  143. for (int16_t x=0; x < tft.width(); x+=6) {
  144. tft.drawLine(0, tft.height()-1, x, 0, color);
  145. }
  146. for (int16_t y=0; y < tft.height(); y+=6) {
  147. tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  148. }
  149. tft.fillScreen(ST7735_BLACK);
  150. for (int16_t x=0; x < tft.width(); x+=6) {
  151. tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  152. }
  153. for (int16_t y=0; y < tft.height(); y+=6) {
  154. tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  155. }
  156. }
  157. void testdrawtext(const char *text, uint16_t color) {
  158. tft.setCursor(0, 0);
  159. tft.setTextColor(color);
  160. tft.setTextWrap(true);
  161. tft.print(text);
  162. }
  163. void testfastlines(uint16_t color1, uint16_t color2) {
  164. tft.fillScreen(ST7735_BLACK);
  165. for (int16_t y=0; y < tft.height(); y+=5) {
  166. tft.drawFastHLine(0, y, tft.width(), color1);
  167. }
  168. for (int16_t x=0; x < tft.width(); x+=5) {
  169. tft.drawFastVLine(x, 0, tft.height(), color2);
  170. }
  171. }
  172. void testdrawrects(uint16_t color) {
  173. tft.fillScreen(ST7735_BLACK);
  174. for (int16_t x=0; x < tft.width(); x+=6) {
  175. tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  176. }
  177. }
  178. void testfillrects(uint16_t color1, uint16_t color2) {
  179. tft.fillScreen(ST7735_BLACK);
  180. for (int16_t x=tft.width()-1; x > 6; x-=6) {
  181. tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  182. tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  183. }
  184. }
  185. void testfillcircles(uint8_t radius, uint16_t color) {
  186. for (int16_t x=radius; x < tft.width(); x+=radius*2) {
  187. for (int16_t y=radius; y < tft.height(); y+=radius*2) {
  188. tft.fillCircle(x, y, radius, color);
  189. }
  190. }
  191. }
  192. void testdrawcircles(uint8_t radius, uint16_t color) {
  193. for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
  194. for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
  195. tft.drawCircle(x, y, radius, color);
  196. }
  197. }
  198. }
  199. void testtriangles() {
  200. tft.fillScreen(ST7735_BLACK);
  201. int color = 0xF800;
  202. int t;
  203. int w = tft.width()/2;
  204. int x = tft.height()-1;
  205. int y = 0;
  206. int z = tft.width();
  207. for(t = 0 ; t <= 15; t+=1) {
  208. tft.drawTriangle(w, y, y, x, z, x, color);
  209. x-=4;
  210. y+=4;
  211. z-=4;
  212. color+=100;
  213. }
  214. }
  215. void testroundrects() {
  216. tft.fillScreen(ST7735_BLACK);
  217. int color = 100;
  218. int i;
  219. int t;
  220. for(t = 0 ; t <= 4; t+=1) {
  221. int x = 0;
  222. int y = 0;
  223. int w = tft.width()-2;
  224. int h = tft.height()-2;
  225. for(i = 0 ; i <= 16; i+=1) {
  226. tft.drawRoundRect(x, y, w, h, 5, color);
  227. x+=2;
  228. y+=3;
  229. w-=4;
  230. h-=6;
  231. color+=1100;
  232. }
  233. color+=100;
  234. }
  235. }
  236. void tftPrintTest() {
  237. tft.setTextWrap(false);
  238. tft.fillScreen(ST7735_BLACK);
  239. tft.setCursor(0, 30);
  240. tft.setTextColor(ST7735_RED);
  241. tft.setTextSize(1);
  242. tft.println("Hello World!");
  243. tft.setTextColor(ST7735_YELLOW);
  244. tft.setTextSize(2);
  245. tft.println("Hello World!");
  246. tft.setTextColor(ST7735_GREEN);
  247. tft.setTextSize(3);
  248. tft.println("Hello World!");
  249. tft.setTextColor(ST7735_BLUE);
  250. tft.setTextSize(4);
  251. tft.print(1234.567);
  252. delay(1500);
  253. tft.setCursor(0, 0);
  254. tft.fillScreen(ST7735_BLACK);
  255. tft.setTextColor(ST7735_WHITE);
  256. tft.setTextSize(0);
  257. tft.println("Hello World!");
  258. tft.setTextSize(1);
  259. tft.setTextColor(ST7735_GREEN);
  260. tft.print(p, 6);
  261. tft.println(" Want pi?");
  262. tft.println(" ");
  263. tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  264. tft.println(" Print HEX!");
  265. tft.println(" ");
  266. tft.setTextColor(ST7735_WHITE);
  267. tft.println("Sketch has been");
  268. tft.println("running for: ");
  269. tft.setTextColor(ST7735_MAGENTA);
  270. tft.print(millis() / 1000);
  271. tft.setTextColor(ST7735_WHITE);
  272. tft.print(" seconds.");
  273. }
  274. void mediabuttons() {
  275. // play
  276. tft.fillScreen(ST7735_BLACK);
  277. tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
  278. tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
  279. delay(500);
  280. // pause
  281. tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
  282. tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
  283. tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
  284. delay(500);
  285. // play color
  286. tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
  287. delay(50);
  288. // pause color
  289. tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
  290. tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
  291. // play color
  292. tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
  293. }