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.

354 lines
8.5KB

  1. #include <SPI.h>
  2. #include <Adafruit_GFX.h>
  3. #include <TFT_ILI9163C.h>
  4. // Color definitions
  5. #define BLACK 0x0000
  6. #define BLUE 0x001F
  7. #define RED 0xF800
  8. #define GREEN 0x07E0
  9. #define CYAN 0x07FF
  10. #define MAGENTA 0xF81F
  11. #define YELLOW 0xFFE0
  12. #define WHITE 0xFFFF
  13. /*
  14. Teensy3.x and Arduino's
  15. You are using 4 wire SPI here, so:
  16. MOSI: 11//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  17. MISO: 12//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  18. SCK: 13//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  19. the rest of pin below:
  20. */
  21. #define __CS 10
  22. #define __DC 9
  23. /*
  24. Teensy 3.x can use: 2,6,9,10,15,20,21,22,23
  25. Arduino's 8 bit: any
  26. DUE: check arduino site
  27. If you do not use reset, tie it to +3V3
  28. */
  29. TFT_ILI9163C display = TFT_ILI9163C(__CS, __DC);
  30. float p = 3.1415926;
  31. void setup(void) {
  32. display.begin();
  33. uint16_t time = millis();
  34. time = millis() - time;
  35. // lcdTestPattern();
  36. // delay(1000);
  37. display.clearScreen();
  38. display.setCursor(0,0);
  39. display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
  40. delay(1000);
  41. tftPrintTest();
  42. delay(2000);
  43. //a single pixel
  44. display.drawPixel(display.width()/2, display.height()/2, GREEN);
  45. delay(500);
  46. // line draw test
  47. testlines(YELLOW);
  48. delay(500);
  49. // optimized lines
  50. testfastlines(RED, BLUE);
  51. delay(500);
  52. testdrawrects(GREEN);
  53. delay(1000);
  54. testfillrects(BLUE, YELLOW);
  55. delay(1000);
  56. randomRect(0);
  57. delay(100);
  58. randomCircles(0);
  59. delay(100);
  60. randomLines();
  61. delay(100);
  62. randomPoints();
  63. delay(500);
  64. display.clearScreen();
  65. testfillcircles(10, BLUE);
  66. testdrawcircles(10, WHITE);
  67. delay(1000);
  68. testroundrects();
  69. delay(500);
  70. testtriangles();
  71. delay(500);
  72. }
  73. void loop() {
  74. testlines(random(0x0010,0xFFFF));
  75. randomLines();
  76. //randomCircles(1);
  77. randomCircles(0);
  78. randomRect(1);
  79. randomRect(1);
  80. randomRect(1);
  81. randomRect(1);
  82. randomRect(1);
  83. randomRect(0);
  84. randomRect(0);
  85. randomRect(0);
  86. randomRect(0);
  87. randomRect(0);
  88. randomRect(0);
  89. randomPoints();
  90. }
  91. void testlines(uint16_t color) {
  92. display.clearScreen();
  93. for (int16_t x=0; x < display.width()-1; x+=6) {
  94. display.drawLine(0, 0, x, display.height()-1, color);
  95. }
  96. for (int16_t y=0; y < display.height()-1; y+=6) {
  97. display.drawLine(0, 0, display.width()-1, y, color);
  98. }
  99. display.clearScreen();
  100. for (int16_t x=0; x < display.width()-1; x+=6) {
  101. display.drawLine(display.width()-1, 0, x, display.height()-1, color);
  102. }
  103. for (int16_t y=0; y < display.height()-1; y+=6) {
  104. display.drawLine(display.width()-1, 0, 0, y, color);
  105. }
  106. display.clearScreen();
  107. for (int16_t x=0; x < display.width()-1; x+=6) {
  108. display.drawLine(0, display.height()-1, x, 0, color);
  109. }
  110. for (int16_t y=0; y < display.height()-1; y+=6) {
  111. display.drawLine(0, display.height()-1, display.width()-1, y, color);
  112. }
  113. display.clearScreen();
  114. for (int16_t x=0; x < display.width()-1; x+=6) {
  115. display.drawLine(display.width()-1, display.height()-1, x, 0, color);
  116. }
  117. for (int16_t y=0; y < display.height()-1; y+=6) {
  118. display.drawLine(display.width()-1, display.height()-1, 0, y, color);
  119. }
  120. delay(500);
  121. }
  122. void testdrawtext(char *text, uint16_t color) {
  123. display.setTextSize(1);
  124. display.setTextColor(WHITE);
  125. display.setCursor(0,0);
  126. for (uint8_t i=0; i < 168; i++) {
  127. if (i == '\n') continue;
  128. display.write(i);
  129. if ((i > 0) && (i % 21 == 0))
  130. display.println();
  131. }
  132. }
  133. void testfastlines(uint16_t color1, uint16_t color2) {
  134. display.clearScreen();
  135. for (int16_t y=0; y < display.height()-1; y+=5) {
  136. display.drawFastHLine(0, y, display.width()-1, color1);
  137. }
  138. for (int16_t x=0; x < display.width()-1; x+=5) {
  139. display.drawFastVLine(x, 0, display.height()-1, color2);
  140. }
  141. }
  142. void testdrawrects(uint16_t color) {
  143. display.clearScreen();
  144. for (int16_t x=0; x < display.height()-1; x+=6) {
  145. display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color);
  146. }
  147. }
  148. void testfillrects(uint16_t color1, uint16_t color2) {
  149. display.clearScreen();
  150. for (int16_t x=display.height()-1; x > 6; x-=6) {
  151. display.fillRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color1);
  152. display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color2);
  153. }
  154. }
  155. void testfillcircles(uint8_t radius, uint16_t color) {
  156. for (uint8_t x=radius; x < display.width()-1; x+=radius*2) {
  157. for (uint8_t y=radius; y < display.height()-1; y+=radius*2) {
  158. display.fillCircle(x, y, radius, color);
  159. }
  160. }
  161. }
  162. void testdrawcircles(uint8_t radius, uint16_t color) {
  163. for (int16_t x=0; x < (display.width()-1)+radius; x+=radius*2) {
  164. for (int16_t y=0; y < (display.height())-1+radius; y+=radius*2) {
  165. display.drawCircle(x, y, radius, color);
  166. }
  167. }
  168. }
  169. void testtriangles() {
  170. display.clearScreen();
  171. int color = 0xF800;
  172. int t;
  173. int w = display.width()/2;
  174. int x = display.height();
  175. int y = 0;
  176. int z = display.width();
  177. for(t = 0 ; t <= 15; t+=1) {
  178. display.drawTriangle(w, y, y, x, z, x, color);
  179. x-=4;
  180. y+=4;
  181. z-=4;
  182. color+=100;
  183. }
  184. }
  185. void testroundrects() {
  186. display.clearScreen();
  187. int color = 100;
  188. int i;
  189. int t;
  190. for(t = 0 ; t <= 4; t+=1) {
  191. int x = 0;
  192. int y = 0;
  193. int w = display.width();
  194. int h = display.height();
  195. for(i = 0 ; i <= 24; i+=1) {
  196. display.drawRoundRect(x, y, w, h, 5, color);
  197. x+=2;
  198. y+=3;
  199. w-=4;
  200. h-=6;
  201. color+=1100;
  202. }
  203. color+=100;
  204. }
  205. }
  206. void tftPrintTest() {
  207. display.clearScreen();
  208. display.setCursor(0, 5);
  209. display.setTextColor(RED);
  210. display.setTextSize(1);
  211. display.println("Hello World!");
  212. display.setTextColor(YELLOW, GREEN);
  213. display.setTextSize(2);
  214. display.print("Hello Wo");
  215. display.setTextColor(BLUE);
  216. display.setTextSize(3);
  217. display.print(12.57);
  218. delay(1500);
  219. display.setCursor(0, 5);
  220. display.clearScreen();
  221. display.setTextColor(WHITE);
  222. display.setTextSize(0);
  223. display.println("Hello World!");
  224. display.setTextSize(1);
  225. display.setTextColor(GREEN);
  226. display.print(p, 5);
  227. display.println(" Want pi?");
  228. display.print(8675309, HEX);
  229. display.print(" Print HEX");
  230. display.setTextColor(WHITE);
  231. display.println("Sketch has been");
  232. display.println("running for: ");
  233. display.setTextColor(MAGENTA);
  234. display.print(millis() / 1000);
  235. display.setTextColor(WHITE);
  236. display.print(" sec.");
  237. }
  238. void randomRect(bool fill){
  239. display.clearScreen();
  240. uint8_t k,c;
  241. for (k = 0; k < 16; k++) {
  242. for (c = 0; c < 32; c++) {
  243. uint8_t cx, cy, x, y, w, h;
  244. // center
  245. cx = random(0,display.width());
  246. cy = random(0,display.height());
  247. // size
  248. w = random(0,30 + 6);
  249. h = random(0,20 + 4);
  250. // upper-left
  251. x = cx - w / 2;
  252. y = cy - h / 2;
  253. if (x < 0) x = 0;
  254. if (y < 0) y = 0;
  255. // adjust size
  256. if (x + w > display.width()) w = display.width() - x;
  257. if (y + h > display.height()) h = display.height() - y;
  258. if (fill){
  259. display.fillRect(x, y, w, h,random(0x0010,0xFFFF));
  260. }
  261. else {
  262. display.drawRect(x, y, w, h,random(0x0010,0xFFFF));
  263. }
  264. }
  265. display.clearScreen();
  266. }
  267. }
  268. void randomCircles(bool fill){
  269. display.clearScreen();
  270. uint8_t k,c;
  271. for (k = 0; k < display.height(); k++) {
  272. for (c = 0; c < display.height()/2; c++) {
  273. // coordinates
  274. uint8_t x = random(0,120 + 3), y = random(0,90 + 2), r = random(0,40 + 1);
  275. if (x - r < 0) r = x;
  276. if (x + r > (display.width()-1)) r = (display.width() - 1) - x;
  277. if (y - r < 0) r = y;
  278. if (y + r > (display.height()-1)) r = (display.height() - 1) - y;
  279. if (fill){
  280. display.fillCircle(x, y, r,random(0x0010,0xFFFF));
  281. }
  282. else {
  283. display.drawCircle(x, y, r,random(0x0010,0xFFFF));
  284. }
  285. }
  286. if (!fill)display.clearScreen();
  287. }
  288. }
  289. void randomLines(){
  290. display.clearScreen();
  291. uint8_t k,c;
  292. for (k = 0; k < display.height(); k++) {
  293. for (c = 0; c < display.height()/2; c++) {
  294. uint8_t x1 = random(0,display.width()), y1 = random(0,display.height()), x2 = random(0,display.width()), y2 = random(0,display.height());
  295. display.drawLine(x1, y1, x2, y2,random(0x0010,0xFFFF));
  296. }
  297. display.clearScreen();
  298. }
  299. }
  300. void randomPoints(){
  301. display.clearScreen();
  302. int k,c;
  303. for (k = 0; k < 128; k++) {
  304. for (c = 0; c < 1000; c++) {
  305. uint8_t x = random(0,display.width()), y = random(0,display.height());
  306. display.drawPixel(x, y,random(0x0010,0xFFFF));
  307. }
  308. display.clearScreen();
  309. }
  310. }