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.

SpeedTest.ino 8.2KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SpeedTest written by Peter Loveday
  2. //
  3. // http://forum.pjrc.com/threads/15576-Teensy3-ST7735-Library?p=21355&viewfull=1#post21355
  4. // This Teensy3 native optimized version requires specific pins
  5. //
  6. #define TFT_SCLK 13 // SCLK can also use pin 14
  7. #define TFT_MOSI 11 // MOSI can also use pin 7
  8. #define TFT_CS 10 // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
  9. #define TFT_DC 9 // but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
  10. #define TFT_RST 8 // RST can use any pin
  11. #define SD_CS 4 // CS for SD card, can use any pin
  12. #include <Adafruit_GFX.h>
  13. #include <ST7735_t3.h>
  14. #include <SPI.h>
  15. ST7735_t3 disp = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  16. #define RGB(r,g,b) (b<<11|g<<6|r)
  17. float pi = 3.1415926f;
  18. void testlines(uint16_t color)
  19. {
  20. disp.fillScreen(ST7735_BLACK);
  21. for (int16_t x=0; x < disp.width(); x+=6)
  22. disp.drawLine(0, 0, x, disp.height()-1, color);
  23. for (int16_t y=0; y < disp.height(); y+=6)
  24. disp.drawLine(0, 0, disp.width()-1, y, color);
  25. for (int16_t x=0; x < disp.width(); x+=6)
  26. disp.drawLine(disp.width()-1, 0, x, disp.height()-1, color);
  27. for (int16_t y=0; y < disp.height(); y+=6)
  28. disp.drawLine(disp.width()-1, 0, 0, y, color);
  29. for (int16_t x=0; x < disp.width(); x+=6)
  30. disp.drawLine(0, disp.height()-1, x, 0, color);
  31. for (int16_t y=0; y < disp.height(); y+=6)
  32. disp.drawLine(0, disp.height()-1, disp.width()-1, y, color);
  33. for (int16_t x=0; x < disp.width(); x+=6)
  34. disp.drawLine(disp.width()-1, disp.height()-1, x, 0, color);
  35. for (int16_t y=0; y < disp.height(); y+=6)
  36. disp.drawLine(disp.width()-1, disp.height()-1, 0, y, color);
  37. }
  38. void testdrawtext(const char *text, uint16_t color, uint16_t bgcolor)
  39. {
  40. disp.setCursor(0, 0);
  41. disp.setTextColor(color, bgcolor);
  42. disp.setTextWrap(true);
  43. disp.print(text);
  44. }
  45. void testfastlines(uint16_t color1, uint16_t color2)
  46. {
  47. disp.fillScreen(ST7735_BLACK);
  48. for (int16_t y=0; y < disp.height(); y+=5)
  49. disp.drawFastHLine(0, y, disp.width(), color1);
  50. for (int16_t x=0; x < disp.width(); x+=5)
  51. disp.drawFastVLine(x, 0, disp.height(), color2);
  52. }
  53. void testdrawrects(uint16_t color)
  54. {
  55. disp.fillScreen(ST7735_BLACK);
  56. for (int16_t x=0; x < disp.width(); x+=6)
  57. disp.drawRect(disp.width()/2 -x/2, disp.height()/2 -x/2 , x, x, color);
  58. }
  59. void testfillrects(uint16_t color1, uint16_t color2)
  60. {
  61. disp.fillScreen(ST7735_BLACK);
  62. for (int16_t x=disp.width()-1; x > 6; x-=6) {
  63. disp.fillRect(disp.width()/2 -x/2, disp.height()/2 -x/2 , x, x, color1);
  64. disp.drawRect(disp.width()/2 -x/2, disp.height()/2 -x/2 , x, x, color2);
  65. }
  66. }
  67. void testfillcircles(uint8_t radius, uint16_t color)
  68. {
  69. for (int16_t x=radius; x < disp.width(); x+=radius*2)
  70. for (int16_t y=radius; y < disp.height(); y+=radius*2)
  71. disp.fillCircle(x, y, radius, color);
  72. }
  73. void testdrawcircles(uint8_t radius, uint16_t color)
  74. {
  75. for (int16_t x=0; x < disp.width()+radius; x+=radius*2)
  76. for (int16_t y=0; y < disp.height()+radius; y+=radius*2)
  77. disp.drawCircle(x, y, radius, color);
  78. }
  79. void testtriangles()
  80. {
  81. disp.fillScreen(ST7735_BLACK);
  82. int color = 0xF800;
  83. int t;
  84. int w = 63;
  85. int x = 159;
  86. int y = 0;
  87. int z = 127;
  88. for (t = 0 ; t <= 15; t+=1) {
  89. disp.drawTriangle(w, y, y, x, z, x, color);
  90. x-=4;
  91. y+=4;
  92. z-=4;
  93. color+=100;
  94. }
  95. }
  96. void testroundrects()
  97. {
  98. disp.fillScreen(ST7735_BLACK);
  99. int color = 100;
  100. int i;
  101. int t;
  102. for(t = 0 ; t <= 4; t+=1) {
  103. int x = 0;
  104. int y = 0;
  105. int w = 127;
  106. int h = 159;
  107. for(i = 0 ; i <= 24; i+=1) {
  108. disp.drawRoundRect(x, y, w, h, 5, color);
  109. x+=2;
  110. y+=3;
  111. w-=4;
  112. h-=6;
  113. color+=1100;
  114. }
  115. color+=100;
  116. }
  117. }
  118. #define SETCOLOR(c) disp.setTextColor(c, bg ? ST7735_BLACK : c);
  119. void tftPrintTest(bool bg)
  120. {
  121. disp.setTextWrap(false);
  122. disp.fillScreen(ST7735_BLACK);
  123. disp.setCursor(0, 30);
  124. SETCOLOR(ST7735_RED);
  125. disp.setTextSize(1);
  126. disp.println("Hello World!");
  127. SETCOLOR(ST7735_YELLOW);
  128. disp.setTextSize(2);
  129. disp.println("Hello World!");
  130. SETCOLOR(ST7735_GREEN);
  131. disp.setTextSize(3);
  132. disp.println("Hello World!");
  133. SETCOLOR(ST7735_BLUE);
  134. disp.setTextSize(4);
  135. disp.print(1234.567);
  136. disp.setCursor(0, 0);
  137. disp.fillScreen(ST7735_BLACK);
  138. SETCOLOR(ST7735_WHITE);
  139. disp.setTextSize(0);
  140. disp.println("Hello World!");
  141. disp.setTextSize(1);
  142. SETCOLOR(ST7735_GREEN);
  143. disp.print(pi, 6);
  144. disp.println(" Want pi?");
  145. disp.println(" ");
  146. disp.print(8675309, HEX);
  147. disp.println(" Print HEX!");
  148. disp.println(" ");
  149. SETCOLOR(ST7735_WHITE);
  150. disp.println("Sketch has been");
  151. disp.println("running for: ");
  152. SETCOLOR(ST7735_MAGENTA);
  153. disp.print(millis() / 1000);
  154. SETCOLOR(ST7735_WHITE);
  155. disp.print(" seconds.");
  156. }
  157. void mediabuttons()
  158. {
  159. // play
  160. disp.fillScreen(ST7735_BLACK);
  161. disp.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
  162. disp.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
  163. // pause
  164. disp.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
  165. disp.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
  166. disp.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
  167. // play color
  168. disp.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
  169. // pause color
  170. disp.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
  171. disp.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
  172. // play color
  173. disp.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
  174. }
  175. int t[20];
  176. int n = 0;
  177. void setup()
  178. {
  179. pinMode(SD_CS, INPUT_PULLUP); // keep SD CS high when not using SD card
  180. // Use this initializer if you're using a 1.8" TFT
  181. disp.initR(INITR_BLACKTAB);
  182. // Use this initializer (uncomment) if you're using a 1.44" TFT
  183. //disp.initR(INITR_144GREENTAB);
  184. disp.setRotation(0);
  185. disp.setTextWrap(true);
  186. disp.setTextColor(RGB(31,31,31), RGB(0,0,0));
  187. disp.setCursor(0, 0);
  188. disp.fillScreen(RGB(0,0,0));
  189. t[n++] = millis();
  190. disp.fillScreen(ST7735_BLACK);
  191. t[n++] = millis();
  192. 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, ST7735_WHITE);
  193. t[n++] = millis();
  194. tftPrintTest(false);
  195. t[n++] = millis();
  196. 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, ST7735_BLACK);
  197. t[n++] = millis();
  198. tftPrintTest(true);
  199. t[n++] = millis();
  200. testlines(ST7735_YELLOW);
  201. t[n++] = millis();
  202. testfastlines(ST7735_RED, ST7735_BLUE);
  203. t[n++] = millis();
  204. testdrawrects(ST7735_GREEN);
  205. t[n++] = millis();
  206. testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
  207. t[n++] = millis();
  208. disp.fillScreen(ST7735_BLACK);
  209. t[n++] = millis();
  210. testfillcircles(10, ST7735_BLUE);
  211. t[n++] = millis();
  212. testdrawcircles(10, ST7735_WHITE);
  213. t[n++] = millis();
  214. testroundrects();
  215. t[n++] = millis();
  216. testtriangles();
  217. t[n++] = millis();
  218. mediabuttons();
  219. t[n++] = millis();
  220. disp.fillScreen(RGB(0,0,0));
  221. }
  222. // These are the results for the adafruit library, so we can report results as Nx
  223. uint16_t normalize[] =
  224. {
  225. 129,
  226. 216,
  227. 348,
  228. 955,
  229. 630,
  230. 1658,
  231. 185,
  232. 168,
  233. 955,
  234. 129,
  235. 195,
  236. 163,
  237. 604,
  238. 362,
  239. 253,
  240. 6950
  241. };
  242. void loop()
  243. {
  244. static float fps = 0.0;
  245. static int h = 0;
  246. uint8_t r,g,b;
  247. if (h < 32) {
  248. r = 31; g = h; b = 0;
  249. } else if (h < 64) {
  250. r = 63-h; g = 31; b = 0;
  251. } else if (h < 96) {
  252. r = 0; g = 31; b = h-64;
  253. } else if (h < 128) {
  254. r = 0; g = 127-h; b = 31;
  255. } else if (h < 160) {
  256. r = h-128; g = 0; b = 31;
  257. } else if (h < 192) {
  258. r = 31; g = 0; b = 191-h;
  259. } else {
  260. r = 31; g = 0; b = 0;
  261. h = 0;
  262. }
  263. h++;
  264. int start = micros();
  265. disp.fillRect(120, 0, 8, 160, RGB(r,g,b));
  266. disp.setTextWrap(false);
  267. disp.setTextColor(RGB(31,31,31), RGB(0,0,0));
  268. disp.setCursor(0, 0);
  269. for (int i=1; i < n; i++) {
  270. disp.print(i < 10 ? "Test " : "Test ");
  271. disp.print(i, DEC);
  272. disp.print(": ");
  273. disp.println(float(normalize[i-1])/float(t[i]-t[i-1]), 2);
  274. }
  275. disp.println("");
  276. disp.print("Total : ");
  277. disp.println(float(normalize[n-1])/float(t[n-1]-t[0]), 2);
  278. disp.println("");
  279. disp.print("FPS : ");
  280. disp.println(fps, 2);
  281. int end = micros();
  282. fps = 1000000.0f/float(end-start);
  283. }