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

LittleFS_PNG.ino (3628B)

      1 
      2 // This example if for processors with LittleFS capability (e.g. RP2040,
      3 // ESP32, ESP8266). It renders a png file that is stored in LittleFS
      4 // using the PNGdec library (available via library manager).
      5 
      6 // The test image is in the sketch "data" folder (press Ctrl+K to see it).
      7 // You must upload the image to LittleFS using the Arduino IDE Tools Data
      8 // Upload menu option (you may need to install extra tools for that).
      9 
     10 // Don't forget to use the Arduino IDE Tools menu to allocate a LittleFS
     11 // memory partition before uploading the sketch and data!
     12 
     13 #include <LittleFS.h>
     14 #define FileSys LittleFS
     15 
     16 // Include the PNG decoder library
     17 #include <PNGdec.h>
     18 
     19 PNG png;
     20 #define MAX_IMAGE_WIDTH 240 // Adjust for your images
     21 
     22 int16_t xpos = 0;
     23 int16_t ypos = 0;
     24 
     25 // Include the TFT library https://github.com/Bodmer/TFT_eSPI
     26 #include "SPI.h"
     27 #include <TFT_eSPI.h>              // Hardware-specific library
     28 TFT_eSPI tft = TFT_eSPI();         // Invoke custom library
     29 
     30 //====================================================================================
     31 //                                    Setup
     32 //====================================================================================
     33 void setup()
     34 {
     35   Serial.begin(115200);
     36   Serial.println("\n\n Using the PNGdec library");
     37 
     38   // Initialise FS
     39   if (!FileSys.begin()) {
     40     Serial.println("LittleFS initialisation failed!");
     41     while (1) yield(); // Stay here twiddling thumbs waiting
     42   }
     43 
     44   // Initialise the TFT
     45   tft.begin();
     46   tft.fillScreen(TFT_BLACK);
     47 
     48   Serial.println("\r\nInitialisation done.");
     49 }
     50 
     51 //====================================================================================
     52 //                                    Loop
     53 //====================================================================================
     54 void loop()
     55 {
     56   // Scan LittleFS and load any *.png files
     57   File root = LittleFS.open("/", "r");
     58   while (File file = root.openNextFile()) {
     59     String strname = file.name();
     60     strname = "/" + strname;
     61     Serial.println(file.name());
     62     // If it is not a directory and filename ends in .png then load it
     63     if (!file.isDirectory() && strname.endsWith(".png")) {
     64       // Pass support callback function names to library
     65       int16_t rc = png.open(strname.c_str(), pngOpen, pngClose, pngRead, pngSeek, pngDraw);
     66       if (rc == PNG_SUCCESS) {
     67         tft.startWrite();
     68         Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
     69         uint32_t dt = millis();
     70         if (png.getWidth() > MAX_IMAGE_WIDTH) {
     71           Serial.println("Image too wide for allocated line buffer size!");
     72         }
     73         else {
     74           rc = png.decode(NULL, 0);
     75           png.close();
     76         }
     77         tft.endWrite();
     78         // How long did rendering take...
     79         Serial.print(millis()-dt); Serial.println("ms");
     80       }
     81     }
     82     delay(3000);
     83     tft.fillScreen(random(0x10000));
     84   }
     85 }
     86 
     87 
     88 //=========================================v==========================================
     89 //                                      pngDraw
     90 //====================================================================================
     91 // This next function will be called during decoding of the png file to
     92 // render each image line to the TFT.  If you use a different TFT library
     93 // you will need to adapt this function to suit.
     94 // Callback function to draw pixels to the display
     95 void pngDraw(PNGDRAW *pDraw) {
     96   uint16_t lineBuffer[MAX_IMAGE_WIDTH];
     97   png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
     98   tft.pushImage(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer);
     99 }