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_1.ino (2362B)

      1 // Demonstrate graph widget functions with a single trace instance
      2 // One trace can be drawn at a time with one trace instance
      3 
      4 // Requires widget library here:
      5 // https://github.com/Bodmer/TFT_eWidget
      6 
      7 #include <TFT_eSPI.h>
      8 TFT_eSPI tft = TFT_eSPI();
      9 
     10 #include <TFT_eWidget.h>               // Widget library
     11 
     12 GraphWidget gr = GraphWidget(&tft);    // Graph widget gr instance with pointer to tft
     13 TraceWidget tr = TraceWidget(&gr);     // Graph trace tr with pointer to gr
     14 
     15 const float gxLow  = 0.0;
     16 const float gxHigh = 100.0;
     17 const float gyLow  = -512.0;
     18 const float gyHigh = 512.0;
     19 
     20 void setup() {
     21   Serial.begin(115200);
     22 
     23   tft.begin();
     24   tft.setRotation(3);
     25   tft.fillScreen(TFT_BLACK);
     26 
     27   // Graph area is 200 pixels wide, 150 pixels high, dark grey background
     28   gr.createGraph(200, 150, tft.color565(5, 5, 5));
     29 
     30   // x scale units is from 0 to 100, y scale units is -512 to 512
     31   gr.setGraphScale(gxLow, gxHigh, gyLow, gyHigh);
     32 
     33   // X grid starts at 0 with lines every 20 x-scale units
     34   // Y grid starts at -512 with lines every 64 y-scale units
     35   // blue grid
     36   gr.setGraphGrid(gxLow, 20.0, gyLow, 64.0, TFT_BLUE);
     37 
     38   // Draw empty graph, top left corner at pixel coordinate 40,10 on TFT
     39   gr.drawGraph(40, 10);
     40 
     41   // Start a trace with using red, trace points are in x and y scale units
     42   // In this example a horizontal line is drawn
     43   tr.startTrace(TFT_RED);
     44   // Add a trace point at 0.0,0.0 on graph
     45   tr.addPoint(0.0, 0.0);
     46   // Add another point at 100.0, 0.0 this will be joined via line to the last point added
     47   tr.addPoint(100.0, 0.0);
     48 
     49   // Start a new trace with using white
     50   tr.startTrace(TFT_WHITE);
     51 }
     52 
     53 void loop() {
     54   static uint32_t plotTime = millis();
     55   static float gx = 0.0, gy = 0.0;
     56   static float delta = 10.0;
     57 
     58   // Create a new plot point every 100ms
     59   if (millis() - plotTime >= 100) {
     60     plotTime = millis();
     61 
     62     // Add a plot, first point in a trace will be a single pixel (if within graph area)
     63     tr.addPoint(gx, gy);
     64     gx += 1.0;
     65     if (gy >  500.0) delta = -10.0;
     66     if (gy < -500.0) delta =  10.0;
     67     gy += delta;
     68 
     69     // If the end of the graph x ais is reached start a new trace at 0.0,0.0
     70     if (gx > gxHigh) {
     71       gx = 0.0;
     72       gy = 0.0;
     73 
     74       // Draw empty graph at 40,10 on display to clear old one
     75       gr.drawGraph(40, 10);
     76       // Start new trace
     77       tr.startTrace(TFT_GREEN);
     78     }
     79   }
     80 }