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_2.ino (9541B)

      1 /*
      2 
      3   This program provides cartesian type graph function
      4 
      5   Revisions
      6   rev     date        author      description
      7   1       12-24-2015  kasprzak    initial creation
      8 
      9   Updated by Bodmer to be an example for the library here:
     10   https://github.com/Bodmer/TFT_eSPI
     11   
     12 */
     13 
     14 #include <TFT_eSPI.h> // Hardware-specific library
     15 #include <SPI.h>
     16 
     17 TFT_eSPI tft = TFT_eSPI();                   // Invoke custom library with default width and height
     18 
     19 
     20 #define LTBLUE    0xB6DF
     21 #define LTTEAL    0xBF5F
     22 #define LTGREEN   0xBFF7
     23 #define LTCYAN    0xC7FF
     24 #define LTRED     0xFD34
     25 #define LTMAGENTA 0xFD5F
     26 #define LTYELLOW  0xFFF8
     27 #define LTORANGE  0xFE73
     28 #define LTPINK    0xFDDF
     29 #define LTPURPLE  0xCCFF
     30 #define LTGREY    0xE71C
     31 
     32 #define BLUE      0x001F
     33 #define TEAL      0x0438
     34 #define GREEN     0x07E0
     35 #define CYAN      0x07FF
     36 #define RED       0xF800
     37 #define MAGENTA   0xF81F
     38 #define YELLOW    0xFFE0
     39 #define ORANGE    0xFC00
     40 #define PINK      0xF81F
     41 #define PURPLE    0x8010
     42 #define GREY      0xC618
     43 #define WHITE     0xFFFF
     44 #define BLACK     0x0000
     45 
     46 #define DKBLUE    0x000D
     47 #define DKTEAL    0x020C
     48 #define DKGREEN   0x03E0
     49 #define DKCYAN    0x03EF
     50 #define DKRED     0x6000
     51 #define DKMAGENTA 0x8008
     52 #define DKYELLOW  0x8400
     53 #define DKORANGE  0x8200
     54 #define DKPINK    0x9009
     55 #define DKPURPLE  0x4010
     56 #define DKGREY    0x4A49
     57 
     58 // these are the only external variables used by the graph function
     59 // it's a flag to draw the coordinate system only on the first call to the Graph() function
     60 // and will mimize flicker
     61 // also create some variables to store the old x and y, if you draw 2 graphs on the same display
     62 // you will need to store ox and oy per each display
     63 bool display1 = true;
     64 bool update1 = true;
     65 
     66 double ox = -999, oy = -999; // Force them to be off screen
     67 
     68 /*
     69 
     70   function to draw a cartesian coordinate system and plot whatever data you want
     71   just pass x and y and the graph will be drawn
     72 
     73   huge arguement list
     74   &d name of your display object
     75   x = x data point
     76   y = y datapont
     77   gx = x graph location (lower left)
     78   gy = y graph location (lower left)
     79   w = width of graph
     80   h = height of graph
     81   xlo = lower bound of x axis
     82   xhi = upper bound of x asis
     83   xinc = division of x axis (distance not count)
     84   ylo = lower bound of y axis
     85   yhi = upper bound of y asis
     86   yinc = division of y axis (distance not count)
     87   title = title of graph
     88   xlabel = x asis label
     89   ylabel = y asis label
     90   &redraw = flag to redraw graph on first call only
     91   color = plotted trace colour
     92 */
     93 
     94 
     95 void Graph(TFT_eSPI &tft, double x, double y, byte dp,
     96                            double gx, double gy, double w, double h,
     97                            double xlo, double xhi, double xinc,
     98                            double ylo, double yhi, double yinc,
     99                            char *title, char *xlabel, char *ylabel,
    100                            bool &redraw, unsigned int color) {
    101 
    102   double ydiv, xdiv;
    103   double i;
    104   double temp;
    105   int rot, newrot;
    106 
    107   // gcolor = graph grid colors
    108   // acolor = axes line colors
    109   // pcolor = color of your plotted data
    110   // tcolor = text color
    111   // bcolor = background color
    112   unsigned int gcolor = DKBLUE;
    113   unsigned int acolor = RED;
    114   unsigned int pcolor = color;
    115   unsigned int tcolor = WHITE;
    116   unsigned int bcolor = BLACK;
    117 
    118   if (redraw == true) {
    119 
    120     redraw = false;
    121     // initialize old x and old y in order to draw the first point of the graph
    122     // but save the transformed value
    123     // note my transform funcition is the same as the map function, except the map uses long and we need doubles
    124     //ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
    125     //oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
    126 
    127     tft.setTextDatum(MR_DATUM);
    128 
    129     // draw y scale
    130     for ( i = ylo; i <= yhi; i += yinc) {
    131       // compute the transform
    132       temp =  (i - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
    133 
    134       if (i == 0) {
    135         tft.drawLine(gx, temp, gx + w, temp, acolor);
    136         tft.setTextColor(acolor, bcolor);
    137         tft.drawString(xlabel, (int)(gx + w) , (int)temp, 2);
    138       }
    139       else {
    140         tft.drawLine(gx, temp, gx + w, temp, gcolor);
    141       }
    142       // draw the axis labels
    143       tft.setTextColor(tcolor, bcolor);
    144       // precision is default Arduino--this could really use some format control
    145       tft.drawFloat(i, dp, gx - 4, temp, 1);
    146     }
    147 
    148     // draw x scale
    149     for (i = xlo; i <= xhi; i += xinc) {
    150 
    151       // compute the transform
    152       temp =  (i - xlo) * ( w) / (xhi - xlo) + gx;
    153       if (i == 0) {
    154         tft.drawLine(temp, gy, temp, gy - h, acolor);
    155         tft.setTextColor(acolor, bcolor);
    156         tft.setTextDatum(BC_DATUM);
    157         tft.drawString(ylabel, (int)temp, (int)(gy - h - 8) , 2);
    158       }
    159       else {
    160         tft.drawLine(temp, gy, temp, gy - h, gcolor);
    161       }
    162       // draw the axis labels
    163       tft.setTextColor(tcolor, bcolor);
    164       tft.setTextDatum(TC_DATUM);
    165       // precision is default Arduino--this could really use some format control
    166       tft.drawFloat(i, dp, temp, gy + 7, 1);
    167     }
    168 
    169     //now draw the graph labels
    170     tft.setTextColor(tcolor, bcolor);
    171     tft.drawString(title, (int)(gx + w / 2) , (int)(gy - h - 30), 4);
    172   }
    173 
    174   // the coordinates are now drawn, plot the data
    175   // the entire plotting code are these few lines...
    176   // recall that ox and oy are initialized above
    177   //x =  (x - xlo) * ( w) / (xhi - xlo) + gx;
    178   //y =  (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
    179   //tft.drawLine(ox, oy, x, y, pcolor);
    180   // it's up to you but drawing 2 more lines to give the graph some thickness
    181   //tft.drawLine(ox, oy + 1, x, y + 1, pcolor);
    182   //tft.drawLine(ox, oy - 1, x, y - 1, pcolor);
    183   //ox = x;
    184   //oy = y;
    185 
    186 }
    187 
    188 void Trace(TFT_eSPI &tft, double x,  double y,  byte dp,
    189            double gx, double gy,
    190            double w, double h,
    191            double xlo, double xhi, double xinc,
    192            double ylo, double yhi, double yinc,
    193            char *title, char *xlabel, char *ylabel,
    194            bool &update1, unsigned int color)
    195 {
    196   double ydiv, xdiv;
    197   double i;
    198   double temp;
    199   int rot, newrot;
    200 
    201   //unsigned int gcolor = DKBLUE;   // gcolor = graph grid color
    202   unsigned int acolor = RED;        // acolor = main axes and label color
    203   unsigned int pcolor = color;      // pcolor = color of your plotted data
    204   unsigned int tcolor = WHITE;      // tcolor = text color
    205   unsigned int bcolor = BLACK;      // bcolor = background color
    206 
    207   // initialize old x and old y in order to draw the first point of the graph
    208   // but save the transformed value
    209   // note my transform funcition is the same as the map function, except the map uses long and we need doubles
    210   if (update1) {
    211     update1 = false;
    212     
    213     ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
    214     oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
    215 
    216     if ((ox < gx) || (ox > gx+w)) {update1 = true; return;}
    217     if ((oy < gy-h) || (oy > gy)) {update1 = true; return;}
    218     
    219 
    220     tft.setTextDatum(MR_DATUM);
    221 
    222     // draw y scale
    223     for ( i = ylo; i <= yhi; i += yinc) {
    224       // compute the transform
    225       temp =  (i - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
    226 
    227       if (i == 0) {
    228         tft.setTextColor(acolor, bcolor);
    229         tft.drawString(xlabel, (int)(gx + w) , (int)temp, 2);
    230       }
    231       // draw the axis labels
    232       tft.setTextColor(tcolor, bcolor);
    233       // precision is default Arduino--this could really use some format control
    234       tft.drawFloat(i, dp, gx - 4, temp, 1);
    235     }
    236 
    237     // draw x scale
    238     for (i = xlo; i <= xhi; i += xinc) {
    239 
    240       // compute the transform
    241       temp =  (i - xlo) * ( w) / (xhi - xlo) + gx;
    242       if (i == 0) {
    243         tft.setTextColor(acolor, bcolor);
    244         tft.setTextDatum(BC_DATUM);
    245         tft.drawString(ylabel, (int)temp, (int)(gy - h - 8) , 2);
    246       }
    247 
    248       // draw the axis labels
    249       tft.setTextColor(tcolor, bcolor);
    250       tft.setTextDatum(TC_DATUM);
    251       // precision is default Arduino--this could really use some format control
    252       tft.drawFloat(i, dp, temp, gy + 7, 1);
    253     }
    254 
    255     //now draw the graph labels
    256     tft.setTextColor(tcolor, bcolor);
    257     tft.drawString(title, (int)(gx + w / 2) , (int)(gy - h - 30), 4);
    258   }
    259 
    260   // the coordinates are now drawn, plot the data
    261   // the entire plotting code are these few lines...
    262   // recall that ox and oy are initialized above
    263   x =  (x - xlo) * ( w) / (xhi - xlo) + gx;
    264   y =  (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
    265 
    266   if ((x < gx) || (x > gx+w)) {update1 = true; return;}
    267   if ((y < gy-h) || (y > gy)) {update1 = true; return;}
    268     
    269     
    270   tft.drawLine(ox, oy, x, y, pcolor);
    271   // it's up to you but drawing 2 more lines to give the graph some thickness
    272   //tft.drawLine(ox, oy + 1, x, y + 1, pcolor);
    273   //tft.drawLine(ox, oy - 1, x, y - 1, pcolor);
    274   ox = x;
    275   oy = y;
    276 
    277 }
    278 
    279 /*
    280 
    281   End of graphing function
    282 
    283 */
    284 
    285 
    286 
    287 void setup() {
    288   double x, y;
    289 
    290   tft.begin();
    291   tft.fillScreen(BLACK);
    292   tft.setRotation(1);
    293 
    294   Graph(tft, x, y, 1, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, .25, "", "", "", display1, YELLOW);
    295 
    296   //delay(1000);
    297   
    298   update1 = true;
    299   for (x = 0; x <= 6.3; x += .1) {
    300     y = sin(x);
    301     Trace(tft, x, y, 1, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, .25, "Sin(x)", "x", "fn(x)", update1, YELLOW);
    302   }
    303 
    304   delay(2000);
    305 
    306   update1 = true;
    307   for (x = 0; x <= 6.3; x += .1) {
    308     y = cos(x);
    309     Trace(tft, x, y, 1, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, .25, "Sin(x) + Cos(x)", " x", "fn(x)", update1, TFT_PINK);
    310   }
    311 
    312   delay(2000);
    313   
    314   update1 = true;
    315   for (x = 0; x <= 6.3; x += .02) {
    316     y = tan(x);
    317     Trace(tft, x, y, 1, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, .25, "Sin(x) + Cos(x) + Tan(x)", " x", "fn(x)", update1, CYAN);
    318   }
    319 }
    320 
    321 
    322 void loop(void) {
    323 
    324 }
    325 
    326 
    327 
    328