查看: 4758|回复: 0

【Tiny ML微型机器学习开发板】LCD仪表显示测评

[复制链接]
  • TA的每日心情

    2021-7-15 14:11
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2021-7-15 14:15:52 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 liliny 于 2021-7-15 14:22 编辑

    Wio Terminal开发板带有LCD屏
    LCD屏
    分辨率320 x 240
    显示屏尺寸2.4英寸
    驱动器ICILI9341
    通过开发板所带的例程,我们能够测试LCD的显示效果

    IMG_20210710_084001.jpg 程序代码:
    1. /*
    2.     Example animated analogue meters using a ILI9341 TFT LCD screen

    3.     Needs Font 2 (also Font 4 if using large scale label)

    4.     Make sure all the display driver and pin comnenctions are correct by
    5.     editting the User_Setup.h file in the TFT_eSPI library folder.

    6.     #########################################################################
    7.     ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
    8.     #########################################################################
    9. */

    10. #include <TFT_eSPI.h> // Hardware-specific library
    11. #include <SPI.h>

    12. TFT_eSPI tft = TFT_eSPI();       // Invoke custom library

    13. #define TFT_GREY 0x5AEB

    14. #define LOOP_PERIOD 35 // Display updates every 35 ms

    15. float ltx = 0;    // Saved x coord of bottom of needle
    16. uint16_t osx = 120, osy = 120; // Saved x & y coords
    17. uint32_t updateTime = 0;       // time for next update

    18. int old_analog =  -999; // Value last displayed
    19. int old_digital = -999; // Value last displayed

    20. int value[6] = {0, 0, 0, 0, 0, 0};
    21. int old_value[6] = { -1, -1, -1, -1, -1, -1};
    22. int d = 0;

    23. void setup(void) {
    24.     tft.init();
    25.     tft.setRotation(0);
    26.     Serial.begin(57600); // For debug
    27.     tft.fillScreen(TFT_BLACK);

    28.     analogMeter(); // Draw analogue meter

    29.     // Draw 6 linear meters
    30.     byte d = 40;
    31.     plotLinear("A0", 0, 160);
    32.     plotLinear("A1", 1 * d, 160);
    33.     plotLinear("A2", 2 * d, 160);
    34.     plotLinear("A3", 3 * d, 160);
    35.     plotLinear("A4", 4 * d, 160);
    36.     plotLinear("A5", 5 * d, 160);

    37.     updateTime = millis(); // Next update time
    38. }


    39. void loop() {
    40.     if (updateTime <= millis()) {
    41.         updateTime = millis() + LOOP_PERIOD;

    42.         d += 4;
    43.         if (d >= 360) {
    44.             d = 0;
    45.         }

    46.         //value[0] = map(analogRead(A0), 0, 1023, 0, 100); // Test with value form Analogue 0

    47.         // Create a Sine wave for testing
    48.         value[0] = 50 + 50 * sin((d + 0) * 0.0174532925);
    49.         value[1] = 50 + 50 * sin((d + 60) * 0.0174532925);
    50.         value[2] = 50 + 50 * sin((d + 120) * 0.0174532925);
    51.         value[3] = 50 + 50 * sin((d + 180) * 0.0174532925);
    52.         value[4] = 50 + 50 * sin((d + 240) * 0.0174532925);
    53.         value[5] = 50 + 50 * sin((d + 300) * 0.0174532925);

    54.         //unsigned long t = millis();

    55.         plotPointer();

    56.         plotNeedle(value[0], 0);

    57.         //Serial.println(millis()-t); // Print time taken for meter update
    58.     }
    59. }


    60. // #########################################################################
    61. //  Draw the analogue meter on the screen
    62. // #########################################################################
    63. void analogMeter() {
    64.     // Meter outline
    65.     tft.fillRect(0, 0, 239, 126, TFT_GREY);
    66.     tft.fillRect(5, 3, 230, 119, TFT_WHITE);

    67.     tft.setTextColor(TFT_BLACK);  // Text colour

    68.     // Draw ticks every 5 degrees from -50 to +50 degrees (100 deg. FSD swing)
    69.     for (int i = -50; i < 51; i += 5) {
    70.         // Long scale tick length
    71.         int tl = 15;

    72.         // Coodinates of tick to draw
    73.         float sx = cos((i - 90) * 0.0174532925);
    74.         float sy = sin((i - 90) * 0.0174532925);
    75.         uint16_t x0 = sx * (100 + tl) + 120;
    76.         uint16_t y0 = sy * (100 + tl) + 140;
    77.         uint16_t x1 = sx * 100 + 120;
    78.         uint16_t y1 = sy * 100 + 140;

    79.         // Coordinates of next tick for zone fill
    80.         float sx2 = cos((i + 5 - 90) * 0.0174532925);
    81.         float sy2 = sin((i + 5 - 90) * 0.0174532925);
    82.         int x2 = sx2 * (100 + tl) + 120;
    83.         int y2 = sy2 * (100 + tl) + 140;
    84.         int x3 = sx2 * 100 + 120;
    85.         int y3 = sy2 * 100 + 140;

    86.         // Yellow zone limits
    87.         //if (i >= -50 && i < 0) {
    88.         //  tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_YELLOW);
    89.         //  tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_YELLOW);
    90.         //}

    91.         // Green zone limits
    92.         if (i >= 0 && i < 25) {
    93.             tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_GREEN);
    94.             tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_GREEN);
    95.         }

    96.         // Orange zone limits
    97.         if (i >= 25 && i < 50) {
    98.             tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_ORANGE);
    99.             tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_ORANGE);
    100.         }

    101.         // Short scale tick length
    102.         if (i % 25 != 0) {
    103.             tl = 8;
    104.         }

    105.         // Recalculate coords incase tick lenght changed
    106.         x0 = sx * (100 + tl) + 120;
    107.         y0 = sy * (100 + tl) + 140;
    108.         x1 = sx * 100 + 120;
    109.         y1 = sy * 100 + 140;

    110.         // Draw tick
    111.         tft.drawLine(x0, y0, x1, y1, TFT_BLACK);

    112.         // Check if labels should be drawn, with position tweaks
    113.         if (i % 25 == 0) {
    114.             // Calculate label positions
    115.             x0 = sx * (100 + tl + 10) + 120;
    116.             y0 = sy * (100 + tl + 10) + 140;
    117.             switch (i / 25) {
    118.                 case -2: tft.drawCentreString("0", x0, y0 - 12, 2); break;
    119.                 case -1: tft.drawCentreString("25", x0, y0 - 9, 2); break;
    120.                 case 0: tft.drawCentreString("50", x0, y0 - 6, 2); break;
    121.                 case 1: tft.drawCentreString("75", x0, y0 - 9, 2); break;
    122.                 case 2: tft.drawCentreString("100", x0, y0 - 12, 2); break;
    123.             }
    124.         }

    125.         // Now draw the arc of the scale
    126.         sx = cos((i + 5 - 90) * 0.0174532925);
    127.         sy = sin((i + 5 - 90) * 0.0174532925);
    128.         x0 = sx * 100 + 120;
    129.         y0 = sy * 100 + 140;
    130.         // Draw scale arc, don't draw the last part
    131.         if (i < 50) {
    132.             tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
    133.         }
    134.     }

    135.     tft.drawString("%RH", 5 + 230 - 40, 119 - 20, 2); // Units at bottom right
    136.     tft.drawCentreString("%RH", 120, 70, 4); // Comment out to avoid font 4
    137.     tft.drawRect(5, 3, 230, 119, TFT_BLACK); // Draw bezel line

    138.     plotNeedle(0, 0); // Put meter needle at 0
    139. }

    140. // #########################################################################
    141. // Update needle position
    142. // This function is blocking while needle moves, time depends on ms_delay
    143. // 10ms minimises needle flicker if text is drawn within needle sweep area
    144. // Smaller values OK if text not in sweep area, zero for instant movement but
    145. // does not look realistic... (note: 100 increments for full scale deflection)
    146. // #########################################################################
    147. void plotNeedle(int value, byte ms_delay) {
    148.     tft.setTextColor(TFT_BLACK, TFT_WHITE);
    149.     char buf[8]; dtostrf(value, 4, 0, buf);
    150.     tft.drawRightString(buf, 40, 119 - 20, 2);

    151.     if (value < -10) {
    152.         value = -10;    // Limit value to emulate needle end stops
    153.     }
    154.     if (value > 110) {
    155.         value = 110;
    156.     }

    157.     // Move the needle util new value reached
    158.     while (!(value == old_analog)) {
    159.         if (old_analog < value) {
    160.             old_analog++;
    161.         } else {
    162.             old_analog--;
    163.         }

    164.         if (ms_delay == 0) {
    165.             old_analog = value;    // Update immediately id delay is 0
    166.         }

    167.         float sdeg = map(old_analog, -10, 110, -150, -30); // Map value to angle
    168.         // Calcualte tip of needle coords
    169.         float sx = cos(sdeg * 0.0174532925);
    170.         float sy = sin(sdeg * 0.0174532925);

    171.         // Calculate x delta of needle start (does not start at pivot point)
    172.         float tx = tan((sdeg + 90) * 0.0174532925);

    173.         // Erase old needle image
    174.         tft.drawLine(120 + 20 * ltx - 1, 140 - 20, osx - 1, osy, TFT_WHITE);
    175.         tft.drawLine(120 + 20 * ltx, 140 - 20, osx, osy, TFT_WHITE);
    176.         tft.drawLine(120 + 20 * ltx + 1, 140 - 20, osx + 1, osy, TFT_WHITE);

    177.         // Re-plot text under needle
    178.         tft.setTextColor(TFT_BLACK);
    179.         tft.drawCentreString("%RH", 120, 70, 4); // // Comment out to avoid font 4

    180.         // Store new needle end coords for next erase
    181.         ltx = tx;
    182.         osx = sx * 98 + 120;
    183.         osy = sy * 98 + 140;

    184.         // Draw the needle in the new postion, magenta makes needle a bit bolder
    185.         // draws 3 lines to thicken needle
    186.         tft.drawLine(120 + 20 * ltx - 1, 140 - 20, osx - 1, osy, TFT_RED);
    187.         tft.drawLine(120 + 20 * ltx, 140 - 20, osx, osy, TFT_MAGENTA);
    188.         tft.drawLine(120 + 20 * ltx + 1, 140 - 20, osx + 1, osy, TFT_RED);

    189.         // Slow needle down slightly as it approaches new postion
    190.         if (abs(old_analog - value) < 10) {
    191.             ms_delay += ms_delay / 5;
    192.         }

    193.         // Wait before next update
    194.         delay(ms_delay);
    195.     }
    196. }

    197. // #########################################################################
    198. //  Draw a linear meter on the screen
    199. // #########################################################################
    200. void plotLinear(char* label, int x, int y) {
    201.     int w = 36;
    202.     tft.drawRect(x, y, w, 155, TFT_GREY);
    203.     tft.fillRect(x + 2, y + 19, w - 3, 155 - 38, TFT_WHITE);
    204.     tft.setTextColor(TFT_CYAN, TFT_BLACK);
    205.     tft.drawCentreString(label, x + w / 2, y + 2, 2);

    206.     for (int i = 0; i < 110; i += 10) {
    207.         tft.drawFastHLine(x + 20, y + 27 + i, 6, TFT_BLACK);
    208.     }

    209.     for (int i = 0; i < 110; i += 50) {
    210.         tft.drawFastHLine(x + 20, y + 27 + i, 9, TFT_BLACK);
    211.     }

    212.     tft.fillTriangle(x + 3, y + 127, x + 3 + 16, y + 127, x + 3, y + 127 - 5, TFT_RED);
    213.     tft.fillTriangle(x + 3, y + 127, x + 3 + 16, y + 127, x + 3, y + 127 + 5, TFT_RED);

    214.     tft.drawCentreString("---", x + w / 2, y + 155 - 18, 2);
    215. }

    216. // #########################################################################
    217. //  Adjust 6 linear meter pointer positions
    218. // #########################################################################
    219. void plotPointer(void) {
    220.     int dy = 187;
    221.     byte pw = 16;

    222.     tft.setTextColor(TFT_GREEN, TFT_BLACK);

    223.     // Move the 6 pointers one pixel towards new value
    224.     for (int i = 0; i < 6; i++) {
    225.         char buf[8]; dtostrf(value[i], 4, 0, buf);
    226.         tft.drawRightString(buf, i * 40 + 36 - 5, 187 - 27 + 155 - 18, 2);

    227.         int dx = 3 + 40 * i;
    228.         if (value[i] < 0) {
    229.             value[i] = 0;    // Limit value to emulate needle end stops
    230.         }
    231.         if (value[i] > 100) {
    232.             value[i] = 100;
    233.         }

    234.         while (!(value[i] == old_value[i])) {
    235.             dy = 187 + 100 - old_value[i];
    236.             if (old_value[i] > value[i]) {
    237.                 tft.drawLine(dx, dy - 5, dx + pw, dy, TFT_WHITE);
    238.                 old_value[i]--;
    239.                 tft.drawLine(dx, dy + 6, dx + pw, dy + 1, TFT_RED);
    240.             } else {
    241.                 tft.drawLine(dx, dy + 5, dx + pw, dy, TFT_WHITE);
    242.                 old_value[i]++;
    243.                 tft.drawLine(dx, dy - 6, dx + pw, dy - 1, TFT_RED);
    244.             }
    245.         }
    246.     }
    247. }
    复制代码


    烧录成功后,屏幕显示效果如图:
    IMG_20210710_083908.jpg

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-11-19 12:16 , Processed in 0.110566 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.