acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Meter_5.ino (7275B)

      1 /*
      2  An example analogue meter using a ST7735 TFT LCD screen
      3 
      4  This example uses the hardware SPI only
      5  Needs Font 2 (also Font 4 if using large scale label)
      6 
      7  Updated by Bodmer for variable meter size
      8 */
      9 
     10 // Define meter size
     11 #define M_SIZE 0.667
     12 
     13 #include <TFT_eSPI.h> // Hardware-specific library
     14 #include <SPI.h>
     15 
     16 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     17 
     18 #define TFT_GREY 0x5AEB
     19 #define TFT_ORANGE      0xFD20      /* 255, 165,   0 */
     20 
     21 float ltx = 0;    // Saved x coord of bottom of needle
     22 uint16_t osx = M_SIZE*120, osy = M_SIZE*120; // Saved x & y coords
     23 uint32_t updateTime = 0;       // time for next update
     24 
     25 int old_analog =  -999; // Value last displayed
     26 
     27 int value[6] = {0, 0, 0, 0, 0, 0};
     28 int old_value[6] = { -1, -1, -1, -1, -1, -1};
     29 int d = 0;
     30 
     31 void setup(void) {
     32   Serial.begin(57600); // For debug
     33   tft.init();
     34   tft.setRotation(3);
     35 
     36   tft.fillScreen(TFT_BLACK);
     37 
     38   analogMeter(); // Draw analogue meter
     39 
     40   updateTime = millis(); // Next update time
     41 }
     42 
     43 
     44 void loop() {
     45   if (updateTime <= millis()) {
     46     updateTime = millis() + 35; // Update meter every 35 milliseconds
     47  
     48     // Create a Sine wave for testing
     49     d += 4; if (d >= 360) d = 0;
     50     value[0] = 50 + 50 * sin((d + 0) * 0.0174532925);
     51     //value[0] = random(0,100);
     52     //unsigned long tt = millis();
     53     plotNeedle(value[0], 0); // It takes between 2 and 14ms to replot the needle with zero delay
     54     //Serial.println(millis()-tt);
     55   }
     56 }
     57 
     58 
     59 // #########################################################################
     60 //  Draw the analogue meter on the screen
     61 // #########################################################################
     62 void analogMeter()
     63 {
     64 
     65   // Meter outline
     66   tft.fillRect(0, 0, M_SIZE*239, M_SIZE*131, TFT_GREY);
     67   tft.fillRect(1, M_SIZE*3, M_SIZE*234, M_SIZE*125, TFT_WHITE);
     68 
     69   tft.setTextColor(TFT_BLACK);  // Text colour
     70 
     71   // Draw ticks every 5 degrees from -50 to +50 degrees (100 deg. FSD swing)
     72   for (int i = -50; i < 51; i += 5) {
     73     // Long scale tick length
     74     int tl = 15;
     75 
     76     // Coodinates of tick to draw
     77     float sx = cos((i - 90) * 0.0174532925);
     78     float sy = sin((i - 90) * 0.0174532925);
     79     uint16_t x0 = sx * (M_SIZE*100 + tl) + M_SIZE*120;
     80     uint16_t y0 = sy * (M_SIZE*100 + tl) + M_SIZE*150;
     81     uint16_t x1 = sx * M_SIZE*100 + M_SIZE*120;
     82     uint16_t y1 = sy * M_SIZE*100 + M_SIZE*150;
     83 
     84     // Coordinates of next tick for zone fill
     85     float sx2 = cos((i + 5 - 90) * 0.0174532925);
     86     float sy2 = sin((i + 5 - 90) * 0.0174532925);
     87     int x2 = sx2 * (M_SIZE*100 + tl) + M_SIZE*120;
     88     int y2 = sy2 * (M_SIZE*100 + tl) + M_SIZE*150;
     89     int x3 = sx2 * M_SIZE*100 + M_SIZE*120;
     90     int y3 = sy2 * M_SIZE*100 + M_SIZE*150;
     91 
     92     // Yellow zone limits
     93     //if (i >= -50 && i < 0) {
     94     //  tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_YELLOW);
     95     //  tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_YELLOW);
     96     //}
     97 
     98     // Green zone limits
     99     if (i >= 0 && i < 25) {
    100       tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_GREEN);
    101       tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_GREEN);
    102     }
    103 
    104     // Orange zone limits
    105     if (i >= 25 && i < 50) {
    106       tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_ORANGE);
    107       tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_ORANGE);
    108     }
    109 
    110     // Short scale tick length
    111     if (i % 25 != 0) tl = 8;
    112 
    113     // Recalculate coords incase tick lenght changed
    114     x0 = sx * (M_SIZE*100 + tl) + M_SIZE*120;
    115     y0 = sy * (M_SIZE*100 + tl) + M_SIZE*150;
    116     x1 = sx * M_SIZE*100 + M_SIZE*120;
    117     y1 = sy * M_SIZE*100 + M_SIZE*150;
    118 
    119     // Draw tick
    120     tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
    121 
    122     // Check if labels should be drawn, with position tweaks
    123     if (i % 25 == 0) {
    124       // Calculate label positions
    125       x0 = sx * (M_SIZE*100 + tl + 10) + M_SIZE*120;
    126       y0 = sy * (M_SIZE*100 + tl + 10) + M_SIZE*150;
    127       switch (i / 25) {
    128         case -2: tft.drawCentreString("0", x0+4, y0-4, 1); break;
    129         case -1: tft.drawCentreString("25", x0+2, y0, 1); break;
    130         case 0: tft.drawCentreString("50", x0, y0, 1); break;
    131         case 1: tft.drawCentreString("75", x0, y0, 1); break;
    132         case 2: tft.drawCentreString("100", x0-2, y0-4, 1); break;
    133       }
    134     }
    135 
    136     // Now draw the arc of the scale
    137     sx = cos((i + 5 - 90) * 0.0174532925);
    138     sy = sin((i + 5 - 90) * 0.0174532925);
    139     x0 = sx * M_SIZE*100 + M_SIZE*120;
    140     y0 = sy * M_SIZE*100 + M_SIZE*150;
    141     // Draw scale arc, don't draw the last part
    142     if (i < 50) tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
    143   }
    144 
    145   tft.drawString("%RH", M_SIZE*(3 + 230 - 40), M_SIZE*(119 - 20), 2); // Units at bottom right
    146   tft.drawCentreString("%RH", M_SIZE*120, M_SIZE*75, 4); // Comment out to avoid font 4
    147   tft.drawRect(1, M_SIZE*3, M_SIZE*236, M_SIZE*126, TFT_BLACK); // Draw bezel line
    148 
    149   plotNeedle(0, 0); // Put meter needle at 0
    150 }
    151 
    152 // #########################################################################
    153 // Update needle position
    154 // This function is blocking while needle moves, time depends on ms_delay
    155 // 10ms minimises needle flicker if text is drawn within needle sweep area
    156 // Smaller values OK if text not in sweep area, zero for instant movement but
    157 // does not look realistic... (note: 100 increments for full scale deflection)
    158 // #########################################################################
    159 void plotNeedle(int value, byte ms_delay)
    160 {
    161   tft.setTextColor(TFT_BLACK, TFT_WHITE);
    162   char buf[8]; dtostrf(value, 4, 0, buf);
    163   tft.drawRightString(buf, 33, M_SIZE*(119 - 20), 2);
    164 
    165   if (value < -10) value = -10; // Limit value to emulate needle end stops
    166   if (value > 110) value = 110;
    167 
    168   // Move the needle until new value reached
    169   while (!(value == old_analog)) {
    170     if (old_analog < value) old_analog++;
    171     else old_analog--;
    172 
    173     if (ms_delay == 0) old_analog = value; // Update immediately if delay is 0
    174 
    175     float sdeg = map(old_analog, -10, 110, -150, -30); // Map value to angle
    176     // Calculate tip of needle coords
    177     float sx = cos(sdeg * 0.0174532925);
    178     float sy = sin(sdeg * 0.0174532925);
    179 
    180     // Calculate x delta of needle start (does not start at pivot point)
    181     float tx = tan((sdeg + 90) * 0.0174532925);
    182 
    183     // Erase old needle image
    184     tft.drawLine(M_SIZE*(120 + 24 * ltx) - 1, M_SIZE*(150 - 24), osx - 1, osy, TFT_WHITE);
    185     tft.drawLine(M_SIZE*(120 + 24 * ltx), M_SIZE*(150 - 24), osx, osy, TFT_WHITE);
    186     tft.drawLine(M_SIZE*(120 + 24 * ltx) + 1, M_SIZE*(150 - 24), osx + 1, osy, TFT_WHITE);
    187 
    188     // Re-plot text under needle
    189     tft.setTextColor(TFT_BLACK, TFT_WHITE);
    190     tft.drawCentreString("%RH", M_SIZE*120, M_SIZE*75, 4); // // Comment out to avoid font 4
    191 
    192     // Store new needle end coords for next erase
    193     ltx = tx;
    194     osx = M_SIZE*(sx * 98 + 120);
    195     osy = M_SIZE*(sy * 98 + 150);
    196 
    197     // Draw the needle in the new postion, magenta makes needle a bit bolder
    198     // draws 3 lines to thicken needle
    199     tft.drawLine(M_SIZE*(120 + 24 * ltx) - 1, M_SIZE*(150 - 24), osx - 1, osy, TFT_RED);
    200     tft.drawLine(M_SIZE*(120 + 24 * ltx), M_SIZE*(150 - 24), osx, osy, TFT_MAGENTA);
    201     tft.drawLine(M_SIZE*(120 + 24 * ltx) + 1, M_SIZE*(150 - 24), osx + 1, osy, TFT_RED);
    202 
    203     // Slow needle down slightly as it approaches new postion
    204     if (abs(old_analog - value) < 10) ms_delay += ms_delay / 5;
    205 
    206     // Wait before next update
    207     delay(ms_delay);
    208   }
    209 }
    210