Advertisement
Guest User

main.cpp

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