Advertisement
Guest User

main.cpp

a guest
Apr 9th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.84 KB | None | 0 0
  1. #include "Adafruit_ST7735.h"
  2. #include "mbed.h"
  3. #include "rtos.h"
  4.  
  5. Adafruit_ST7735 tft(D11, D12, D13, D10, D8,
  6.                     D9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RSTss
  7. Thread displayThread;
  8.  
  9. void display();
  10.  
  11. void testlines(uint16_t color);
  12. void testfastlines(uint16_t color1, uint16_t color2);
  13. void testdrawrects(uint16_t color);
  14. void testfillrects(uint16_t color1, uint16_t color2);
  15. void testfillcircles(uint8_t radius, uint16_t color);
  16. void testdrawcircles(uint8_t radius, uint16_t color);
  17. void testtriangles(void);
  18. void testroundrects(void);
  19. void mediabuttons(void);
  20. void testdrawtext(char *text, uint16_t color);
  21. void tftPrintTest(void);
  22.  
  23. int main(void) {
  24.   printf("Start\r\n");
  25.   displayThread.start(display);
  26.  
  27.   while (1) {
  28.   }
  29. }
  30.  
  31. void display() {
  32.   printf("Display Thread \r\n");
  33.   // Use this initializer if you're using a 1.8" TFT
  34.  
  35.   tft.initR(); // initialize a ST7735S chip, black tab
  36.   tft.setRotation(1);
  37.   tft.fillScreen(ST7735_BLACK);
  38.   char hello[] =
  39.       "123Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur "
  40.       "adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, "
  41.       "fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor "
  42.       "neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet "
  43.       "ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a "
  44.       "tortor imperdiet posuere. ";
  45.   testdrawtext(hello, ST7735_WHITE);
  46.   ThisThread::sleep_for(1000);
  47.  
  48.   // tft print function!
  49.   tftPrintTest();
  50.   ThisThread::sleep_for(1000);
  51.  
  52.   tft.drawPixel(tft.width() / 2, tft.height() / 2, ST7735_GREEN);
  53.   ThisThread::sleep_for(500);
  54.  
  55.   testlines(ST7735_YELLOW);
  56.   ThisThread::sleep_for(500);
  57.  
  58.   testfastlines(ST7735_RED, ST7735_BLUE);
  59.   ThisThread::sleep_for(500);
  60.  
  61.   testdrawrects(ST7735_GREEN);
  62.   ThisThread::sleep_for(500);
  63.  
  64.   testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
  65.   ThisThread::sleep_for(500);
  66.  
  67.   tft.fillScreen(ST7735_BLACK);
  68.   testfillcircles(10, ST7735_BLUE);
  69.   testdrawcircles(10, ST7735_WHITE);
  70.   ThisThread::sleep_for(500);
  71.  
  72.   testroundrects();
  73.   ThisThread::sleep_for(500);
  74.  
  75.   testtriangles();
  76.   ThisThread::sleep_for(500);
  77.  
  78.   mediabuttons();
  79.  
  80.   ThisThread::sleep_for(500);
  81.  
  82.   while (1) {
  83.     tft.invertDisplay(true);
  84.     ThisThread::sleep_for(500);
  85.     tft.invertDisplay(false);
  86.     ThisThread::sleep_for(500);
  87.   }
  88. }
  89.  
  90. void testlines(uint16_t color) {
  91.   tft.fillScreen(ST7735_BLACK);
  92.   for (int16_t x = 0; x < tft.width(); x += 6) {
  93.     tft.drawLine(0, 0, x, tft.height() - 1, color);
  94.   }
  95.   for (int16_t y = 0; y < tft.height(); y += 6) {
  96.     tft.drawLine(0, 0, tft.width() - 1, y, color);
  97.   }
  98.  
  99.   tft.fillScreen(ST7735_BLACK);
  100.   for (int16_t x = 0; x < tft.width(); x += 6) {
  101.     tft.drawLine(tft.width() - 1, 0, x, tft.height() - 1, color);
  102.   }
  103.   for (int16_t y = 0; y < tft.height(); y += 6) {
  104.     tft.drawLine(tft.width() - 1, 0, 0, y, color);
  105.   }
  106.  
  107.   tft.fillScreen(ST7735_BLACK);
  108.   for (int16_t x = 0; x < tft.width(); x += 6) {
  109.     tft.drawLine(0, tft.height() - 1, x, 0, color);
  110.   }
  111.   for (int16_t y = 0; y < tft.height(); y += 6) {
  112.     tft.drawLine(0, tft.height() - 1, tft.width() - 1, y, color);
  113.   }
  114.  
  115.   tft.fillScreen(ST7735_BLACK);
  116.   for (int16_t x = 0; x < tft.width(); x += 6) {
  117.     tft.drawLine(tft.width() - 1, tft.height() - 1, x, 0, color);
  118.   }
  119.   for (int16_t y = 0; y < tft.height(); y += 6) {
  120.     tft.drawLine(tft.width() - 1, tft.height() - 1, 0, y, color);
  121.   }
  122. }
  123.  
  124. void testfastlines(uint16_t color1, uint16_t color2) {
  125.   tft.fillScreen(ST7735_BLACK);
  126.   for (int16_t y = 0; y < tft.height(); y += 5) {
  127.     tft.drawFastHLine(0, y, tft.width(), color1);
  128.   }
  129.   for (int16_t x = 0; x < tft.width(); x += 5) {
  130.     tft.drawFastVLine(x, 0, tft.height(), color2);
  131.   }
  132. }
  133.  
  134. void testdrawrects(uint16_t color) {
  135.   tft.fillScreen(ST7735_BLACK);
  136.   for (int16_t x = 0; x < tft.width(); x += 6) {
  137.     tft.drawRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x,
  138.                  color);
  139.   }
  140. }
  141.  
  142. void testfillrects(uint16_t color1, uint16_t color2) {
  143.   tft.fillScreen(ST7735_BLACK);
  144.   for (int16_t x = tft.width() - 1; x > 6; x -= 6) {
  145.     tft.fillRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x,
  146.                  color1);
  147.     tft.drawRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x,
  148.                  color2);
  149.   }
  150. }
  151.  
  152. void testfillcircles(uint8_t radius, uint16_t color) {
  153.   for (int16_t x = radius; x < tft.width(); x += radius * 2) {
  154.     for (int16_t y = radius; y < tft.height(); y += radius * 2) {
  155.       tft.fillCircle(x, y, radius, color);
  156.     }
  157.   }
  158. }
  159.  
  160. void testdrawcircles(uint8_t radius, uint16_t color) {
  161.   for (int16_t x = 0; x < tft.width() + radius; x += radius * 2) {
  162.     for (int16_t y = 0; y < tft.height() + radius; y += radius * 2) {
  163.       tft.drawCircle(x, y, radius, color);
  164.     }
  165.   }
  166. }
  167.  
  168. void testtriangles() {
  169.   tft.fillScreen(ST7735_BLACK);
  170.   int color = 0xF800;
  171.   int t;
  172.   int w = tft.width() / 2;
  173.   int x = tft.height() - 1;
  174.   int y = 0;
  175.   int z = tft.width();
  176.   for (t = 0; t <= 15; t += 1) {
  177.     tft.drawTriangle(w, y, y, x, z, x, color);
  178.     x -= 4;
  179.     y += 4;
  180.     z -= 4;
  181.     color += 100;
  182.   }
  183. }
  184.  
  185. void testroundrects() {
  186.   tft.fillScreen(ST7735_BLACK);
  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 = tft.width() - 2;
  194.     int h = tft.height() - 2;
  195.     for (i = 0; i <= 16; i += 1) {
  196.       tft.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.  
  207. void mediabuttons() {
  208.   // play
  209.   tft.fillScreen(ST7735_BLACK);
  210.   tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
  211.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
  212.   ThisThread::sleep_for(500);
  213.   // pause
  214.   tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
  215.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
  216.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
  217.   ThisThread::sleep_for(500);
  218.   // play color
  219.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
  220.   ThisThread::sleep_for(500);
  221.   // pause color
  222.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
  223.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
  224.   ThisThread::sleep_for(500);
  225.   // play color
  226.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
  227. }
  228.  
  229. void testdrawtext(char *text, uint16_t color) {
  230.   tft.setCursor(0, 0);
  231.   tft.setTextColor(color);
  232.   tft.setTextWrap(true);
  233.   tft.printf("%s", text);
  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.printf("Hello World!\r\n");
  243.   tft.setTextColor(ST7735_YELLOW);
  244.   tft.setTextSize(2);
  245.   tft.printf("Hello World!\r\n");
  246.   tft.setTextColor(ST7735_GREEN);
  247.   tft.setTextSize(3);
  248.   tft.printf("Hello World!\r\n");
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement