#define LGFX_AUTODETECT #include LGFX tft; /*\ * * ⚠️ Some of those features use a lot of maths, don't expect FTL performances :-) * * Features: * * - Antialiased thick lines, filled with plain color or gradient * - Antialiased circles, filled with plain color or gradient * - Rectangular fill with linear or radial gradient * * LGFX Methods: * * NOTE: COLOR can be any of uint8_t, uint16_t, uint32_t, RGBColor, rgb888_t, rgb565_t, rgb888_t, etc * * - Draw thick+smooth lines: * void drawWideLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, float thick, COLOR color) * * - Draw thick+smooth lines (gradient fill): * void drawWideLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, float thick, const colors_t gradient) * * - Draw wedge line: * void drawWedgeLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, float thick0, float thick1, COLOR color) * * - Draw wedge line (gradient fill): * void drawWedgeLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, float thick0, float thick1, const colors_t gradient) * * - Fill smooth circle with single color (redundant with fillSmoothCircle(), kept for benchmark): * void drawSpot(int32_t x, int32_t y, float radius, COLOR fg_color) * * - Fill smooth circle, like fillSmoothCircle() but with gradient fill: * void drawGradientSpot(int32_t x, int32_t y, float radius, const colors_t gradient) * * - Draw antialiased line: * void drawSmoothLine( LovyanGFX* gfx, int32_t x0, int32_t y0, int32_t x1, int32_t y1, COLOR color ) * * - Draw gradient line (not smoothed, used for filling): * void drawGradientLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, const colors_t gradient ) * void drawGradientHLine(int32_t x, int32_t y, uint32_t w, const colors_t gradient ) * void drawGradientVLine(int32_t x, int32_t y, uint32_t h, const colors_t gradient ) * * - Fill rect with radial/linear gradient (fill_style_t can be any of RADIAL, HLINEAR or VLINEAR): * void fillGradientRect(int32_t x, int32_t y, uint32_t w, uint32_t h, COLOR start, COLOR end, fill_style_t style=RADIAL ) * void fillGradientRect(int32_t x, int32_t y, uint32_t w, uint32_t h, const colors_t gradient, fill_style_t style=RADIAL ) * * - Create a gradient item from a sized array of colors or from a pointer to an array of colors: * colors_t createGradient( rgb888_t[] colors ) * colors_t createGradient( rgb888_t* colors, uint32_t count ) * * - Example: * lgfx::rgb888_t colors[] = { {0x00,0xff,0x00}, {0xff,0xff,0x00}, {0xff,0x80,0x00}, {0xff,0x00,0x00} }; * auto gradient = tft.createGradient( colors ); // create a gradient item * tft.fillGradientRect( 10, 10, 200, 200, gradient, HLINEAR ); * * \*/ // a couple of colors array for the gradient examples static constexpr const lgfx::rgb888_t heatMap[] = { {0, 0xff, 0}, {0xff, 0xff, 0}, {0xff, 0x80, 0}, {0xff, 0, 0}, {0x80, 0, 0} }; // green => yellow => orange => red => dark red static constexpr const lgfx::rgb888_t greyscale[] = { {0,0,0}, {0xff,0xff,0xff} }; // black => white // a couple of gradient examples based on colors arrays static constexpr const lgfx::colors_t heatMapGradient = { heatMap, sizeof(heatMap)/sizeof(lgfx::rgb888_t) }; static constexpr const lgfx::colors_t greyScaleGradient = { greyscale, sizeof(greyscale)/sizeof(lgfx::rgb888_t) }; // for degrees to radians conversions static constexpr double deg2rad = M_PI/180.0; // some constant values based on display width/height struct constValues_t { const uint32_t middleX = (tft.width()/2)-1; const uint32_t middleY = (tft.height()/2)-1; const uint32_t r1 = std::min(tft.width(),tft.height()) *.39; const uint32_t r2 = r1/5; const uint32_t r3 = std::min(tft.width(),tft.height()) *.45; const uint32_t r4 = std::min(tft.width(),tft.height()) *.37; const uint32_t thick1 = std::min(tft.width(),tft.height())/35; const uint32_t thick2 = std::min(tft.width(),tft.height())/75; const uint32_t thick3 = std::min(tft.width(),tft.height())/150; const uint32_t bw0 = std::max(tft.width(),tft.height())/4; const uint32_t bw1 = std::max(tft.width(),tft.height())/8; const uint32_t max_thickness = std::min(tft.width(),tft.height())/25; const lgfx::rgb888_t colors888[4] = { {0x00,0xff,0x00}, {0xff,0xff,0x00}, {0xff,0x80,0x00}, {0xff,0x00,0x00} }; const lgfx::colors_t gradient888 = tft.createGradient( colors888 ); }; // some random values based on display width/height struct randomValues_t { constValues_t constValues; const uint32_t x0 = random()%tft.width(); const uint32_t y0 = random()%tft.height(); const uint32_t x1 = random()%tft.width(); const uint32_t y1 = random()%tft.height(); const uint32_t thickness0 = (random()%constValues.max_thickness)+2; const uint32_t thickness1 = (random()%constValues.max_thickness)+2; const RGBColor colorstart = RGBColor( uint8_t(rand() % 0xff), uint8_t(rand() % 0xff), uint8_t(rand() % 0xff) ); const RGBColor colorend = RGBColor( uint8_t(rand() % 0xff), uint8_t(rand() % 0xff), uint8_t(rand() % 0xff) ); const lgfx::rgb888_t color24start = rand()%0x1000000u; const lgfx::rgb888_t color24end = rand()%0x1000000u; const lgfx::rgb565_t color16start = rand()%0x10000; const lgfx::rgb565_t color16end = rand()%0x10000; const lgfx::rgb332_t color8start = rand()%256; const lgfx::rgb332_t color8end = rand()%256; const lgfx::rgb888_t colors888[2] = { color24start, color24end }; const lgfx::colors_t gradient888 = tft.createGradient( colors888 ); }; void drawGrid() { randomValues_t v; auto cv = v.constValues; float theta = ( M_PI / 180.0f ) * 22.5; // angle float sin0 = sinf(theta); float cos0 = cosf(theta); float sin1 = sinf(M_PI-theta); float cos1 = cosf(M_PI-theta); for( int32_t y=-tft.height()*2; y=0 && last_y>=0 && (i/15)%2==1 ) { lgfx::rgb888_t colorsRGB[] = { last_color, color }; auto gradientRGB = tft.createGradient( colorsRGB ); tft.drawWedgeLine( last_x, last_y, x2, y2, cv.thick1, cv.thick3, gradientRGB ); } last_color = color; last_x = x2; last_y = y2; } } void setup() { tft.init(); tft.fillScreen(TFT_WHITE); } void loop() { static bool toggler = true; toggler = !toggler; if( toggler ) { runRandomLinesDemo(); vTaskDelay(1000); } else { runGradientDemo(); vTaskDelay(5000); } }