Brendon Shaw
Active member
I am trying to get the Adafruit 1.54" 240x240 Wide Angle TFT ST7789 working with a Teensy 3.6, using the Adafruit library, by reading the bitmaps from the SD card but colours on the screen do not match the bitmap (See attached file). I think the problem was in the display library Color definitions section, but this does not affect the bitamp. I have drawn blocks of colour, but there are very different to the display.
Is the problem with the code or how the bitmap images are generated and saved?
Is the problem with the code or how the bitmap images are generated and saved?
Code:
#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 <SD.h>
#include <Arduino_ST7789.h> // Hardware-specific library for ST7789 (with or without CS pin)
#define BUFFPIXEL 20
File myFile;
char picturefilename[300];
char picturefiledelay[300];
File root;
const int chipSelect = BUILTIN_SDCARD;
#define filenameflag = 1;
#define timedelaynameflag = 2;
#define TFT_SCLK 13 // SCLK can also use pin 14
#define TFT_MOSI 11 // MOSI can also use pin 7
#define TFT_CS 10 // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define TFT_DC 9 // but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define TFT_RST 8 // RST can use any pin
#define SD_CS BUILTIN_SDCARD // CS for SD card, can use any pin
// 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);
Arduino_ST7789 tft = Arduino_ST7789(-1, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_CS); //for display with CS pin and DC via 9bit SPI
float p = 3.1415926;
void setup(void)
{
pinMode(SD_CS, INPUT_PULLUP); // don't touch the SD card
Serial.begin(9600);
Serial.print("hello!");
// Or use this initializer (uncomment) if you're using a 1.54" 240x240 TFT
tft.init(240, 240); // initialize a ST7789 chip, 240x240 pixels
tft.fillScreen(BLACK); red
delay(5000);
tft.fillScreen(BLUE);
delay(5000);
tft.fillScreen(YELLOW);
delay(5000);
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
tft.fillScreen(ST7735_BLACK);
tft.setCursor(5, tft.height()/2 - 6);
tft.print("Unable to access");
tft.setCursor(32, tft.height()/2 + 6);
tft.print("SD card");
while (1) {
writedrawtext("Error with SD Card", ST7735_WHITE, 0 ,0);
}
}
Serial.println("Screen and SD card has all started");
}
void loop()
{
int i, ptr2, timedelay;
String tmp;
bmpDraw("320x320.bmp",0,0);
delay(1000);
}
#define BUFFPIXEL 20
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));
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)
{
tft.fillScreen(ST7735_BLACK);
writedrawtext("Error with picture format", ST7735_WHITE, 0, 0);
writedrawtext(filename, ST7735_WHITE, 0, 40);
}
}
// 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;
}
void writedrawtext(const char *text, uint16_t color, int x, int y) {
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(text);
}
Code:
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF