acidportal

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

Analogue_meters.ino (2980B)

      1 /*
      2   Example animated analogue meters
      3 
      4   Needs Font 2 (also Font 4 if using large scale label)
      5 
      6   Make sure all the display driver and pin connections are correct by
      7   editing the User_Setup.h file in the TFT_eSPI library folder.
      8 
      9   #########################################################################
     10   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     11   #########################################################################
     12 
     13   Requires widget library here:
     14   https://github.com/Bodmer/TFT_eWidget
     15 */
     16 
     17 #include <TFT_eSPI.h>     // Hardware-specific library
     18 #include <TFT_eWidget.h>  // Widget library
     19 
     20 TFT_eSPI tft  = TFT_eSPI();      // Invoke custom library
     21 
     22 MeterWidget   amps  = MeterWidget(&tft);
     23 MeterWidget   volts = MeterWidget(&tft);
     24 MeterWidget   ohms  = MeterWidget(&tft);
     25 
     26 #define LOOP_PERIOD 35 // Display updates every 35 ms
     27 
     28 void setup(void) 
     29 {
     30   tft.init();
     31   tft.setRotation(0);
     32   Serial.begin(115200); // For debug
     33   
     34   
     35   // Colour zones are set as a start and end percentage of full scale (0-100)
     36   // If start and end of a colour zone are the same then that colour is not used
     37   //            --Red--  -Org-   -Yell-  -Grn-
     38   amps.setZones(75, 100, 50, 75, 25, 50, 0, 25); // Example here red starts at 75% and ends at 100% of full scale
     39   // Meter is 239 pixels wide and 126 pixels high
     40   amps.analogMeter(0, 0, 2.0, "mA", "0", "0.5", "1.0", "1.5", "2.0");    // Draw analogue meter at 0, 0
     41 
     42   // Colour draw order is red, orange, yellow, green. So red can be full scale with green drawn
     43   // last on top to indicate a "safe" zone.
     44   //             -Red-   -Org-  -Yell-  -Grn-
     45   volts.setZones(0, 100, 25, 75, 0, 0, 40, 60);
     46   volts.analogMeter(0, 128, 10.0, "V", "0", "2.5", "5", "7.5", "10"); // Draw analogue meter at 0, 128
     47 
     48   // No coloured zones if not defined
     49   ohms.analogMeter(0, 256, 100, "R", "0", "", "50", "", "100"); // Draw analogue meter at 0, 128
     50 }
     51 
     52 
     53 void loop() 
     54 {
     55   static int d = 0;
     56   static uint32_t updateTime = 0;  
     57 
     58   if (millis() - updateTime >= LOOP_PERIOD) 
     59   {
     60     updateTime = millis();
     61 
     62     d += 4; if (d > 360) d = 0;
     63 
     64     // Create a Sine wave for testing, value is in range 0 - 100
     65     float value = 50.0 + 50.0 * sin((d + 0) * 0.0174532925);
     66 
     67     float current;
     68     current = mapValue(value, (float)0.0, (float)100.0, (float)0.0, (float)2.0);
     69     //Serial.print("I = "); Serial.print(current);
     70     amps.updateNeedle(current, 0);
     71 
     72     float voltage;
     73     voltage = mapValue(value, (float)0.0, (float)100.0, (float)0.0, (float)10.0);
     74     //Serial.print(", V = "); Serial.println(voltage);
     75     volts.updateNeedle(voltage, 0);
     76     
     77     float resistance;
     78     resistance = mapValue(value, (float)0.0, (float)100.0, (float)0.0, (float)100.0);
     79     //Serial.print(", R = "); Serial.println(resistance);
     80     ohms.updateNeedle(resistance, 0);
     81   }
     82 }
     83 
     84 float mapValue(float ip, float ipmin, float ipmax, float tomin, float tomax)
     85 {
     86   return tomin + (((tomax - tomin) * (ip - ipmin))/ (ipmax - ipmin));
     87 }