mirror of
https://github.com/Bodmer/TFT_eSPI.git
synced 2025-08-06 14:14:44 +02:00
Add SPIFFS BMP (bitmap) image file rendering example
This commit is contained in:
88
examples/Generic/TFT_SPIFFS_BMP/BMP_functions.ino
Normal file
88
examples/Generic/TFT_SPIFFS_BMP/BMP_functions.ino
Normal file
@@ -0,0 +1,88 @@
|
||||
// Bodmers BMP image rendering function
|
||||
|
||||
void drawBmp(const char *filename, int16_t x, int16_t y) {
|
||||
|
||||
if ((x >= tft.width()) || (y >= tft.height())) return;
|
||||
|
||||
fs::File bmpFS;
|
||||
|
||||
// Open requested file on SD card
|
||||
bmpFS = SPIFFS.open(filename, "r");
|
||||
|
||||
if (!bmpFS)
|
||||
{
|
||||
Serial.print("File not found");
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t seekOffset;
|
||||
uint16_t w, h, row, col;
|
||||
uint8_t r, g, b;
|
||||
|
||||
uint32_t startTime = millis();
|
||||
|
||||
if (read16(bmpFS) == 0x4D42)
|
||||
{
|
||||
read32(bmpFS);
|
||||
read32(bmpFS);
|
||||
seekOffset = read32(bmpFS);
|
||||
read32(bmpFS);
|
||||
w = read32(bmpFS);
|
||||
h = read32(bmpFS);
|
||||
|
||||
if ((read16(bmpFS) == 1) && (read16(bmpFS) == 24) && (read32(bmpFS) == 0))
|
||||
{
|
||||
y += h - 1;
|
||||
|
||||
tft.setSwapBytes(true);
|
||||
bmpFS.seek(seekOffset);
|
||||
|
||||
uint16_t padding = (4 - ((w * 3) & 3)) & 3;
|
||||
uint8_t lineBuffer[w * 3 + padding];
|
||||
|
||||
for (row = 0; row < h; row++) {
|
||||
|
||||
bmpFS.read(lineBuffer, sizeof(lineBuffer));
|
||||
uint8_t* bptr = lineBuffer;
|
||||
uint16_t* tptr = (uint16_t*)lineBuffer;
|
||||
// Convert 24 to 16 bit colours
|
||||
for (uint16_t col = 0; col < w; col++)
|
||||
{
|
||||
b = *bptr++;
|
||||
g = *bptr++;
|
||||
r = *bptr++;
|
||||
*tptr++ = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
|
||||
}
|
||||
|
||||
// Push the pixel row to screen, pushImage will crop the line if needed
|
||||
// y is decremented as the BMP image is drawn bottom up
|
||||
tft.pushImage(x, y--, w, 1, (uint16_t*)lineBuffer);
|
||||
}
|
||||
Serial.print("Loaded in "); Serial.print(millis() - startTime);
|
||||
Serial.println(" ms");
|
||||
}
|
||||
else Serial.println("BMP format not recognized.");
|
||||
}
|
||||
bmpFS.close();
|
||||
}
|
||||
|
||||
// These read 16- and 32-bit types from the SD card file.
|
||||
// BMP data is stored little-endian, Arduino is little-endian too.
|
||||
// May need to reverse subscript order if porting elsewhere.
|
||||
|
||||
uint16_t read16(fs::File &f) {
|
||||
uint16_t result;
|
||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||
((uint8_t *)&result)[1] = f.read(); // MSB
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t read32(fs::File &f) {
|
||||
uint32_t result;
|
||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||
((uint8_t *)&result)[1] = f.read();
|
||||
((uint8_t *)&result)[2] = f.read();
|
||||
((uint8_t *)&result)[3] = f.read(); // MSB
|
||||
return result;
|
||||
}
|
||||
|
64
examples/Generic/TFT_SPIFFS_BMP/TFT_SPIFFS_BMP.ino
Normal file
64
examples/Generic/TFT_SPIFFS_BMP/TFT_SPIFFS_BMP.ino
Normal file
@@ -0,0 +1,64 @@
|
||||
// This sketch draws BMP images pulled from SPIFFS onto the TFT. It is an
|
||||
// an example from this library: https://github.com/Bodmer/TFT_eSPI
|
||||
|
||||
// Images in SPIFFS must be put in the root folder (top level) to be found
|
||||
// Use the SPIFFS library example to verify SPIFFS works!
|
||||
|
||||
// The example image used to test this sketch can be found in the sketch
|
||||
// Data folder, press Ctrl+K to see this folder. Use the IDE "Tools" menu
|
||||
// option to upload the sketches data folder to the SPIFFS
|
||||
|
||||
// This sketch ahs been tested on the ESP32 and ESP8266
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
|
||||
//====================================================================================
|
||||
// Libraries
|
||||
//====================================================================================
|
||||
// Call up the SPIFFS FLASH filing system this is part of the ESP Core
|
||||
#define FS_NO_GLOBALS
|
||||
#include <FS.h>
|
||||
|
||||
#ifdef ESP32
|
||||
#include "SPIFFS.h" // For ESP32 only
|
||||
#endif
|
||||
|
||||
// Call up the TFT library
|
||||
#include <TFT_eSPI.h> // Hardware-specific library for ESP8266
|
||||
|
||||
// Invoke TFT library
|
||||
TFT_eSPI tft = TFT_eSPI();
|
||||
|
||||
//====================================================================================
|
||||
// Setup
|
||||
//====================================================================================
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
if (!SPIFFS.begin()) {
|
||||
Serial.println("SPIFFS initialisation failed!");
|
||||
while (1) yield(); // Stay here twiddling thumbs waiting
|
||||
}
|
||||
Serial.println("\r\nSPIFFS initialised.");
|
||||
|
||||
// Now initialise the TFT
|
||||
tft.begin();
|
||||
tft.setRotation(0); // 0 & 2 Portrait. 1 & 3 landscape
|
||||
tft.fillScreen(TFT_BLACK);
|
||||
}
|
||||
|
||||
//====================================================================================
|
||||
// Loop
|
||||
//====================================================================================
|
||||
void loop()
|
||||
{
|
||||
int x = random(tft.width() - 128);
|
||||
int y = random(tft.height() - 160);
|
||||
|
||||
drawBmp("/parrot.bmp", x, y);
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
//====================================================================================
|
||||
|
BIN
examples/Generic/TFT_SPIFFS_BMP/data/parrot.bmp
Normal file
BIN
examples/Generic/TFT_SPIFFS_BMP/data/parrot.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
Reference in New Issue
Block a user