mock_ili9341.ino 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /***************************************************
  2. This is our GFX example for the Adafruit ILI9341 Breakout and Shield
  3. ----> http://www.adafruit.com/products/1651
  4. Check out the links above for our tutorials and wiring diagrams
  5. These displays use SPI to communicate, 4 or 5 pins are required to
  6. interface (RST is optional)
  7. Adafruit invests time and resources providing this open source code,
  8. please support Adafruit and open-source hardware by purchasing
  9. products from Adafruit!
  10. Written by Limor Fried/Ladyada for Adafruit Industries.
  11. MIT license, all text above must be included in any redistribution
  12. ****************************************************/
  13. #include "SPI.h"
  14. #include "Adafruit_GFX.h"
  15. #include "Adafruit_ILI9341.h"
  16. // For the Adafruit shield, these are the default.
  17. #define TFT_DC 9
  18. #define TFT_CS 10
  19. // Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
  20. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  21. // If using the breakout, change pins as desired
  22. //Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
  23. void setup() {
  24. Serial.begin(9600);
  25. Serial.println("ILI9341 Test!");
  26. tft.begin();
  27. // read diagnostics (optional but can help debug problems)
  28. uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  29. Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  30. x = tft.readcommand8(ILI9341_RDMADCTL);
  31. Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  32. x = tft.readcommand8(ILI9341_RDPIXFMT);
  33. Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  34. x = tft.readcommand8(ILI9341_RDIMGFMT);
  35. Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  36. x = tft.readcommand8(ILI9341_RDSELFDIAG);
  37. Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
  38. Serial.println(F("Benchmark Time (microseconds)"));
  39. delay(10);
  40. Serial.print(F("Screen fill "));
  41. Serial.println(testFillScreen());
  42. delay(500);
  43. Serial.print(F("Text "));
  44. Serial.println(testText());
  45. delay(3000);
  46. Serial.print(F("Lines "));
  47. Serial.println(testLines(ILI9341_CYAN));
  48. delay(500);
  49. Serial.print(F("Horiz/Vert Lines "));
  50. Serial.println(testFastLines(ILI9341_RED, ILI9341_BLUE));
  51. delay(500);
  52. Serial.print(F("Rectangles (outline) "));
  53. Serial.println(testRects(ILI9341_GREEN));
  54. delay(500);
  55. Serial.print(F("Rectangles (filled) "));
  56. Serial.println(testFilledRects(ILI9341_YELLOW, ILI9341_MAGENTA));
  57. delay(500);
  58. Serial.print(F("Circles (filled) "));
  59. Serial.println(testFilledCircles(10, ILI9341_MAGENTA));
  60. Serial.print(F("Circles (outline) "));
  61. Serial.println(testCircles(10, ILI9341_WHITE));
  62. delay(500);
  63. Serial.print(F("Triangles (outline) "));
  64. Serial.println(testTriangles());
  65. delay(500);
  66. Serial.print(F("Triangles (filled) "));
  67. Serial.println(testFilledTriangles());
  68. delay(500);
  69. Serial.print(F("Rounded rects (outline) "));
  70. Serial.println(testRoundRects());
  71. delay(500);
  72. Serial.print(F("Rounded rects (filled) "));
  73. Serial.println(testFilledRoundRects());
  74. delay(500);
  75. Serial.println(F("Done!"));
  76. }
  77. void loop(void) {
  78. for(uint8_t rotation=0; rotation<4; rotation++) {
  79. tft.setRotation(rotation);
  80. testText();
  81. delay(1000);
  82. }
  83. }
  84. unsigned long testFillScreen() {
  85. unsigned long start = micros();
  86. tft.fillScreen(ILI9341_BLACK);
  87. yield();
  88. tft.fillScreen(ILI9341_RED);
  89. yield();
  90. tft.fillScreen(ILI9341_GREEN);
  91. yield();
  92. tft.fillScreen(ILI9341_BLUE);
  93. yield();
  94. tft.fillScreen(ILI9341_BLACK);
  95. yield();
  96. return micros() - start;
  97. }
  98. unsigned long testText() {
  99. tft.fillScreen(ILI9341_BLACK);
  100. unsigned long start = micros();
  101. tft.setCursor(0, 0);
  102. tft.setTextColor(ILI9341_WHITE); tft.setTextSize(1);
  103. tft.println("Hello World!");
  104. tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
  105. tft.println(1234.56);
  106. tft.setTextColor(ILI9341_RED); tft.setTextSize(3);
  107. tft.println(0xDEADBEEF, HEX);
  108. tft.println();
  109. tft.setTextColor(ILI9341_GREEN);
  110. tft.setTextSize(5);
  111. tft.println("Groop");
  112. tft.setTextSize(2);
  113. tft.println("I implore thee,");
  114. tft.setTextSize(1);
  115. tft.println("my foonting turlingdromes.");
  116. tft.println("And hooptiously drangle me");
  117. tft.println("with crinkly bindlewurdles,");
  118. tft.println("Or I will rend thee");
  119. tft.println("in the gobberwarts");
  120. tft.println("with my blurglecruncheon,");
  121. tft.println("see if I don't!");
  122. return micros() - start;
  123. }
  124. unsigned long testLines(uint16_t color) {
  125. unsigned long start, t;
  126. int x1, y1, x2, y2,
  127. w = tft.width(),
  128. h = tft.height();
  129. tft.fillScreen(ILI9341_BLACK);
  130. yield();
  131. x1 = y1 = 0;
  132. y2 = h - 1;
  133. start = micros();
  134. for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  135. x2 = w - 1;
  136. for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  137. t = micros() - start; // fillScreen doesn't count against timing
  138. yield();
  139. tft.fillScreen(ILI9341_BLACK);
  140. yield();
  141. x1 = w - 1;
  142. y1 = 0;
  143. y2 = h - 1;
  144. start = micros();
  145. for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  146. x2 = 0;
  147. for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  148. t += micros() - start;
  149. yield();
  150. tft.fillScreen(ILI9341_BLACK);
  151. yield();
  152. x1 = 0;
  153. y1 = h - 1;
  154. y2 = 0;
  155. start = micros();
  156. for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  157. x2 = w - 1;
  158. for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  159. t += micros() - start;
  160. yield();
  161. tft.fillScreen(ILI9341_BLACK);
  162. yield();
  163. x1 = w - 1;
  164. y1 = h - 1;
  165. y2 = 0;
  166. start = micros();
  167. for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  168. x2 = 0;
  169. for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  170. yield();
  171. return micros() - start;
  172. }
  173. unsigned long testFastLines(uint16_t color1, uint16_t color2) {
  174. unsigned long start;
  175. int x, y, w = tft.width(), h = tft.height();
  176. tft.fillScreen(ILI9341_BLACK);
  177. start = micros();
  178. for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
  179. for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);
  180. return micros() - start;
  181. }
  182. unsigned long testRects(uint16_t color) {
  183. unsigned long start;
  184. int n, i, i2,
  185. cx = tft.width() / 2,
  186. cy = tft.height() / 2;
  187. tft.fillScreen(ILI9341_BLACK);
  188. n = min(tft.width(), tft.height());
  189. start = micros();
  190. for(i=2; i<n; i+=6) {
  191. i2 = i / 2;
  192. tft.drawRect(cx-i2, cy-i2, i, i, color);
  193. }
  194. return micros() - start;
  195. }
  196. unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
  197. unsigned long start, t = 0;
  198. int n, i, i2,
  199. cx = tft.width() / 2 - 1,
  200. cy = tft.height() / 2 - 1;
  201. tft.fillScreen(ILI9341_BLACK);
  202. n = min(tft.width(), tft.height());
  203. for(i=n; i>0; i-=6) {
  204. i2 = i / 2;
  205. start = micros();
  206. tft.fillRect(cx-i2, cy-i2, i, i, color1);
  207. t += micros() - start;
  208. // Outlines are not included in timing results
  209. tft.drawRect(cx-i2, cy-i2, i, i, color2);
  210. yield();
  211. }
  212. return t;
  213. }
  214. unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
  215. unsigned long start;
  216. int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
  217. tft.fillScreen(ILI9341_BLACK);
  218. start = micros();
  219. for(x=radius; x<w; x+=r2) {
  220. for(y=radius; y<h; y+=r2) {
  221. tft.fillCircle(x, y, radius, color);
  222. }
  223. }
  224. return micros() - start;
  225. }
  226. unsigned long testCircles(uint8_t radius, uint16_t color) {
  227. unsigned long start;
  228. int x, y, r2 = radius * 2,
  229. w = tft.width() + radius,
  230. h = tft.height() + radius;
  231. // Screen is not cleared for this one -- this is
  232. // intentional and does not affect the reported time.
  233. start = micros();
  234. for(x=0; x<w; x+=r2) {
  235. for(y=0; y<h; y+=r2) {
  236. tft.drawCircle(x, y, radius, color);
  237. }
  238. }
  239. return micros() - start;
  240. }
  241. unsigned long testTriangles() {
  242. unsigned long start;
  243. int n, i, cx = tft.width() / 2 - 1,
  244. cy = tft.height() / 2 - 1;
  245. tft.fillScreen(ILI9341_BLACK);
  246. n = min(cx, cy);
  247. start = micros();
  248. for(i=0; i<n; i+=5) {
  249. tft.drawTriangle(
  250. cx , cy - i, // peak
  251. cx - i, cy + i, // bottom left
  252. cx + i, cy + i, // bottom right
  253. tft.color565(i, i, i));
  254. }
  255. return micros() - start;
  256. }
  257. unsigned long testFilledTriangles() {
  258. unsigned long start, t = 0;
  259. int i, cx = tft.width() / 2 - 1,
  260. cy = tft.height() / 2 - 1;
  261. tft.fillScreen(ILI9341_BLACK);
  262. start = micros();
  263. for(i=min(cx,cy); i>10; i-=5) {
  264. start = micros();
  265. tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  266. tft.color565(0, i*10, i*10));
  267. t += micros() - start;
  268. tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  269. tft.color565(i*10, i*10, 0));
  270. yield();
  271. }
  272. return t;
  273. }
  274. unsigned long testRoundRects() {
  275. unsigned long start;
  276. int w, i, i2,
  277. cx = tft.width() / 2 - 1,
  278. cy = tft.height() / 2 - 1;
  279. tft.fillScreen(ILI9341_BLACK);
  280. w = min(tft.width(), tft.height());
  281. start = micros();
  282. for(i=0; i<w; i+=6) {
  283. i2 = i / 2;
  284. tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));
  285. }
  286. return micros() - start;
  287. }
  288. unsigned long testFilledRoundRects() {
  289. unsigned long start;
  290. int i, i2,
  291. cx = tft.width() / 2 - 1,
  292. cy = tft.height() / 2 - 1;
  293. tft.fillScreen(ILI9341_BLACK);
  294. start = micros();
  295. for(i=min(tft.width(), tft.height()); i>20; i-=6) {
  296. i2 = i / 2;
  297. tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
  298. yield();
  299. }
  300. return micros() - start;
  301. }