acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

Smooth_Graphics_Demo.ino (5356B)

      1 // Sketch to demonstrate smooth (anti-aliased) graphics funtions:
      2 // Smooth graphics result in less pixel resolution jaggedness.
      3 
      4 #include <TFT_eSPI.h> // Master copy here: https://github.com/Bodmer/TFT_eSPI
      5 
      6 TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h
      7 
      8 TFT_eSprite spr = TFT_eSprite(&tft);
      9 
     10 // =========================================================================
     11 // Setup
     12 // =========================================================================
     13 void setup() {
     14   Serial.begin(115200);
     15   Serial.println("Booting...");
     16 
     17   // Initialise the screen
     18   tft.init();
     19 
     20   // Ideally set orientation for good viewing angle range because
     21   // the anti-aliasing effectiveness varies with screen viewing angle
     22   tft.setRotation(0);
     23 
     24   tft.fillScreen(TFT_BLACK);
     25 
     26   // Small sprite for spot demo
     27   spr.createSprite(23, 23);
     28 }
     29 
     30 // =========================================================================
     31 // Loop
     32 // =========================================================================
     33 void loop() {
     34 
     35   // drawSpot is for small anti-aliased circles, coordinates and radius are
     36   // floating point to allow sub-pixel positioning (large circles will
     37   // be slow to draw). Use fillSmoothCircle() for large circles.
     38   // In this case black is the backgorund colour for the anti-aliasing
     39   float x = 10.5;
     40   float y = 10.5;
     41   float r = 8.6;
     42   tft.drawSpot(x, y, r, TFT_WHITE, TFT_BLACK);
     43 
     44   // Fill sprite with a colour
     45   spr.fillSprite(TFT_RED);
     46   // Draw spot in sprite, the backgorund colour is ommitted so function
     47   // reads background colour for aliasing. (To use this method with direct write
     48   // to TFT (tft.drawSpot...) requires the capability to read data from the TFT!)
     49   spr.drawSpot(x, y, r, TFT_WHITE);
     50   spr.pushSprite(21, 0);
     51 
     52 
     53   // Draw a segmented ring meter type display
     54   // Centre of screen
     55   int cx = tft.width()  / 2;
     56   int cy = tft.height() / 2;
     57 
     58   // Inner and outer radius of ring
     59   float r1 = min(cx, cy) - 40.0;
     60   float r2 = min(cx, cy) - 10.0;
     61 
     62   // Inner and outer line width
     63   int w1 = r1 / 25;
     64   int w2 = r2 / 20;
     65 
     66   // The following will be updated by the getCoord function
     67   float px1 = 0.0;
     68   float py1 = 0.0;
     69   float px2 = 0.0;
     70   float py2 = 0.0;
     71 
     72   // Wedge line function, an anti-aliased wide line between 2 points, with different
     73   // line widths at the two ends. Background colour is black.
     74   for (int angle = -130; angle <= 130; angle += 10) {
     75     getCoord(cx, cy, &px1, &py1, &px2, &py2, r1, r2, angle);
     76     uint16_t colour = rainbow(map(angle, -130, 130, 0, 127));
     77     if (angle > 45) colour = TFT_DARKGREY;
     78     tft.drawWedgeLine(px1, py1, px2, py2, w1, w2, colour, TFT_BLACK);
     79   }
     80 
     81   // Smooth dark red filled circle
     82   tft.fillSmoothCircle(cx, cy, r1 - 8, TFT_MAROON, TFT_BLACK);
     83 
     84   // Draw a white dial pointer using wedge line function
     85   getCoord(cx, cy, &px1, &py1, &px2, &py2, 0, r1 - 10, 45);
     86   // Magenta wedge line pointer on red background
     87   // Line tapers from radius 5 to zero
     88   tft.drawWedgeLine(cx, cy, px2, py2, 5, 0, TFT_WHITE, TFT_MAROON);
     89 
     90   delay(5000);
     91 
     92   // Test wideLine function
     93   tft.fillScreen(TFT_BLACK);
     94 
     95   // Line width
     96   int wd = 5;
     97 
     98   // Screen limits
     99   int w = tft.width() - wd;
    100   int h = tft.height() - wd;
    101 
    102   // Line end coords
    103   int x1 = w - 1;
    104   int x2 = w - 1;
    105   int y1 = h - 1;
    106   int y2 = wd;
    107 
    108   for (x2 = wd; x2 < w; x2 += wd * 3) tft.drawWideLine(x1, y1, x2, y2, wd, TFT_WHITE, TFT_BLACK);
    109 
    110   x2    = wd;
    111   for (y2 = wd; y2 < h; y2 += wd * 4) tft.drawWideLine(x1, y1, x2, y2, wd, TFT_WHITE, TFT_BLACK);
    112 
    113   delay(5000);
    114 
    115   // Demo filled smooth rounded rectangle
    116   tft.fillScreen(TFT_BLACK);
    117 
    118   x1 = 30;
    119   y1 = 30;
    120   w = tft.width() - 2 * x1;
    121   h = tft.height() - 2 * y1;
    122   int rad = 30;
    123 
    124   tft.fillSmoothRoundRect(x1, y1, w, h, rad, TFT_CYAN, TFT_BLACK);
    125 
    126   // Wait forever
    127   while (1) delay(100);
    128 }
    129 
    130 
    131 // =========================================================================
    132 // Get coordinates of two ends of a line from r1 to r2, pivot at x,y, angle a
    133 // =========================================================================
    134 // Coordinates are returned to caller via the xp and yp pointers
    135 #define DEG2RAD 0.0174532925
    136 void getCoord(int16_t x, int16_t y, float *xp1, float *yp1, float *xp2, float *yp2, int16_t r1, int16_t r2, float a)
    137 {
    138   float sx = cos( (a - 90) * DEG2RAD);
    139   float sy = sin( (a - 90) * DEG2RAD);
    140   *xp1 =  sx * r1 + x;
    141   *yp1 =  sy * r1 + y;
    142   *xp2 =  sx * r2 + x;
    143   *yp2 =  sy * r2 + y;
    144 }
    145 
    146 // =========================================================================
    147 // Return a 16 bit rainbow colour
    148 // =========================================================================
    149 unsigned int rainbow(byte value)
    150 {
    151   // Value is expected to be in range 0-127
    152   // The value is converted to a spectrum colour from 0 = blue through to 127 = red
    153 
    154   byte red = 0; // Red is the top 5 bits of a 16 bit colour value
    155   byte green = 0;// Green is the middle 6 bits
    156   byte blue = 0; // Blue is the bottom 5 bits
    157 
    158   byte quadrant = value / 32;
    159 
    160   if (quadrant == 0) {
    161     blue = 31;
    162     green = 2 * (value % 32);
    163     red = 0;
    164   }
    165   if (quadrant == 1) {
    166     blue = 31 - (value % 32);
    167     green = 63;
    168     red = 0;
    169   }
    170   if (quadrant == 2) {
    171     blue = 0;
    172     green = 63;
    173     red = value % 32;
    174   }
    175   if (quadrant == 3) {
    176     blue = 0;
    177     green = 63 - 2 * (value % 32);
    178     red = 31;
    179   }
    180   return (red << 11) + (green << 5) + blue;
    181 }