Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 14.36 KB | None | 0 0
  1. // All the mcufriend.com UNO shields have the same pinout.
  2. // i.e. control pins A0-A4.  Data D2-D9.  microSD D10-D13.
  3. // Touchscreens are normally A1, A2, D7, D6 but the order varies
  4. //
  5. // This demo should work with most Adafruit TFT libraries
  6. // If you are not using a shield,  use a full Adafruit constructor()
  7. // e.g. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  8.  
  9. #define LCD_CS A3 // Chip Select goes to Analog 3
  10. #define LCD_CD A2 // Command/Data goes to Analog 2
  11. #define LCD_WR A1 // LCD Write goes to Analog 1
  12. #define LCD_RD A0 // LCD Read goes to Analog 0
  13. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  14.  
  15. #include <SPI.h>          // f.k. for Arduino-1.5.2
  16. #include "Adafruit_GFX.h"// Hardware-specific library
  17. #include <MCUFRIEND_kbv.h>
  18. MCUFRIEND_kbv tft;
  19. //#include <Adafruit_TFTLCD.h>
  20. //Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  21.  
  22. // Assign human-readable names to some common 16-bit color values:
  23. #define BLACK   0x0000
  24. #define BLUE    0x001F
  25. #define RED     0xF800
  26. #define GREEN   0x07E0
  27. #define CYAN    0x07FF
  28. #define MAGENTA 0xF81F
  29. #define YELLOW  0xFFE0
  30. #define WHITE   0xFFFF
  31.  
  32. #ifndef min
  33. #define min(a, b) (((a) < (b)) ? (a) : (b))
  34. #endif
  35.  
  36. void setup(void);
  37. void loop(void);
  38. unsigned long testFillScreen();
  39. unsigned long testText();
  40. unsigned long testLines(uint16_t color);
  41. unsigned long testFastLines(uint16_t color1, uint16_t color2);
  42. unsigned long testRects(uint16_t color);
  43. unsigned long testFilledRects(uint16_t color1, uint16_t color2);
  44. unsigned long testFilledCircles(uint8_t radius, uint16_t color);
  45. unsigned long testCircles(uint8_t radius, uint16_t color);
  46. unsigned long testTriangles();
  47. unsigned long testFilledTriangles();
  48. unsigned long testRoundRects();
  49. unsigned long testFilledRoundRects();
  50. void progmemPrint(const char *str);
  51. void progmemPrintln(const char *str);
  52. void runtests(void);
  53.  
  54. uint16_t g_identifier;
  55.  
  56. extern const uint8_t hanzi[];
  57. void showhanzi(unsigned int x, unsigned int y, unsigned char index)
  58. {
  59.     uint8_t i, j, c, first = 1;
  60.     uint8_t *temp = (uint8_t*)hanzi;
  61.     uint16_t color;
  62.     tft.setAddrWindow(x, y, x + 31, y + 31); //设置区域
  63.     temp += index * 128;
  64.     for (j = 0; j < 128; j++)
  65.     {
  66.         c = pgm_read_byte(temp);
  67.         for (i = 0; i < 8; i++)
  68.         {
  69.             if ((c & (1 << i)) != 0)
  70.             {
  71.                 color = RED;
  72.             }
  73.             else
  74.             {
  75.                 color = BLACK;
  76.             }
  77.             tft.pushColors(&color, 1, first);
  78.             first = 0;
  79.         }
  80.         temp++;
  81.     }
  82. }
  83.  
  84. void setup(void) {
  85.     Serial.begin(9600);
  86.     uint32_t when = millis();
  87.     //    while (!Serial) ;   //hangs a Leonardo until you connect a Serial
  88.     if (!Serial) delay(5000);           //allow some time for Leonardo
  89.     Serial.println("Serial took " + String((millis() - when)) + "ms to start");
  90.     //    tft.reset();                 //hardware reset
  91.     uint16_t ID = tft.readID(); //
  92.     Serial.print("ID = 0x");
  93.     Serial.println(ID, HEX);
  94.     if (ID == 0xD3D3) ID = 0x9481; // write-only shield
  95. //    ID = 0x9329;                             // force ID
  96.     tft.begin(ID);
  97. }
  98.  
  99. #if defined(MCUFRIEND_KBV_H_)
  100. uint16_t scrollbuf[320];    // my biggest screen is 320x480
  101. #define READGRAM(x, y, buf, w, h)  tft.readGRAM(x, y, buf, w, h)
  102. #else
  103. uint16_t scrollbuf[320];    // Adafruit only does 240x320
  104. // Adafruit can read a block by one pixel at a time
  105. int16_t  READGRAM(int16_t x, int16_t y, uint16_t *block, int16_t w, int16_t h)
  106. {
  107.     uint16_t *p;
  108.     for (int row = 0; row < h; row++) {
  109.         p = block + row * w;
  110.         for (int col = 0; col < w; col++) {
  111.             *p++ = tft.readPixel(x + col, y + row);
  112.         }
  113.     }
  114. }
  115. #endif
  116.  
  117. void windowScroll(int16_t x, int16_t y, int16_t wid, int16_t ht, int16_t dx, int16_t dy, uint16_t *buf)
  118. {
  119.     if (dx) for (int16_t row = 0; row < ht; row++) {
  120.             READGRAM(x, y + row, buf, wid, 1);
  121.             tft.setAddrWindow(x, y + row, x + wid - 1, y + row);
  122.             tft.pushColors(buf + dx, wid - dx, 1);
  123.             tft.pushColors(buf + 0, dx, 0);
  124.         }
  125.     if (dy) for (int16_t col = 0; col < wid; col++) {
  126.             READGRAM(x + col, y, buf, 1, ht);
  127.             tft.setAddrWindow(x + col, y, x + col, y + ht - 1);
  128.             tft.pushColors(buf + dy, ht - dy, 1);
  129.             tft.pushColors(buf + 0, dy, 0);
  130.         }
  131. }
  132.  
  133. void printmsg(int row, const char *msg)
  134. {
  135.     tft.setTextColor(YELLOW, BLACK);
  136.     tft.setCursor(0, row);
  137.     tft.println(msg);
  138. }
  139.  
  140. void loop(void) {
  141.     uint8_t aspect;
  142.     uint16_t pixel;
  143.     const char *aspectname[] = {
  144.         "PORTRAIT", "LANDSCAPE", "PORTRAIT_REV", "LANDSCAPE_REV"
  145.     };
  146.     const char *colorname[] = { "BLUE", "GREEN", "RED", "GRAY" };
  147.     uint16_t colormask[] = { 0x001F, 0x07E0, 0xF800, 0xFFFF };
  148.     uint16_t dx, rgb, n, wid, ht, msglin;
  149.     tft.setRotation(0);
  150.     delay(2000);
  151.     testText();
  152.     if (tft.height() > 64) {
  153.         for (uint8_t cnt = 0; cnt < 4; cnt++) {
  154.             aspect = (cnt + 0) & 3;
  155.             tft.setRotation(aspect);
  156.             wid = tft.width();
  157.             ht = tft.height();
  158.             msglin = (ht > 160) ? 200 : 112;
  159.             testText();
  160.             dx = wid / 32;
  161.         }
  162.     }
  163. }
  164.  
  165. typedef struct {
  166.     PGM_P msg;
  167.     uint32_t ms;
  168. } TEST;
  169. TEST result[12];
  170.  
  171. #define RUNTEST(n, str, test) { result[n].msg = PSTR(str); result[n].ms = test; delay(500); }
  172.  
  173. void runtests(void)
  174. {
  175.     uint8_t i, len = 24, cnt;
  176.     uint32_t total;
  177.     RUNTEST(0, "FillScreen               ", testFillScreen());
  178.     RUNTEST(1, "Text                     ", testText());
  179.     RUNTEST(2, "Lines                    ", testLines(CYAN));
  180.     RUNTEST(3, "Horiz/Vert Lines         ", testFastLines(RED, BLUE));
  181.     RUNTEST(4, "Rectangles (outline)     ", testRects(GREEN));
  182.     RUNTEST(5, "Rectangles (filled)      ", testFilledRects(YELLOW, MAGENTA));
  183.     RUNTEST(6, "Circles (filled)         ", testFilledCircles(10, MAGENTA));
  184.     RUNTEST(7, "Circles (outline)        ", testCircles(10, WHITE));
  185.     RUNTEST(8, "Triangles (outline)      ", testTriangles());
  186.     RUNTEST(9, "Triangles (filled)       ", testFilledTriangles());
  187.     RUNTEST(10, "Rounded rects (outline)  ", testRoundRects());
  188.     RUNTEST(11, "Rounded rects (filled)   ", testFilledRoundRects());
  189.  
  190.     tft.fillScreen(BLACK);
  191.     tft.setTextColor(GREEN);
  192.     tft.setCursor(0, 0);
  193.     uint16_t wid = tft.width();
  194.     if (wid > 176) {
  195.         tft.setTextSize(2);
  196. #if defined(MCUFRIEND_KBV_H_)
  197.         tft.print("MCUFRIEND ");
  198. #if MCUFRIEND_KBV_H_ != 0
  199.         tft.print(0.01 * MCUFRIEND_KBV_H_, 2);
  200. #else
  201.         tft.print("for");
  202. #endif
  203.         tft.println(" UNO");
  204. #else
  205.         tft.println("Adafruit-Style Tests");
  206. #endif
  207.     } else len = wid / 6 - 8;
  208.     tft.setTextSize(1);
  209.     total = 0;
  210.     for (i = 0; i < 12; i++) {
  211.         PGM_P str = result[i].msg;
  212.         char c;
  213.         if (len > 24) {
  214.             if (i < 10) tft.print(" ");
  215.             tft.print(i);
  216.             tft.print(": ");
  217.         }
  218.         uint8_t cnt = len;
  219.         while ((c = pgm_read_byte(str++)) && cnt--) tft.print(c);
  220.         tft.print(" ");
  221.         tft.println(result[i].ms);
  222.         total += result[i].ms;
  223.     }
  224.     tft.setTextSize(2);
  225.     tft.print("Total:");
  226.     tft.print(0.000001 * total);
  227.     tft.println("sec");
  228.     g_identifier = tft.readID();
  229.     tft.print("ID: 0x");
  230.     tft.println(tft.readID(), HEX);
  231. //    tft.print("Reg(00):0x");
  232. //    tft.println(tft.readReg(0x00), HEX);
  233.     tft.print("F_CPU:");
  234.     tft.print(0.000001 * F_CPU);
  235. #if defined(__OPTIMIZE_SIZE__)
  236.     tft.println("MHz -Os");
  237. #else
  238.     tft.println("MHz");
  239. #endif
  240.  
  241.     delay(10000);
  242. }
  243.  
  244. // Standard Adafruit tests.  will adjust to screen size
  245.  
  246. unsigned long testFillScreen() {
  247.     unsigned long start = micros();
  248.     tft.fillScreen(BLACK);
  249.     tft.fillScreen(RED);
  250.     tft.fillScreen(GREEN);
  251.     tft.fillScreen(BLUE);
  252.     tft.fillScreen(BLACK);
  253.     return micros() - start;
  254. }
  255.  
  256. unsigned long testText() {
  257.     unsigned long start;
  258.     start = micros();
  259.    float pressureRawValue =  0.0;
  260.   pressureRawValue = analogRead(A0);
  261.  
  262.   float pressureKpaValue = pressureRawValue * 63.529 + 5.411;
  263.   float pressureBarValue = pressureKpaValue * 0.01;
  264.  
  265.  
  266.   char* s;
  267.   printmsg(0, "Tozi text e ot nas");
  268.   sprintf(s, "Raw %.4f V", pressureRawValue);
  269.   printmsg(0, s);
  270.   sprintf(s, "kPA %.4f kPA", pressureKpaValue);
  271.   printmsg(0, s);
  272.   sprintf(s, "BAR %.4f bar", pressureBarValue);
  273.   printmsg(0, s);
  274.     return micros() - start;
  275. }
  276.  
  277. unsigned long testLines(uint16_t color) {
  278.     unsigned long start, t;
  279.     int           x1, y1, x2, y2,
  280.                   w = tft.width(),
  281.                   h = tft.height();
  282.  
  283.     tft.fillScreen(BLACK);
  284.  
  285.     x1 = y1 = 0;
  286.     y2    = h - 1;
  287.     start = micros();
  288.     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  289.     x2    = w - 1;
  290.     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  291.     t     = micros() - start; // fillScreen doesn't count against timing
  292.  
  293.     tft.fillScreen(BLACK);
  294.  
  295.     x1    = w - 1;
  296.     y1    = 0;
  297.     y2    = h - 1;
  298.     start = micros();
  299.     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  300.     x2    = 0;
  301.     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  302.     t    += micros() - start;
  303.  
  304.     tft.fillScreen(BLACK);
  305.  
  306.     x1    = 0;
  307.     y1    = h - 1;
  308.     y2    = 0;
  309.     start = micros();
  310.     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  311.     x2    = w - 1;
  312.     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  313.     t    += micros() - start;
  314.  
  315.     tft.fillScreen(BLACK);
  316.  
  317.     x1    = w - 1;
  318.     y1    = h - 1;
  319.     y2    = 0;
  320.     start = micros();
  321.     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  322.     x2    = 0;
  323.     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  324.  
  325.     return micros() - start;
  326. }
  327.  
  328. unsigned long testFastLines(uint16_t color1, uint16_t color2) {
  329.     unsigned long start;
  330.     int           x, y, w = tft.width(), h = tft.height();
  331.  
  332.     tft.fillScreen(BLACK);
  333.     start = micros();
  334.     for (y = 0; y < h; y += 5) tft.drawFastHLine(0, y, w, color1);
  335.     for (x = 0; x < w; x += 5) tft.drawFastVLine(x, 0, h, color2);
  336.  
  337.     return micros() - start;
  338. }
  339.  
  340. unsigned long testRects(uint16_t color) {
  341.     unsigned long start;
  342.     int           n, i, i2,
  343.                   cx = tft.width()  / 2,
  344.                   cy = tft.height() / 2;
  345.  
  346.     tft.fillScreen(BLACK);
  347.     n     = min(tft.width(), tft.height());
  348.     start = micros();
  349.     for (i = 2; i < n; i += 6) {
  350.         i2 = i / 2;
  351.         tft.drawRect(cx - i2, cy - i2, i, i, color);
  352.     }
  353.  
  354.     return micros() - start;
  355. }
  356.  
  357. unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
  358.     unsigned long start, t = 0;
  359.     int           n, i, i2,
  360.                   cx = tft.width()  / 2 - 1,
  361.                   cy = tft.height() / 2 - 1;
  362.  
  363.     tft.fillScreen(BLACK);
  364.     n = min(tft.width(), tft.height());
  365.     for (i = n; i > 0; i -= 6) {
  366.         i2    = i / 2;
  367.         start = micros();
  368.         tft.fillRect(cx - i2, cy - i2, i, i, color1);
  369.         t    += micros() - start;
  370.         // Outlines are not included in timing results
  371.         tft.drawRect(cx - i2, cy - i2, i, i, color2);
  372.     }
  373.  
  374.     return t;
  375. }
  376.  
  377. unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
  378.     unsigned long start;
  379.     int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
  380.  
  381.     tft.fillScreen(BLACK);
  382.     start = micros();
  383.     for (x = radius; x < w; x += r2) {
  384.         for (y = radius; y < h; y += r2) {
  385.             tft.fillCircle(x, y, radius, color);
  386.         }
  387.     }
  388.  
  389.     return micros() - start;
  390. }
  391.  
  392. unsigned long testCircles(uint8_t radius, uint16_t color) {
  393.     unsigned long start;
  394.     int           x, y, r2 = radius * 2,
  395.                         w = tft.width()  + radius,
  396.                         h = tft.height() + radius;
  397.  
  398.     // Screen is not cleared for this one -- this is
  399.     // intentional and does not affect the reported time.
  400.     start = micros();
  401.     for (x = 0; x < w; x += r2) {
  402.         for (y = 0; y < h; y += r2) {
  403.             tft.drawCircle(x, y, radius, color);
  404.         }
  405.     }
  406.  
  407.     return micros() - start;
  408. }
  409.  
  410. unsigned long testTriangles() {
  411.     unsigned long start;
  412.     int           n, i, cx = tft.width()  / 2 - 1,
  413.                         cy = tft.height() / 2 - 1;
  414.  
  415.     tft.fillScreen(BLACK);
  416.     n     = min(cx, cy);
  417.     start = micros();
  418.     for (i = 0; i < n; i += 5) {
  419.         tft.drawTriangle(
  420.             cx    , cy - i, // peak
  421.             cx - i, cy + i, // bottom left
  422.             cx + i, cy + i, // bottom right
  423.             tft.color565(0, 0, i));
  424.     }
  425.  
  426.     return micros() - start;
  427. }
  428.  
  429. unsigned long testFilledTriangles() {
  430.     unsigned long start, t = 0;
  431.     int           i, cx = tft.width()  / 2 - 1,
  432.                      cy = tft.height() / 2 - 1;
  433.  
  434.     tft.fillScreen(BLACK);
  435.     start = micros();
  436.     for (i = min(cx, cy); i > 10; i -= 5) {
  437.         start = micros();
  438.         tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  439.                          tft.color565(0, i, i));
  440.         t += micros() - start;
  441.         tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  442.                          tft.color565(i, i, 0));
  443.     }
  444.  
  445.     return t;
  446. }
  447.  
  448. unsigned long testRoundRects() {
  449.     unsigned long start;
  450.     int           w, i, i2, red, step,
  451.                   cx = tft.width()  / 2 - 1,
  452.                   cy = tft.height() / 2 - 1;
  453.  
  454.     tft.fillScreen(BLACK);
  455.     w     = min(tft.width(), tft.height());
  456.     start = micros();
  457.     red = 0;
  458.     step = (256 * 6) / w;
  459.     for (i = 0; i < w; i += 6) {
  460.         i2 = i / 2;
  461.         red += step;
  462.         tft.drawRoundRect(cx - i2, cy - i2, i, i, i / 8, tft.color565(red, 0, 0));
  463.     }
  464.  
  465.     return micros() - start;
  466. }
  467.  
  468. unsigned long testFilledRoundRects() {
  469.     unsigned long start;
  470.     int           i, i2, green, step,
  471.                   cx = tft.width()  / 2 - 1,
  472.                   cy = tft.height() / 2 - 1;
  473.  
  474.     tft.fillScreen(BLACK);
  475.     start = micros();
  476.     green = 256;
  477.     step = (256 * 6) / min(tft.width(), tft.height());
  478.     for (i = min(tft.width(), tft.height()); i > 20; i -= 6) {
  479.         i2 = i / 2;
  480.         green -= step;
  481.         tft.fillRoundRect(cx - i2, cy - i2, i, i, i / 8, tft.color565(0, green, 0));
  482.     }
  483.  
  484.     return micros() - start;
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement