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_DMA.ino (3996B)

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