sid8580
Well-known member
Not sure why this is so difficult (spent 3 hours on it now) but I can't get a bitmap to display on my otherwise working T3.6 w/ 240x240 ST7789. I'll get "BMP format not recognized" no matter what bit depth I use, or even using the example (parrot.bmp).
The only thing I can think of at this point is, maybe the version of the SDfat library I'm using presents some issue (SdFatSdioEX)?
Both should be initializing as 'SD' and obviously it's reading something, but the numbers make no sense, the file I'm using is 176k.
If there is a best/supported/correct SD library to use, I can try that, I just fought with that for nearly a week last time (turned out to be a giant library mess I've never gone back to) so I'm reluctant to mess with that again just to test this bitmap function :-(
Output of serial monitor:
15:06:17.623 -> Loading image 'vui24.bmp'
15:06:17.623 -> File size: 2738376002
15:06:17.623 -> Image Offset: 2738376002
15:06:17.623 -> Header size: 2738376002
15:06:17.623 -> BMP format not recognized.
15:06:17.623 -> UI - done
15:06:19.663 -> Cleared -
The only thing I can think of at this point is, maybe the version of the SDfat library I'm using presents some issue (SdFatSdioEX)?
Both should be initializing as 'SD' and obviously it's reading something, but the numbers make no sense, the file I'm using is 176k.
If there is a best/supported/correct SD library to use, I can try that, I just fought with that for nearly a week last time (turned out to be a giant library mess I've never gone back to) so I'm reluctant to mess with that again just to test this bitmap function :-(
Output of serial monitor:
15:06:17.623 -> Loading image 'vui24.bmp'
15:06:17.623 -> File size: 2738376002
15:06:17.623 -> Image Offset: 2738376002
15:06:17.623 -> Header size: 2738376002
15:06:17.623 -> BMP format not recognized.
15:06:17.623 -> UI - done
15:06:19.663 -> Cleared -
Code:
/***************************************************
This is a library for several Adafruit displays based on ST77* drivers.
Works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
The 1.8" TFT shield
----> https://www.adafruit.com/product/802
The 1.44" TFT breakout
----> https://www.adafruit.com/product/2088
The 1.54" TFT breakout
----> https://www.adafruit.com/product/3787
The 2.0" TFT breakout
----> https://www.adafruit.com/product/4311
as well as Adafruit raw 1.8" TFT display
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
// This Teensy3 and 4 native optimized and extended version
// requires specific pins.
// If you use the short version of the constructor and the DC
// pin is hardware CS pin, then it will be slower.
// undefine this if you wish to try sync updates, note:
// some processors will undefine this.
#define USE_FRAME_BUFFER
#define TFT_SCLK 32 // SCLK can also use pin 14
#define TFT_MOSI 0 // MOSI can also use pin 7
#define TFT_CS 30 // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define TFT_DC 31 // but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define TFT_RST 29 // RST can use any pin
//#define SD_CS 55 // CS for SD card, can use any pin
// Note the above pins are for the SPI object. For those Teensy boards which have
// more than one SPI object, such as T3.5, T3.6, T4 which have at SPI1 and SPI2
// LC with SPI1, look at the cards that come with the teensy or the web page
// https://www.pjrc.com/teensy/pinout.html to select the appropriate IO pins.
#include <Adafruit_GFX.h> // Core graphics library
#include <ST7735_t3.h> // Hardware-specific library
#include <ST7789_t3.h> // Hardware-specific library
#include <SPI.h>
//#include <Adafruit_ImageReader.h>
// Option 1: use any pins but a little slower
// Note: code will detect if specified pins are the hardware SPI pins
// and will use hardware SPI if appropriate
// For 1.44" and 1.8" TFT with ST7735 use
//ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
// For 1.54" or other TFT with ST7789, This has worked with some ST7789
// displays without CS pins, for those you can pass in -1 or 0xff for CS
// More notes by the tft.init call
ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
// For 1.44" and 1.8" TFT with ST7735 use
//ST7735_t3 tft = ST7735_t3(cs, dc, rst);
// For 1.54" TFT with ST7789
//ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_RST);
float p = 3.1415926;
#define BUFFPIXEL 20
#include "SDFat.h"
#include <SPI.h>
SdFatSdioEX SD;
void setup(void) {
//pinMode(SD_CS, INPUT_PULLUP); // don't touch the SD card
Serial.begin(9600);
Serial.print("hello!");
// use this initializer (uncomment) if you're using a 1.54" 240x240 TFT
tft.init(240, 240); // initialize a ST7789 chip, 240x240 pixels
Serial.println("init");
// SD card init
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
} else {
Serial.print(" SD initialization complete.");
}
}
void loop() {
tft.fillScreen(ST7735_BLACK);
Serial.println("Cleared -");
bmpDraw("vui24.bmp", 0, 0);
Serial.println("UI - done");
delay(2000);
}
void bmpDraw(const char *filename, uint8_t x, uint8_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 >= tft.width()) || (y >= tft.height())) return;
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
bmpFile = SD.open(filename);
if (!bmpFile) {
Serial.print("File not found");
tft.fillScreen(ST7735_BLACK);
tft.setCursor(12, tft.height()/2 - 12);
tft.print("Unable to");
tft.setCursor(12, tft.height()/2 - 0);
tft.print("read file: ");
tft.setCursor(12, tft.height()/2 + 12);
tft.setTextColor(ST7735_YELLOW);
tft.print(filename);
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: "); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
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) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
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 is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft.width()) w = tft.width() - x;
if((y+h-1) >= tft.height()) h = tft.height() - y;
// Set TFT address window to clipped image bounds
tft.setAddrWindow(x, y, x+w-1, y+h-1);
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
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++];
tft.pushColor(tft.Color565(r,g,b));
} // end pixel
} // end scanline
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println("BMP format not recognized.");
}
// 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;
}
Last edited: