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

ImgViewerPng.ino (8645B)

      1 /*******************************************************************************
      2  * PNG Image Viewer
      3  * This is a simple PNG image viewer example
      4  * Image Source: https://github.com/logos
      5  *
      6  * Dependent libraries:
      7  * PNGdec: https://github.com/bitbank2/PNGdec.git
      8  *
      9  * Setup steps:
     10  * 1. Change your LCD parameters in Arduino_GFX setting
     11  * 2. Upload PNG file
     12  *   FFat (ESP32):
     13  *     upload FFat (FatFS) data with ESP32 Sketch Data Upload:
     14  *     ESP32: https://github.com/lorol/arduino-esp32fs-plugin
     15  *   LittleFS (ESP32 / ESP8266 / Pico):
     16  *     upload LittleFS data with ESP8266 LittleFS Data Upload:
     17  *     ESP32: https://github.com/lorol/arduino-esp32fs-plugin
     18  *     ESP8266: https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
     19  *     Pico: https://github.com/earlephilhower/arduino-pico-littlefs-plugin.git
     20  *   SPIFFS (ESP32):
     21  *     upload SPIFFS data with ESP32 Sketch Data Upload:
     22  *     ESP32: https://github.com/lorol/arduino-esp32fs-plugin
     23  *   SD:
     24  *     Most Arduino system built-in support SD file system.
     25  *     Wio Terminal require extra dependant Libraries:
     26  *     - Seeed_Arduino_FS: https://github.com/Seeed-Studio/Seeed_Arduino_FS.git
     27  *     - Seeed_Arduino_SFUD: https://github.com/Seeed-Studio/Seeed_Arduino_SFUD.git
     28  ******************************************************************************/
     29 #define PNG_FILENAME "/octocat.png"
     30 #define PNG_4BPP_FILENAME "/octocat-4bpp.png"
     31 
     32 /*******************************************************************************
     33  * Start of Arduino_GFX setting
     34  *
     35  * Arduino_GFX try to find the settings depends on selected board in Arduino IDE
     36  * Or you can define the display dev kit not in the board list
     37  * Defalult pin list for non display dev kit:
     38  * Arduino Nano, Micro and more: CS:  9, DC:  8, RST:  7, BL:  6, SCK: 13, MOSI: 11, MISO: 12
     39  * ESP32 various dev board     : CS:  5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
     40  * ESP32-C3 various dev board  : CS:  7, DC:  2, RST:  1, BL:  3, SCK:  4, MOSI:  6, MISO: nil
     41  * ESP32-S2 various dev board  : CS: 34, DC: 38, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
     42  * ESP32-S3 various dev board  : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
     43  * ESP8266 various dev board   : CS: 15, DC:  4, RST:  2, BL:  5, SCK: 14, MOSI: 13, MISO: 12
     44  * Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
     45  * RTL8720 BW16 old patch core : CS: 18, DC: 17, RST:  2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
     46  * RTL8720_BW16 Official core  : CS:  9, DC:  8, RST:  6, BL:  3, SCK: 10, MOSI: 12, MISO: 11
     47  * RTL8722 dev board           : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
     48  * RTL8722_mini dev board      : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI:  9, MISO: 10
     49  * Seeeduino XIAO dev board    : CS:  3, DC:  2, RST:  1, BL:  0, SCK:  8, MOSI: 10, MISO:  9
     50  * Teensy 4.1 dev board        : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
     51  ******************************************************************************/
     52 #include <Arduino_GFX_Library.h>
     53 
     54 #define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
     55 
     56 /* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
     57 #if defined(DISPLAY_DEV_KIT)
     58 Arduino_GFX *gfx = create_default_Arduino_GFX();
     59 #else /* !defined(DISPLAY_DEV_KIT) */
     60 
     61 /* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
     62 Arduino_DataBus *bus = create_default_Arduino_DataBus();
     63 
     64 /* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
     65 Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 3 /* rotation */, false /* IPS */);
     66 
     67 #endif /* !defined(DISPLAY_DEV_KIT) */
     68 /*******************************************************************************
     69  * End of Arduino_GFX setting
     70  ******************************************************************************/
     71 
     72 /* Wio Terminal */
     73 #if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
     74 #include <Seeed_FS.h>
     75 #include <SD/Seeed_SD.h>
     76 #elif defined(TARGET_RP2040)
     77 #include <LittleFS.h>
     78 #include <SD.h>
     79 #elif defined(ESP32)
     80 #include <FFat.h>
     81 #include <LittleFS.h>
     82 #include <SPIFFS.h>
     83 #include <SD.h>
     84 #elif defined(ESP8266)
     85 #include <LittleFS.h>
     86 #include <SD.h>
     87 #else
     88 #include <SD.h>
     89 #endif
     90 
     91 #include <PNGdec.h>
     92 PNG png;
     93 
     94 int16_t w, h, xOffset, yOffset;
     95 
     96 // Functions to access a file on the SD card
     97 File pngFile;
     98 
     99 void *myOpen(const char *filename, int32_t *size)
    100 {
    101 /* Wio Terminal */
    102 #if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
    103   pngFile = SD.open(filename, "r");
    104 #elif defined(TARGET_RP2040)
    105   pngFile = LittleFS.open(filename, "r");
    106   // pngFile = SD.open(filename, "r");
    107 #elif defined(ESP32)
    108   // pngFile = FFat.open(filename, "r");
    109   pngFile = LittleFS.open(filename, "r");
    110   // pngFile = SPIFFS.open(filename, "r");
    111   // pngFile = SD.open(filename, "r");
    112 #elif defined(ESP8266)
    113   pngFile = LittleFS.open(filename, "r");
    114   // pngFile = SD.open(filename, "r");
    115 #else
    116   pngFile = SD.open(filename, FILE_READ);
    117 #endif
    118 
    119   if (!pngFile || pngFile.isDirectory())
    120   {
    121     Serial.println(F("ERROR: Failed to open " PNG_FILENAME " file for reading"));
    122     gfx->println(F("ERROR: Failed to open " PNG_FILENAME " file for reading"));
    123   }
    124   else
    125   {
    126     *size = pngFile.size();
    127     Serial.printf("Opened '%s', size: %d\n", filename, *size);
    128   }
    129 
    130   return &pngFile;
    131 }
    132 
    133 void myClose(void *handle)
    134 {
    135   if (pngFile)
    136     pngFile.close();
    137 }
    138 
    139 int32_t myRead(PNGFILE *handle, uint8_t *buffer, int32_t length)
    140 {
    141   if (!pngFile)
    142     return 0;
    143   return pngFile.read(buffer, length);
    144 }
    145 
    146 int32_t mySeek(PNGFILE *handle, int32_t position)
    147 {
    148   if (!pngFile)
    149     return 0;
    150   return pngFile.seek(position);
    151 }
    152 
    153 // Function to draw pixels to the display
    154 void PNGDraw(PNGDRAW *pDraw)
    155 {
    156   uint16_t usPixels[320];
    157   uint8_t usMask[320];
    158 
    159   // Serial.printf("Draw pos = 0,%d. size = %d x 1\n", pDraw->y, pDraw->iWidth);
    160   png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_LITTLE_ENDIAN, 0x00000000);
    161   png.getAlphaMask(pDraw, usMask, 1);
    162   gfx->draw16bitRGBBitmap(xOffset, yOffset + pDraw->y, usPixels, usMask, pDraw->iWidth, 1);
    163 }
    164 
    165 void setup()
    166 {
    167   Serial.begin(115200);
    168   // Serial.setDebugOutput(true);
    169   // while(!Serial);
    170   Serial.println("PNG Image Viewer");
    171 
    172 #ifdef GFX_EXTRA_PRE_INIT
    173   GFX_EXTRA_PRE_INIT();
    174 #endif
    175 
    176   // Init Display
    177   gfx->begin();
    178   gfx->fillScreen(BLACK);
    179 
    180   w = gfx->width(), h = gfx->height();
    181   gfx->fillScreen(BLACK);
    182   for (int16_t x = 0; x < w; x += 5)
    183   {
    184     gfx->drawFastVLine(x, 0, h, PINK);
    185   }
    186 
    187 #ifdef GFX_BL
    188   pinMode(GFX_BL, OUTPUT);
    189   digitalWrite(GFX_BL, HIGH);
    190 #endif
    191 
    192 /* Wio Terminal */
    193 #if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
    194   if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
    195 #elif defined(TARGET_RP2040)
    196   if (!LittleFS.begin())
    197   // if (!SD.begin(SS))
    198 #elif defined(ESP32)
    199   // if (!FFat.begin())
    200   if (!LittleFS.begin())
    201   // if (!SPIFFS.begin())
    202   // if (!SD.begin(SS))
    203 #elif defined(ESP8266)
    204   if (!LittleFS.begin())
    205   // if (!SD.begin(SS))
    206 #else
    207   if (!SD.begin())
    208 #endif
    209   {
    210     Serial.println(F("ERROR: File System Mount Failed!"));
    211     gfx->println(F("ERROR: File System Mount Failed!"));
    212   }
    213   else
    214   {
    215     unsigned long start = millis();
    216     int rc;
    217     rc = png.open(PNG_FILENAME, myOpen, myClose, myRead, mySeek, PNGDraw);
    218     if (rc == PNG_SUCCESS)
    219     {
    220       int16_t pw = png.getWidth();
    221       int16_t ph = png.getHeight();
    222 
    223       xOffset = (w - pw) / 2;
    224       yOffset = (h - ph) / 2;
    225 
    226       rc = png.decode(NULL, 0);
    227 
    228       Serial.printf("Draw offset: (%d, %d), time used: %lu\n", xOffset, yOffset, millis() - start);
    229       Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
    230       png.close();
    231     }
    232     else
    233     {
    234       Serial.println("png.open() failed!");
    235     }
    236   }
    237 
    238   delay(5000); // 5 seconds
    239 }
    240 
    241 void loop()
    242 {
    243   unsigned long start = millis();
    244   int rc;
    245   rc = png.open(PNG_4BPP_FILENAME, myOpen, myClose, myRead, mySeek, PNGDraw);
    246   if (rc == PNG_SUCCESS)
    247   {
    248     // random draw position
    249     int16_t pw = png.getWidth();
    250     int16_t ph = png.getHeight();
    251     xOffset = random(w) - (pw / 2);
    252     yOffset = random(h) - (ph / 2);
    253 
    254     rc = png.decode(NULL, 0);
    255 
    256     Serial.printf("Draw offset: (%d, %d), time used: %lu\n", xOffset, yOffset, millis() - start);
    257     Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
    258     png.close();
    259   }
    260   else
    261   {
    262     Serial.println("png.open() failed!");
    263   }
    264 
    265   delay(1000); // 1 second
    266 }