I programmed an Arduino Nano to display bmp images on a lcd tft display.
The code I wrote is doing some other stuff too but that is not imporant the only thing thats not working is the displaying of the bmp images. If you have any questions about my code feel free to ask.
#include <MCUFRIEND_kbv.h>
#include <SD.h>
#include <SPI.h>
#define statusPin1 13
#define statusPin2 A5
#define SD_CS 10
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define WHITE 0xFFFF
MCUFRIEND_kbv tft;
File bmpFile;
uint8_t currentPage = 0;
uint8_t lastPage = 255;
//y, x
const uint16_t imagePositions[6][2] = {
{0, 350}, {0, 200}, {0, 10},
{170, 350}, {170, 200}, {170, 10}
};
void drawBMP(const char *filename, int x, int y) {
bmpFile = SD.open(filename);
if (!bmpFile) {
Serial.print("Fehlt: "); Serial.println(filename);
return;
}
if (bmpFile.read() != 'B' || bmpFile.read() != 'M') {
Serial.println("Kein BMP");
bmpFile.close();
return;
}
bmpFile.seek(10);
uint32_t dataOffset = bmpFile.read() | (bmpFile.read() << 8) | (bmpFile.read() << 16) | (bmpFile.read() << 24);
bmpFile.seek(18);
int32_t bmpWidth = bmpFile.read() | (bmpFile.read() << 8) | (bmpFile.read() << 16) | (bmpFile.read() << 24);
int32_t bmpHeight = bmpFile.read() | (bmpFile.read() << 8) | (bmpFile.read() << 16) | (bmpFile.read() << 24);
if (bmpWidth != 120 || bmpHeight != 120) {
Serial.println("Wrong BMP Size");
bmpFile.close();
return;
}
bmpFile.seek(dataOffset);
uint8_t r, g, b;
uint16_t color;
int rowSize = (bmpWidth * 3 + 3) & ~3;
for (int row = 0; row < bmpHeight; row++) {
int pos = dataOffset + (bmpHeight - 1 - row) * rowSize;
bmpFile.seek(pos);
for (int col = 0; col < bmpWidth; col++) {
b = bmpFile.read();
g = bmpFile.read();
r = bmpFile.read();
color = tft.color565(r, g, b);
tft.drawPixel(x + col, y + row, color);
}
}
bmpFile.close();
}
void showPage(uint8_t p) {
tft.fillScreen(BLACK);
int startIndex = (p - 1) * 6;
for (int i = 0; i < 6; i++) {
char filename[12];
sprintf(filename, "/%02d.bmp", startIndex + i + 1);
int x = imagePositions[i][0];
int y = imagePositions[i][1];
drawBMP(filename, x, y);
}
}
void setup() {
Serial.begin(9600);
pinMode(statusPin1, INPUT);
pinMode(statusPin2, INPUT);
uint16_t ID = tft.readID();
tft.begin(ID);
tft.setRotation(0);
tft.fillScreen(BLACK);
if (!SD.begin(SD_CS)) {
tft.setTextColor(RED);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("SD Card Initialization problem");
while (1);
}
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Display ready");
}
void loop() {
bool pin1 = digitalRead(statusPin1);
bool pin2 = digitalRead(statusPin2);
if (!pin1 && pin2){
currentPage = 1; Serial.println("1");
}else if (pin1 && !pin2){
currentPage = 2; Serial.println("2");
}else if (!pin1 && !pin2){
currentPage = 3; Serial.println("3");
}else if (pin1 && pin2){
currentPage = 4; Serial.println("4");
}
if (currentPage != lastPage) {
lastPage = currentPage;
showPage(currentPage);
}
delay(200);
}
Thank you for every comment!