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

TFT_Pie_Chart.ino (2316B)

      1 // This sketch includes a function to draw circle segments
      2 // for pie charts in 1 degree increments
      3 
      4 #include <TFT_eSPI.h> // Hardware-specific library
      5 #include <SPI.h>
      6 
      7 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
      8 
      9 #define DEG2RAD 0.0174532925
     10 
     11 byte inc = 0;
     12 unsigned int col = 0;
     13 
     14 
     15 void setup(void)
     16 {
     17   tft.begin();
     18 
     19   tft.setRotation(1);
     20 
     21   tft.fillScreen(TFT_BLACK);
     22 }
     23 
     24 void loop() {
     25 
     26   // Draw 4 pie chart segments
     27   fillSegment(160, 120, 0, 60, 100, TFT_RED);
     28   fillSegment(160, 120, 60, 30, 100, TFT_GREEN);
     29   fillSegment(160, 120, 60 + 30, 120, 100, TFT_BLUE);
     30   fillSegment(160, 120, 60 + 30 + 120, 150, 100, TFT_YELLOW);
     31 
     32   delay(4000);
     33 
     34   // Erase old chart with 360 degree black plot
     35   fillSegment(160, 120, 0, 360, 100, TFT_BLACK);
     36 }
     37 
     38 
     39 // #########################################################################
     40 // Draw circle segments
     41 // #########################################################################
     42 
     43 // x,y == coords of centre of circle
     44 // start_angle = 0 - 359
     45 // sub_angle   = 0 - 360 = subtended angle
     46 // r = radius
     47 // colour = 16 bit colour value
     48 
     49 int fillSegment(int x, int y, int start_angle, int sub_angle, int r, unsigned int colour)
     50 {
     51   // Calculate first pair of coordinates for segment start
     52   float sx = cos((start_angle - 90) * DEG2RAD);
     53   float sy = sin((start_angle - 90) * DEG2RAD);
     54   uint16_t x1 = sx * r + x;
     55   uint16_t y1 = sy * r + y;
     56 
     57   // Draw colour blocks every inc degrees
     58   for (int i = start_angle; i < start_angle + sub_angle; i++) {
     59 
     60     // Calculate pair of coordinates for segment end
     61     int x2 = cos((i + 1 - 90) * DEG2RAD) * r + x;
     62     int y2 = sin((i + 1 - 90) * DEG2RAD) * r + y;
     63 
     64     tft.fillTriangle(x1, y1, x2, y2, x, y, colour);
     65 
     66     // Copy segment end to sgement start for next segment
     67     x1 = x2;
     68     y1 = y2;
     69   }
     70 }
     71 
     72 
     73 // #########################################################################
     74 // Return the 16 bit colour with brightness 0-100%
     75 // #########################################################################
     76 unsigned int brightness(unsigned int colour, int brightness)
     77 {
     78   byte red   = colour >> 11;
     79   byte green = (colour & 0x7E0) >> 5;
     80   byte blue  = colour & 0x1F;
     81 
     82   blue =  (blue * brightness)/100;
     83   green = (green * brightness)/100;
     84   red =   (red * brightness)/100;
     85 
     86   return (red << 11) + (green << 5) + blue;
     87 }
     88