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

MultiplePDQgraphicstest.ino (15163B)

      1 /*
      2   Adapted from the Adafruit and Xark's PDQ graphicstest sketch.
      3 
      4   See end of file for original header text and MIT license info.
      5 */
      6 
      7 /*******************************************************************************
      8  * Start of Arduino_GFX setting
      9  ******************************************************************************/
     10 #include <Arduino_GFX_Library.h>
     11 
     12 /* all display share same SPI Data Bus with individual CS, RST pins connected to MCU RST pin */
     13 Arduino_DataBus *bus1 = new Arduino_HWSPI(DF_GFX_DC, 21 /* CS */);
     14 Arduino_GFX *gfx1 = new Arduino_SSD1331(bus1, GFX_NOT_DEFINED /* RST */, 0 /* rotation */);
     15 
     16 Arduino_DataBus *bus2 = new Arduino_HWSPI(DF_GFX_DC, 32 /* CS */);
     17 Arduino_GFX *gfx2 = new Arduino_ST7735(bus2, GFX_NOT_DEFINED /* RST */, 3 /* rotation */, true /* IPS */, 80 /* width */, 160 /* height */, 26 /* col offset 1 */, 1 /* row offset 1 */, 26 /* col offset 2 */, 1 /* row offset 2 */);
     18 
     19 Arduino_DataBus *bus3 = new Arduino_HWSPI(DF_GFX_DC, 22 /* CS */);
     20 Arduino_GFX *gfx3 = new Arduino_ST7789(bus3, GFX_NOT_DEFINED /* RST */, 0 /* rotation */, true /* IPS */, 240 /* width */, 240 /* height */, 0 /* col offset 1 */, 0 /* row offset 1 */, 0 /* col offset 2 */, 80 /* row offset 2 */);
     21 
     22 Arduino_DataBus *bus4 = new Arduino_HWSPI(DF_GFX_DC, 5 /* CS */);
     23 Arduino_GFX *gfx4 = new Arduino_ILI9341(bus4, GFX_NOT_DEFINED /* RST */, 0 /* rotation */, false /* IPS */);
     24 /*******************************************************************************
     25  * End of Arduino_GFX setting
     26  ******************************************************************************/
     27 
     28 Arduino_GFX *gfx = gfx1;
     29 int32_t w, h, n, n1, cx, cy, cx1, cy1, cn, cn1;
     30 uint8_t tsa, tsb, tsc, ds;
     31 
     32 void setup()
     33 {
     34   Serial.begin(115200);
     35   // Serial.setDebugOutput(true);
     36   // while(!Serial);
     37   Serial.println("Arduino_GFX library Multiple Device Test!");
     38 
     39 #ifdef GFX_EXTRA_PRE_INIT
     40   GFX_EXTRA_PRE_INIT();
     41 #endif
     42 
     43   gfx1->begin();
     44   gfx1->fillScreen(RED);
     45   delay(200);
     46 
     47   gfx2->begin();
     48   gfx2->fillScreen(YELLOW);
     49   delay(200);
     50 
     51   gfx3->begin();
     52   gfx3->fillScreen(GREEN);
     53   delay(200);
     54 
     55   gfx4->begin();
     56   gfx4->fillScreen(BLUE);
     57   delay(200);
     58 }
     59 
     60 void loop()
     61 {
     62   test();
     63 
     64   if (gfx == gfx1)
     65   {
     66     gfx = gfx2;
     67   }
     68   else if (gfx == gfx2)
     69   {
     70     gfx = gfx3;
     71   }
     72   else if (gfx == gfx3)
     73   {
     74     gfx = gfx4;
     75   }
     76   else // gfx == gfx4
     77   {
     78     gfx = gfx1;
     79   }
     80   delay(200);
     81 }
     82 
     83 static inline uint32_t micros_start() __attribute__((always_inline));
     84 static inline uint32_t micros_start()
     85 {
     86   uint8_t oms = millis();
     87   while ((uint8_t)millis() == oms)
     88     ;
     89   return micros();
     90 }
     91 
     92 void test()
     93 {
     94   w = gfx->width();
     95   h = gfx->height();
     96   n = min(w, h);
     97   n1 = n - 1;
     98   cx = w / 2;
     99   cy = h / 2;
    100   cx1 = cx - 1;
    101   cy1 = cy - 1;
    102   cn = min(cx1, cy1);
    103   cn1 = cn - 1;
    104   tsa = ((w <= 176) || (h <= 160)) ? 1 : (((w <= 240) || (h <= 240)) ? 2 : 3); // text size A
    105   tsb = ((w <= 240) || (h <= 220)) ? 1 : 2;                                    // text size B
    106   tsc = ((w <= 220) || (h <= 220)) ? 1 : 2;                                    // text size C
    107   ds = (w <= 160) ? 9 : 12;                                                    // digit size
    108 
    109   Serial.println(F("Benchmark\tmicro-secs"));
    110 
    111   int32_t usecFillScreen = testFillScreen();
    112   serialOut(F("Screen fill\t"), usecFillScreen, 100, true);
    113 
    114   int32_t usecText = testText();
    115   serialOut(F("Text\t"), usecText, 3000, true);
    116 
    117   int32_t usecPixels = testPixels();
    118   serialOut(F("Pixels\t"), usecPixels, 100, true);
    119 
    120   int32_t usecLines = testLines();
    121   serialOut(F("Lines\t"), usecLines, 100, true);
    122 
    123   int32_t usecFastLines = testFastLines();
    124   serialOut(F("Horiz/Vert Lines\t"), usecFastLines, 100, true);
    125 
    126   int32_t usecFilledRects = testFilledRects();
    127   serialOut(F("Rectangles (filled)\t"), usecFilledRects, 100, false);
    128 
    129   int32_t usecRects = testRects();
    130   serialOut(F("Rectangles (outline)\t"), usecRects, 100, true);
    131 
    132   int32_t usecFilledTrangles = testFilledTriangles();
    133   serialOut(F("Triangles (filled)\t"), usecFilledTrangles, 100, false);
    134 
    135   int32_t usecTriangles = testTriangles();
    136   serialOut(F("Triangles (outline)\t"), usecTriangles, 100, true);
    137 
    138   int32_t usecFilledCircles = testFilledCircles(10);
    139   serialOut(F("Circles (filled)\t"), usecFilledCircles, 100, false);
    140 
    141   int32_t usecCircles = testCircles(10);
    142   serialOut(F("Circles (outline)\t"), usecCircles, 100, true);
    143 
    144   int32_t usecFilledArcs = testFillArcs();
    145   serialOut(F("Arcs (filled)\t"), usecFilledArcs, 100, false);
    146 
    147   int32_t usecArcs = testArcs();
    148   serialOut(F("Arcs (outline)\t"), usecArcs, 100, true);
    149 
    150   int32_t usecFilledRoundRects = testFilledRoundRects();
    151   serialOut(F("Rounded rects (filled)\t"), usecFilledRoundRects, 100, false);
    152 
    153   int32_t usecRoundRects = testRoundRects();
    154   serialOut(F("Rounded rects (outline)\t"), usecRoundRects, 100, true);
    155 
    156   Serial.println(F("Done!"));
    157 
    158   uint16_t c = 4;
    159   int8_t d = 1;
    160   for (int32_t i = 0; i < h; i++)
    161   {
    162     gfx->drawFastHLine(0, i, w, c);
    163     c += d;
    164     if (c <= 4 || c >= 11)
    165     {
    166       d = -d;
    167     }
    168   }
    169 
    170   gfx->setCursor(0, 0);
    171 
    172   gfx->setTextSize(tsa);
    173   gfx->setTextColor(MAGENTA);
    174   gfx->println(F("Arduino GFX PDQ"));
    175 
    176   if (h > w)
    177   {
    178     gfx->setTextSize(tsb);
    179     gfx->setTextColor(GREEN);
    180     gfx->print(F("\nBenchmark "));
    181     gfx->setTextSize(tsc);
    182     if (ds == 12)
    183     {
    184       gfx->print(F("   "));
    185     }
    186     gfx->println(F("micro-secs"));
    187   }
    188 
    189   gfx->setTextSize(1);
    190   printnice(F("Screen fill "), usecFillScreen);
    191   printnice(F("Text        "), usecText);
    192   printnice(F("Pixels      "), usecPixels);
    193   printnice(F("Lines       "), usecLines);
    194   printnice(F("H/V Lines   "), usecFastLines);
    195   printnice(F("Rectangles F"), usecFilledRects);
    196   printnice(F("Rectangles  "), usecRects);
    197   printnice(F("Triangles F "), usecFilledTrangles);
    198   printnice(F("Triangles   "), usecTriangles);
    199   printnice(F("Circles F   "), usecFilledCircles);
    200   printnice(F("Circles     "), usecCircles);
    201   printnice(F("Arcs F      "), usecFilledArcs);
    202   printnice(F("Arcs        "), usecArcs);
    203   printnice(F("RoundRects F"), usecFilledRoundRects);
    204   printnice(F("RoundRects  "), usecRoundRects);
    205 
    206   if ((h > w) || (h > 240))
    207   {
    208     gfx->setTextSize(tsc);
    209     gfx->setTextColor(GREEN);
    210     gfx->print(F("\nBenchmark Complete!"));
    211   }
    212 }
    213 
    214 void serialOut(const __FlashStringHelper *item, int32_t v, uint32_t d, bool clear)
    215 {
    216 #ifdef CANVAS
    217   gfx->flush();
    218 #endif
    219   Serial.print(item);
    220   if (v < 0)
    221   {
    222     Serial.println(F("N/A"));
    223   }
    224   else
    225   {
    226     Serial.println(v);
    227   }
    228   delay(d);
    229   if (clear)
    230   {
    231     gfx->fillScreen(BLACK);
    232   }
    233 }
    234 
    235 void printnice(const __FlashStringHelper *item, long int v)
    236 {
    237   gfx->setTextSize(tsb);
    238   gfx->setTextColor(CYAN);
    239   gfx->print(item);
    240 
    241   gfx->setTextSize(tsc);
    242   gfx->setTextColor(YELLOW);
    243   if (v < 0)
    244   {
    245     gfx->println(F("      N / A"));
    246   }
    247   else
    248   {
    249     char str[32] = {0};
    250 #ifdef RTL8722DM
    251     sprintf(str, "%d", (int)v);
    252 #else
    253     sprintf(str, "%ld", v);
    254 #endif
    255     for (char *p = (str + strlen(str)) - 3; p > str; p -= 3)
    256     {
    257       memmove(p + 1, p, strlen(p) + 1);
    258       *p = ',';
    259     }
    260     while (strlen(str) < ds)
    261     {
    262       memmove(str + 1, str, strlen(str) + 1);
    263       *str = ' ';
    264     }
    265     gfx->println(str);
    266   }
    267 }
    268 
    269 int32_t testFillScreen()
    270 {
    271   uint32_t start = micros_start();
    272   // Shortened this tedious test!
    273   gfx->fillScreen(WHITE);
    274   gfx->fillScreen(RED);
    275   gfx->fillScreen(GREEN);
    276   gfx->fillScreen(BLUE);
    277   gfx->fillScreen(BLACK);
    278 
    279   return micros() - start;
    280 }
    281 
    282 int32_t testText()
    283 {
    284   uint32_t start = micros_start();
    285   gfx->setCursor(0, 0);
    286 
    287   gfx->setTextSize(1);
    288   gfx->setTextColor(WHITE, BLACK);
    289   gfx->println(F("Hello World!"));
    290 
    291   gfx->setTextSize(2);
    292   gfx->setTextColor(gfx->color565(0xff, 0x00, 0x00));
    293   gfx->print(F("RED "));
    294   gfx->setTextColor(gfx->color565(0x00, 0xff, 0x00));
    295   gfx->print(F("GREEN "));
    296   gfx->setTextColor(gfx->color565(0x00, 0x00, 0xff));
    297   gfx->println(F("BLUE"));
    298 
    299   gfx->setTextSize(tsa);
    300   gfx->setTextColor(YELLOW);
    301   gfx->println(1234.56);
    302 
    303   gfx->setTextColor(WHITE);
    304   gfx->println((w > 128) ? 0xDEADBEEF : 0xDEADBEE, HEX);
    305 
    306   gfx->setTextColor(CYAN, WHITE);
    307   gfx->println(F("Groop,"));
    308 
    309   gfx->setTextSize(tsc);
    310   gfx->setTextColor(MAGENTA, WHITE);
    311   gfx->println(F("I implore thee,"));
    312 
    313   gfx->setTextSize(1);
    314   gfx->setTextColor(NAVY, WHITE);
    315   gfx->println(F("my foonting turlingdromes."));
    316 
    317   gfx->setTextColor(DARKGREEN, WHITE);
    318   gfx->println(F("And hooptiously drangle me"));
    319 
    320   gfx->setTextColor(DARKCYAN, WHITE);
    321   gfx->println(F("with crinkly bindlewurdles,"));
    322 
    323   gfx->setTextColor(MAROON, WHITE);
    324   gfx->println(F("Or I will rend thee"));
    325 
    326   gfx->setTextColor(PURPLE, WHITE);
    327   gfx->println(F("in the gobberwartsb"));
    328 
    329   gfx->setTextColor(OLIVE, WHITE);
    330   gfx->println(F("with my blurglecruncheon,"));
    331 
    332   gfx->setTextColor(DARKGREY, WHITE);
    333   gfx->println(F("see if I don't!"));
    334 
    335   gfx->setTextSize(2);
    336   gfx->setTextColor(RED);
    337   gfx->println(F("Size 2"));
    338 
    339   gfx->setTextSize(3);
    340   gfx->setTextColor(ORANGE);
    341   gfx->println(F("Size 3"));
    342 
    343   gfx->setTextSize(4);
    344   gfx->setTextColor(YELLOW);
    345   gfx->println(F("Size 4"));
    346 
    347   gfx->setTextSize(5);
    348   gfx->setTextColor(GREENYELLOW);
    349   gfx->println(F("Size 5"));
    350 
    351   gfx->setTextSize(6);
    352   gfx->setTextColor(GREEN);
    353   gfx->println(F("Size 6"));
    354 
    355   gfx->setTextSize(7);
    356   gfx->setTextColor(BLUE);
    357   gfx->println(F("Size 7"));
    358 
    359   gfx->setTextSize(8);
    360   gfx->setTextColor(PURPLE);
    361   gfx->println(F("Size 8"));
    362 
    363   gfx->setTextSize(9);
    364   gfx->setTextColor(PINK);
    365   gfx->println(F("Size 9"));
    366 
    367   return micros() - start;
    368 }
    369 
    370 int32_t testPixels()
    371 {
    372   uint32_t start = micros_start();
    373 
    374   for (int16_t y = 0; y < h; y++)
    375   {
    376     for (int16_t x = 0; x < w; x++)
    377     {
    378       gfx->drawPixel(x, y, gfx->color565(x << 3, y << 3, x * y));
    379     }
    380 #ifdef ESP8266
    381     yield(); // avoid long run triggered ESP8266 WDT restart
    382 #endif
    383   }
    384 
    385   return micros() - start;
    386 }
    387 
    388 int32_t testLines()
    389 {
    390   uint32_t start;
    391   int32_t x1, y1, x2, y2;
    392 
    393   start = micros_start();
    394 
    395   x1 = y1 = 0;
    396   y2 = h - 1;
    397   for (x2 = 0; x2 < w; x2 += 6)
    398   {
    399     gfx->drawLine(x1, y1, x2, y2, BLUE);
    400   }
    401 #ifdef ESP8266
    402   yield(); // avoid long run triggered ESP8266 WDT restart
    403 #endif
    404 
    405   x2 = w - 1;
    406   for (y2 = 0; y2 < h; y2 += 6)
    407   {
    408     gfx->drawLine(x1, y1, x2, y2, BLUE);
    409   }
    410 #ifdef ESP8266
    411   yield(); // avoid long run triggered ESP8266 WDT restart
    412 #endif
    413 
    414   x1 = w - 1;
    415   y1 = 0;
    416   y2 = h - 1;
    417   for (x2 = 0; x2 < w; x2 += 6)
    418   {
    419     gfx->drawLine(x1, y1, x2, y2, BLUE);
    420   }
    421 #ifdef ESP8266
    422   yield(); // avoid long run triggered ESP8266 WDT restart
    423 #endif
    424 
    425   x2 = 0;
    426   for (y2 = 0; y2 < h; y2 += 6)
    427   {
    428     gfx->drawLine(x1, y1, x2, y2, BLUE);
    429   }
    430 #ifdef ESP8266
    431   yield(); // avoid long run triggered ESP8266 WDT restart
    432 #endif
    433 
    434   x1 = 0;
    435   y1 = h - 1;
    436   y2 = 0;
    437   for (x2 = 0; x2 < w; x2 += 6)
    438   {
    439     gfx->drawLine(x1, y1, x2, y2, BLUE);
    440   }
    441 #ifdef ESP8266
    442   yield(); // avoid long run triggered ESP8266 WDT restart
    443 #endif
    444 
    445   x2 = w - 1;
    446   for (y2 = 0; y2 < h; y2 += 6)
    447   {
    448     gfx->drawLine(x1, y1, x2, y2, BLUE);
    449   }
    450 #ifdef ESP8266
    451   yield(); // avoid long run triggered ESP8266 WDT restart
    452 #endif
    453 
    454   x1 = w - 1;
    455   y1 = h - 1;
    456   y2 = 0;
    457   for (x2 = 0; x2 < w; x2 += 6)
    458   {
    459     gfx->drawLine(x1, y1, x2, y2, BLUE);
    460   }
    461 #ifdef ESP8266
    462   yield(); // avoid long run triggered ESP8266 WDT restart
    463 #endif
    464 
    465   x2 = 0;
    466   for (y2 = 0; y2 < h; y2 += 6)
    467   {
    468     gfx->drawLine(x1, y1, x2, y2, BLUE);
    469   }
    470 #ifdef ESP8266
    471   yield(); // avoid long run triggered ESP8266 WDT restart
    472 #endif
    473 
    474   return micros() - start;
    475 }
    476 
    477 int32_t testFastLines()
    478 {
    479   uint32_t start;
    480   int32_t x, y;
    481 
    482   start = micros_start();
    483 
    484   for (y = 0; y < h; y += 5)
    485   {
    486     gfx->drawFastHLine(0, y, w, RED);
    487   }
    488   for (x = 0; x < w; x += 5)
    489   {
    490     gfx->drawFastVLine(x, 0, h, BLUE);
    491   }
    492 
    493   return micros() - start;
    494 }
    495 
    496 int32_t testFilledRects()
    497 {
    498   uint32_t start;
    499   int32_t i, i2;
    500 
    501   start = micros_start();
    502 
    503   for (i = n; i > 0; i -= 6)
    504   {
    505     i2 = i / 2;
    506 
    507     gfx->fillRect(cx - i2, cy - i2, i, i, gfx->color565(i, i, 0));
    508   }
    509 
    510   return micros() - start;
    511 }
    512 
    513 int32_t testRects()
    514 {
    515   uint32_t start;
    516   int32_t i, i2;
    517 
    518   start = micros_start();
    519   for (i = 2; i < n; i += 6)
    520   {
    521     i2 = i / 2;
    522     gfx->drawRect(cx - i2, cy - i2, i, i, GREEN);
    523   }
    524 
    525   return micros() - start;
    526 }
    527 
    528 int32_t testFilledCircles(uint8_t radius)
    529 {
    530   uint32_t start;
    531   int32_t x, y, r2 = radius * 2;
    532 
    533   start = micros_start();
    534 
    535   for (x = radius; x < w; x += r2)
    536   {
    537     for (y = radius; y < h; y += r2)
    538     {
    539       gfx->fillCircle(x, y, radius, MAGENTA);
    540     }
    541   }
    542 
    543   return micros() - start;
    544 }
    545 
    546 int32_t testCircles(uint8_t radius)
    547 {
    548   uint32_t start;
    549   int32_t x, y, r2 = radius * 2;
    550   int32_t w1 = w + radius;
    551   int32_t h1 = h + radius;
    552 
    553   // Screen is not cleared for this one -- this is
    554   // intentional and does not affect the reported time.
    555   start = micros_start();
    556 
    557   for (x = 0; x < w1; x += r2)
    558   {
    559     for (y = 0; y < h1; y += r2)
    560     {
    561       gfx->drawCircle(x, y, radius, WHITE);
    562     }
    563   }
    564 
    565   return micros() - start;
    566 }
    567 
    568 int32_t testFillArcs()
    569 {
    570   int16_t i, r = 360 / cn;
    571   uint32_t start = micros_start();
    572 
    573   for (i = 6; i < cn; i += 6)
    574   {
    575     gfx->fillArc(cx1, cy1, i, i - 3, 0, i * r, RED);
    576   }
    577 
    578   return micros() - start;
    579 }
    580 
    581 int32_t testArcs()
    582 {
    583   int16_t i, r = 360 / cn;
    584   uint32_t start = micros_start();
    585 
    586   for (i = 6; i < cn; i += 6)
    587   {
    588     gfx->drawArc(cx1, cy1, i, i - 3, 0, i * r, WHITE);
    589   }
    590 
    591   return micros() - start;
    592 }
    593 
    594 int32_t testFilledTriangles()
    595 {
    596   uint32_t start;
    597   int32_t i;
    598 
    599   start = micros_start();
    600 
    601   for (i = cn1; i > 10; i -= 5)
    602   {
    603     gfx->fillTriangle(cx1, cy1 - i, cx1 - i, cy1 + i, cx1 + i, cy1 + i,
    604                       gfx->color565(0, i, i));
    605   }
    606 
    607   return micros() - start;
    608 }
    609 
    610 int32_t testTriangles()
    611 {
    612   uint32_t start;
    613   int32_t i;
    614 
    615   start = micros_start();
    616 
    617   for (i = 0; i < cn; i += 5)
    618   {
    619     gfx->drawTriangle(
    620         cx1, cy1 - i,     // peak
    621         cx1 - i, cy1 + i, // bottom left
    622         cx1 + i, cy1 + i, // bottom right
    623         gfx->color565(0, 0, i));
    624   }
    625 
    626   return micros() - start;
    627 }
    628 
    629 int32_t testFilledRoundRects()
    630 {
    631   uint32_t start;
    632   int32_t i, i2;
    633 
    634   start = micros_start();
    635 
    636   for (i = n1; i > 20; i -= 6)
    637   {
    638     i2 = i / 2;
    639     gfx->fillRoundRect(cx - i2, cy - i2, i, i, i / 8, gfx->color565(0, i, 0));
    640   }
    641 
    642   return micros() - start;
    643 }
    644 
    645 int32_t testRoundRects()
    646 {
    647   uint32_t start;
    648   int32_t i, i2;
    649 
    650   start = micros_start();
    651 
    652   for (i = 20; i < n1; i += 6)
    653   {
    654     i2 = i / 2;
    655     gfx->drawRoundRect(cx - i2, cy - i2, i, i, i / 8, gfx->color565(i, 0, 0));
    656   }
    657 
    658   return micros() - start;
    659 }
    660 
    661 /***************************************************
    662   Original sketch text:
    663 
    664   This is an example sketch for the Adafruit 2.2" SPI display.
    665   This library works with the Adafruit 2.2" TFT Breakout w/SD card
    666   ----> http://www.adafruit.com/products/1480
    667 
    668   Check out the links above for our tutorials and wiring diagrams
    669   These displays use SPI to communicate, 4 or 5 pins are required to
    670   interface (RST is optional)
    671   Adafruit invests time and resources providing this open source code,
    672   please support Adafruit and open-source hardware by purchasing
    673   products from Adafruit!
    674 
    675   Written by Limor Fried/Ladyada for Adafruit Industries.
    676   MIT license, all text above must be included in any redistribution
    677  ****************************************************/