acidportal- 😈 Worlds smallest Evil Portal on a LilyGo T-QT |
git clone git://git.acid.vegas/acidportal.git |
Log | Files | Refs | Archive | README | LICENSE |
TFT_FillArcSpiral.ino (4286B)
1 // This sketch tests a fillArc function that has been adapted to permit the drawing of spirals 2 3 // Sketch also includes (but does not use) a function to change the brightness of a colour 4 5 #include <TFT_eSPI.h> // Hardware-specific library 6 #include <SPI.h> 7 8 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library 9 10 #define DEG2RAD 0.0174532925 11 12 int segment = 0; 13 unsigned int col = 0; 14 int delta = -1; 15 16 byte red = 31; // Red is the top 5 bits of a 16 bit colour value 17 byte green = 0;// Green is the middle 6 bits 18 byte blue = 0; // Blue is the bottom 5 bits 19 byte state = 0; 20 21 void setup(void) { 22 tft.begin(); 23 24 tft.setRotation(1); 25 26 tft.fillScreen(TFT_BLACK); 27 } 28 29 30 void loop() { 31 fillArc(160, 120, segment*6, 1, 120-segment/4, 120-segment/4, 3, rainbow(col)); 32 33 segment+=delta; 34 col+=1; 35 if (col>191) col = 0; 36 if (segment <0) delta = 1; 37 if (segment >298) delta = -1; // ~5 turns in the spiral (300*6 degrees) 38 //delay(5); // Slow drawing down 39 } 40 41 // ######################################################################### 42 // Draw an arc with a defined thickness (modified to aid drawing spirals) 43 // ######################################################################### 44 45 // x,y == coords of centre of arc 46 // start_angle = 0 - 359 47 // seg_count = number of 3 degree segments to draw (120 => 360 degree arc) 48 // rx = x axis radius 49 // yx = y axis radius 50 // w = width (thickness) of arc in pixels 51 // colour = 16 bit colour value 52 // Note if rx and ry are the same an arc of a circle is drawn 53 54 void fillArc(int x, int y, int start_angle, int seg_count, int rx, int ry, int w, unsigned int colour) 55 { 56 57 // Make the segment size 7 degrees to prevent gaps when drawing spirals 58 byte seg = 7; // Angle a single segment subtends (made more than 6 deg. for spiral drawing) 59 byte inc = 6; // Draw segments every 6 degrees 60 61 // Draw colour blocks every inc degrees 62 for (int i = start_angle; i < start_angle + seg * seg_count; i += inc) { 63 // Calculate pair of coordinates for segment start 64 float sx = cos((i - 90) * DEG2RAD); 65 float sy = sin((i - 90) * DEG2RAD); 66 uint16_t x0 = sx * (rx - w) + x; 67 uint16_t y0 = sy * (ry - w) + y; 68 uint16_t x1 = sx * rx + x; 69 uint16_t y1 = sy * ry + y; 70 71 // Calculate pair of coordinates for segment end 72 float sx2 = cos((i + seg - 90) * DEG2RAD); 73 float sy2 = sin((i + seg - 90) * DEG2RAD); 74 int x2 = sx2 * (rx - w) + x; 75 int y2 = sy2 * (ry - w) + y; 76 int x3 = sx2 * rx + x; 77 int y3 = sy2 * ry + y; 78 79 tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour); 80 tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour); 81 } 82 } 83 84 // ######################################################################### 85 // Return a 16 bit colour with brightness 0 - 100% 86 // ######################################################################### 87 unsigned int brightness(unsigned int colour, int brightness) 88 { 89 byte red = colour >> 11; 90 byte green = (colour & 0x7E0) >> 5; 91 byte blue = colour & 0x1F; 92 93 blue = (blue * brightness)/100; 94 green = (green * brightness)/100; 95 red = (red * brightness)/100; 96 97 return (red << 11) + (green << 5) + blue; 98 } 99 100 // ######################################################################### 101 // Return a 16 bit rainbow colour 102 // ######################################################################### 103 unsigned int rainbow(byte value) 104 { 105 // Value is expected to be in range 0-127 106 // The value is converted to a spectrum colour from 0 = blue through to 127 = red 107 108 switch (state) { 109 case 0: 110 green ++; 111 if (green == 64) { 112 green = 63; 113 state = 1; 114 } 115 break; 116 case 1: 117 red--; 118 if (red == 255) { 119 red = 0; 120 state = 2; 121 } 122 break; 123 case 2: 124 blue ++; 125 if (blue == 32) { 126 blue = 31; 127 state = 3; 128 } 129 break; 130 case 3: 131 green --; 132 if (green == 255) { 133 green = 0; 134 state = 4; 135 } 136 break; 137 case 4: 138 red ++; 139 if (red == 32) { 140 red = 31; 141 state = 5; 142 } 143 break; 144 case 5: 145 blue --; 146 if (blue == 255) { 147 blue = 0; 148 state = 0; 149 } 150 break; 151 } 152 return red << 11 | green << 5 | blue; 153 } 154