acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Graph_demo_2.ino (3255B)

      1 // Demonstrate graph widget functions with two independant trace instances
      2 // Multiple traces can be drawn at a time with multiple trace instances
      3 // Note: Traces are automatically clipped at graph boundaries by widget library
      4 
      5 // Requires widget library here:
      6 // https://github.com/Bodmer/TFT_eWidget
      7 
      8 #include <TFT_eSPI.h>
      9 TFT_eSPI tft = TFT_eSPI();
     10 
     11 #include <TFT_eWidget.h>               // Widget library
     12 
     13 GraphWidget gr = GraphWidget(&tft);    // Graph widget
     14 
     15 // Traces are drawn on tft using graph instance
     16 TraceWidget tr1 = TraceWidget(&gr);    // Graph trace 1
     17 TraceWidget tr2 = TraceWidget(&gr);    // Graph trace 2
     18 
     19 void setup() {
     20   Serial.begin(115200);
     21   delay(5000);
     22   tft.begin();
     23   tft.setRotation(3);
     24   tft.fillScreen(TFT_BLACK);
     25 
     26   // Graph area is 200 pixels wide, 150 high, dark grey background
     27   gr.createGraph(200, 150, tft.color565(5, 5, 5));
     28 
     29   // x scale units is from 0 to 100, y scale units is -50 to 50
     30   gr.setGraphScale(0.0, 100.0, -50.0, 50.0);
     31 
     32   // X grid starts at 0 with lines every 10 x-scale units
     33   // Y grid starts at -50 with lines every 25 y-scale units
     34   // blue grid
     35   gr.setGraphGrid(0.0, 10.0, -50.0, 25.0, TFT_BLUE);
     36 
     37   // Draw empty graph, top left corner at 40,10 on TFT
     38   gr.drawGraph(40, 10);
     39 
     40   // Start a trace with using red and another with green
     41   tr1.startTrace(TFT_RED);
     42   tr2.startTrace(TFT_GREEN);
     43 
     44   // Add points on graph to trace 1 using graph scale factors
     45   tr1.addPoint(0.0, 0.0);
     46   tr1.addPoint(100.0, 0.0);
     47 
     48   // Add points on graph to trace 2 using graph scale factors
     49   // Points are off graph so the plotted line is clipped to graph area
     50   tr2.addPoint(0.0, -100.0);
     51   tr2.addPoint(100.0, 100.0);
     52 
     53   // Get x,y pixel coordinates of any scaled point on graph
     54   // and ring that point.
     55   tft.drawCircle(gr.getPointX(50.0), gr.getPointY(0.0), 5, TFT_MAGENTA);
     56 
     57   // Draw the x axis scale
     58   tft.setTextDatum(TC_DATUM); // Top centre text datum
     59   tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(-50.0) + 3);
     60   tft.drawNumber(50, gr.getPointX(50.0), gr.getPointY(-50.0) + 3);
     61   tft.drawNumber(100, gr.getPointX(100.0), gr.getPointY(-50.0) + 3);
     62 
     63   // Draw the y axis scale
     64   tft.setTextDatum(MR_DATUM); // Middle right text datum
     65   tft.drawNumber(-50, gr.getPointX(0.0), gr.getPointY(-50.0));
     66   tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(0.0));
     67   tft.drawNumber(50, gr.getPointX(0.0), gr.getPointY(50.0));
     68 
     69   // Restart traces with new colours
     70   tr1.startTrace(TFT_WHITE);
     71   tr2.startTrace(TFT_YELLOW);
     72 }
     73 
     74 void loop() {
     75   static uint32_t plotTime = millis();
     76   static float gx = 0.0, gy = 0.0;
     77   static float delta = 7.0;
     78 
     79   // Sample periodically
     80   if (millis() - plotTime >= 100) {
     81     plotTime = millis();
     82 
     83     // Add a new point on each trace
     84     tr1.addPoint(gx, gy);
     85     tr2.addPoint(gx, gy/2.0); // half y amplitude
     86 
     87     // Create next plot point
     88     gx += 1.0;
     89     gy += delta;
     90     if (gy >  70.0) { delta = -7.0; gy =  70.0; }
     91     if (gy < -70.0) { delta =  7.0; gy = -70.0; }
     92 
     93     // If the end of the graph is reached start 2 new traces
     94     if (gx > 100.0) {
     95       gx = 0.0;
     96       gy = 0.0;
     97 
     98       // Draw empty graph at 40,10 on display
     99       gr.drawGraph(40, 10);
    100       // Start new trace
    101       tr1.startTrace(TFT_GREEN);
    102       tr2.startTrace(TFT_YELLOW);
    103     }
    104   }
    105 }