Example Code for Arduino-Display BMP from SD
Last revision 2025/12/25
Display a picture “ladar.bmp” from SD card.
Hardware Preparation
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Download the DFRobot_TELEMATICS_3.5-_TFT_Touch_LCD_Shield library: DFRobot_TELEMATICS_3.5-_TFT_Touch_LCD_Shield library
- About how to install the library?
Note: This library only works on Classic Arduino IDE 1.0.x)
Sample Code
#include <Arduino.h>
#include <SPI.h>
#include <MultiLCD.h>
#include <SD.h>
#include "datalogger.h"
#define STATE_SD_READY 0x1
#define STATE_OBD_READY 0x2
#define STATE_GPS_CONNECTED 0x4
#define STATE_GPS_READY 0x8
#define STATE_MEMS_READY 0x10
#define STATE_GUI_ON 0x20
LCD_R61581 lcd;
CDataLogger logger;
byte state = 0;
// 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(File & f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(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;
}
bool checkSD()
{
Sd2Card card;
SdVolume volume;
state &= ~STATE_SD_READY;
pinMode(SS, OUTPUT);
lcd.setFontSize(FONT_SIZE_MEDIUM);
if (card.init(SPI_FULL_SPEED, SD_CS_PIN)) {
const char* type;
switch(card.type()) {
case SD_CARD_TYPE_SD1:
type = "SD1";
break;
case SD_CARD_TYPE_SD2:
type = "SD2";
break;
case SD_CARD_TYPE_SDHC:
type = "SDHC";
break;
default:
type = "SDx";
}
lcd.print(type);
lcd.write(' ');
if (!volume.init(card)) {
lcd.print("No FAT!");
return false;
}
uint32_t volumesize = volume.blocksPerCluster();
volumesize >>= 1; // 512 bytes per block
volumesize *= volume.clusterCount();
volumesize >>= 10;
lcd.print((int)volumesize);
lcd.print("MB\n");
} else {
lcd.println("No SD Card");
return false;
}
if (!SD.begin(SD_CS_PIN)) {
lcd.println("Bad SD");
return false;
}
state |= STATE_SD_READY;
return true;
}
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
#define BUFFPIXEL 20
void bmpDraw(char *fileName, uint16_t x, uint16_t y){
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if((x >= 480) || (y >= 320))
return;
if((bmpFile = SD.open(fileName)) == NULL){
lcd.println("File not found");
return;
}
if(read16(bmpFile) == 0x4d42){
Serial.print("File size:");
Serial.println(read32(bmpFile));
(void)read32(bmpFile);
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: ");
Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("Header size: ");
Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1){
bmpDepth = read16(bmpFile);
Serial.print("Bit Depth: ");
Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed 没有压缩
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= 480)
w = 480 - x;
if((y+h-1) >= 320)
h = 320 - y;
for (row=0; row<h; row++) { // For each scanline...
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
lcd.drawPixel(col + x, row + y, Color565(r,g,b));
} // end pixel
} // end scanline
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
}
}
}
bmpFile.close();
if(!goodBmp)
Serial.println("BMP format not recognized.");
}
static uint32_t startTime = 0;
void dataIdleLoop(){
// called while initializing
char buf[10];
unsigned int t = (millis() - startTime) / 1000;
sprintf(buf, "%02u:%02u", t / 60, t % 60);
lcd.setFontSize(FONT_SIZE_XLARGE);
lcd.setColor(RGB16(0,0,0));
lcd.setBackColor(RGB16_WHITE);
lcd.setCursor(0, 0);
lcd.print(buf);
}
void setup(){
Serial.begin(115200);
lcd.begin();
lcd.setBackLight(255);
lcd.setFontSize(FONT_SIZE_MEDIUM); //set font size
lcd.println();
if (checkSD()) {
bmpDraw("ladar.bmp", 0, 0);
}
}
void loop(){
}
Was this article helpful?
