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_ArcFill.ino (4351B)

      1 // Demo using arcFill to draw ellipses and a segmented elipse
      2 #include <TFT_eSPI.h> // Hardware-specific library
      3 #include <SPI.h>
      4 
      5 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
      6 
      7 #define DEG2RAD 0.0174532925
      8 
      9 #define LOOP_DELAY 10 // Loop delay to slow things down
     10 
     11 byte inc = 0;
     12 unsigned int col = 0;
     13 
     14 byte red = 31; // Red is the top 5 bits of a 16 bit colour value
     15 byte green = 0;// Green is the middle 6 bits
     16 byte blue = 0; // Blue is the bottom 5 bits
     17 byte state = 0;
     18 
     19 void setup(void) {
     20   tft.begin();
     21 
     22   tft.setRotation(1);
     23 
     24   tft.fillScreen(TFT_BLACK);
     25 
     26 }
     27 
     28 
     29 void loop() {
     30 
     31   // Continuous elliptical arc drawing
     32   fillArc(160, 120, inc * 6, 1, 140, 100, 10, rainbow(col));
     33 
     34   // Continuous segmented (inc*2) elliptical arc drawing
     35   fillArc(160, 120, ((inc * 2) % 60) * 6, 1, 120, 80, 30, rainbow(col));
     36 
     37   // Circle drawing using arc with arc width = radius
     38   fillArc(160, 120, inc * 6, 1, 42, 42, 42, rainbow(col));
     39 
     40   inc++;
     41   col += 1;
     42   if (col > 191) col = 0;
     43   if (inc > 59) inc = 0;
     44 
     45   delay(LOOP_DELAY);
     46 }
     47 
     48 
     49 // #########################################################################
     50 // Draw a circular or elliptical arc with a defined thickness
     51 // #########################################################################
     52 
     53 // x,y == coords of centre of arc
     54 // start_angle = 0 - 359
     55 // seg_count = number of 6 degree segments to draw (60 => 360 degree arc)
     56 // rx = x axis outer radius
     57 // ry = y axis outer radius
     58 // w  = width (thickness) of arc in pixels
     59 // colour = 16 bit colour value
     60 // Note if rx and ry are the same then an arc of a circle is drawn
     61 
     62 void fillArc(int x, int y, int start_angle, int seg_count, int rx, int ry, int w, unsigned int colour)
     63 {
     64 
     65   byte seg = 6; // Segments are 3 degrees wide = 120 segments for 360 degrees
     66   byte inc = 6; // Draw segments every 3 degrees, increase to 6 for segmented ring
     67 
     68   // Calculate first pair of coordinates for segment start
     69   float sx = cos((start_angle - 90) * DEG2RAD);
     70   float sy = sin((start_angle - 90) * DEG2RAD);
     71   uint16_t x0 = sx * (rx - w) + x;
     72   uint16_t y0 = sy * (ry - w) + y;
     73   uint16_t x1 = sx * rx + x;
     74   uint16_t y1 = sy * ry + y;
     75 
     76   // Draw colour blocks every inc degrees
     77   for (int i = start_angle; i < start_angle + seg * seg_count; i += inc) {
     78 
     79     // Calculate pair of coordinates for segment end
     80     float sx2 = cos((i + seg - 90) * DEG2RAD);
     81     float sy2 = sin((i + seg - 90) * DEG2RAD);
     82     int x2 = sx2 * (rx - w) + x;
     83     int y2 = sy2 * (ry - w) + y;
     84     int x3 = sx2 * rx + x;
     85     int y3 = sy2 * ry + y;
     86 
     87     tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour);
     88     tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour);
     89 
     90     // Copy segment end to sgement start for next segment
     91     x0 = x2;
     92     y0 = y2;
     93     x1 = x3;
     94     y1 = y3;
     95   }
     96 }
     97 
     98 // #########################################################################
     99 // Return the 16 bit colour with brightness 0-100%
    100 // #########################################################################
    101 unsigned int brightness(unsigned int colour, int brightness)
    102 {
    103   byte red   = colour >> 11;
    104   byte green = (colour & 0x7E0) >> 5;
    105   byte blue  = colour & 0x1F;
    106 
    107   blue =  (blue * brightness) / 100;
    108   green = (green * brightness) / 100;
    109   red =   (red * brightness) / 100;
    110 
    111   return (red << 11) + (green << 5) + blue;
    112 }
    113 
    114 // #########################################################################
    115 // Return a 16 bit rainbow colour
    116 // #########################################################################
    117 unsigned int rainbow(byte value)
    118 {
    119   // Value is expected to be in range 0-127
    120   // The value is converted to a spectrum colour from 0 = blue through to 127 = red
    121 
    122   switch (state) {
    123     case 0:
    124       green ++;
    125       if (green == 64) {
    126         green = 63;
    127         state = 1;
    128       }
    129       break;
    130     case 1:
    131       red--;
    132       if (red == 255) {
    133         red = 0;
    134         state = 2;
    135       }
    136       break;
    137     case 2:
    138       blue ++;
    139       if (blue == 32) {
    140         blue = 31;
    141         state = 3;
    142       }
    143       break;
    144     case 3:
    145       green --;
    146       if (green == 255) {
    147         green = 0;
    148         state = 4;
    149       }
    150       break;
    151     case 4:
    152       red ++;
    153       if (red == 32) {
    154         red = 31;
    155         state = 5;
    156       }
    157       break;
    158     case 5:
    159       blue --;
    160       if (blue == 255) {
    161         blue = 0;
    162         state = 0;
    163       }
    164       break;
    165   }
    166   return red << 11 | green << 5 | blue;
    167 }
    168