Cannot display bitmaps, T3.6 w/ ST7789 w/ onboard SD

Status
Not open for further replies.

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 -

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:
If anyone's looked at this - apologies, came back to it less frustrated to find there's more work I need to do to first before bringing it here - please disregard the code and let me narrow it down some:

Which SD library is the fastest and/or Teensy optimized? I've found several long threads on this and despite that I'm not entirely clear what you'd all recommend or which location I can find it (or if the answer is already part of the Teensyduino library).

My problems come from the fact I've got some confusion going on there I believe, different SD libraries used between display operations I'm trying to test.

Thanks.
 
This works, as in I can now load files from SD and display text and primitives onscreen. But to display a bitmap... still no go. But I left it on and after long enough it started displaying parrot.bmp partially, it's always off screen somewhat when it does appear and I can't predict when even though this example code is supposed to just loop...

I thought I had it figured out, as in I could just keep playing with it and eventually figure out what the 0,0 screen coordinate offset was and the proper rotation, but 4 hours later and I'm still lost.

Tried to nail down what the setRotation value is as well as the image x and y position with lots of serial prints, but it won't work the same way twice. I've also tried creating many iterations where I manually set the rotation and bmpDraw starting coordinates.

Code:
/***************************************************
// 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

#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);

float p = 3.1415926;

#include <SD.h>
const int chipSelect = BUILTIN_SDCARD;
//#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!");
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // Or use this initializer (uncomment) if you're using a 1.54" 240x240 TFT
  tft.init(240, 240);   // initialize a ST7789 chip, 240x240 pixel


//good
//  tft.fillRoundRect(3, 92, 56, 56, 8, 0xf800);
//  tft.fillRoundRect(63, 92, 56, 56, 8, 0xf800);
//  tft.fillRoundRect(123, 92, 56, 56, 8, 0xf800);
//  tft.fillRoundRect(183, 92, 56, 56, 8, 0xf800);    

//  tft.drawRoundRect(2, 91, 58, 58, 8, 0x0000);
//  tft.drawRoundRect(61, 91, 58, 58, 8, 0x0000);
//  tft.drawRoundRect(121, 91, 58, 58, 8, 0x0000);
//  tft.drawRoundRect(181, 91, 58, 58, 8, 0x0000);  

//good
//  tft.fillTriangle(159, 19, 219, 19, 189, 69, 0xf800);  
//  tft.fillRect(159, 29, 60, 10, 0x0000);
//  tft.fillRect(159, 49, 60, 10, 0x0000);

//  delay(2000);
//  tft.fillScreen(ST7735_BLACK);
//  delay(2000);
//   Serial.print("tft-width ");
//   Serial.println(tft.width());
//   Serial.print("tft-hi ");
//   Serial.println(tft.height());
//    tft.setRotation(0);  
//    bmpDraw("parrot.bmp", 0, 0);
//    Serial.println("UI - done");
//    delay(2000);
//    tft.setRotation(1); 
//    bmpDraw("parrot.bmp",0, 0);
//    Serial.println("UI2 - done");
//    delay(2000);
//    tft.setRotation(2); 
//    bmpDraw("parrot.bmp", 0, 0);
//    Serial.println("UI3 - done");
//    delay(2000);
//    tft.setRotation(3); 
//    bmpDraw("parrot.bmp", 0, 0);
//    Serial.println("UI4 - done");
//    delay(2000);
//    Serial.println("getRotation ");
//    Serial.println(tft.getRotation());
//    tft.setRotation(0);  
//    bmpDraw("parrot.bmp", -90, 0);
//    Serial.println("UI - done");
//    delay(2000);
//    tft.setRotation(1); 
//    bmpDraw("parrot.bmp", -90, 0);
//    Serial.println("UI2 - done");
//    delay(2000);
//    tft.setRotation(2); 
//    bmpDraw("parrot.bmp", -90, 0);
//    Serial.println("UI3 - done");
//    delay(2000);
//    tft.setRotation(3); 
//    bmpDraw("parrot.bmp", -90, 0);
//    Serial.println("UI4 - done");
//    delay(2000);    
//
//
//    tft.setRotation(0);  
//    bmpDraw("parrot.bmp", 0, -90);
//    Serial.println("UI - done");
//    delay(2000);
//    tft.setRotation(1); 
//    bmpDraw("parrot.bmp", 0, -90);
//    Serial.println("UI2 - done");
//    delay(2000);
//    tft.setRotation(2); 
//    bmpDraw("parrot.bmp", 0, -90);
//    Serial.println("UI3 - done");
//    delay(2000);

    tft.fillScreen(ST7735_BLACK);
    tft.setRotation(0); 
    bmpDraw("vui24.bmp", 0, 0);
    Serial.println("UI4 - done");
    delay(1000);   
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 60, 60);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 120, 120);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 180, 180);
    Serial.println("UI4 - done");
    delay(1000);   

    tft.fillScreen(ST7735_BLACK);
    tft.setRotation(1); 
    bmpDraw("vui24.bmp", 0, 0);
    Serial.println("UI4 - done");
    delay(1000);   
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 60, 60);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 120, 120);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 180, 180);
    Serial.println("UI4 - done");
    delay(1000);   

    tft.fillScreen(ST7735_BLACK);
    tft.setRotation(2); 
    bmpDraw("vui24.bmp", 0, 0);
    Serial.println("UI4 - done");
    delay(1000);   
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 60, 60);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 120, 120);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 180, 180);
    Serial.println("UI4 - done");
    delay(1000);   

    tft.fillScreen(ST7735_BLACK);
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 0, 0);
    Serial.println("UI4 - done");
    delay(1000);   
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 60, 60);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 120, 120);
    Serial.println("UI4 - done");
    delay(1000); 
    tft.setRotation(3); 
    bmpDraw("vui24.bmp", 180, 180);
    Serial.println("UI4 - done");
    delay(1000);          

}

void loop() {

  tft.fillScreen(ST7735_BLACK); // Clear display
  for (int i=0; i<4; i++) {      // Draw 4 parrots
    bmpDraw("vui24.bmp", tft.width() / 4 * i, tft.height() / 4 * i);
    Serial.println("width ");
    Serial.println(tft.width() / 4 * i);
    Serial.println("height ");
    Serial.println(tft.height() / 4 * i);   
  }
  delay(1000);
  tft.setRotation(tft.getRotation() + 1); // Inc rotation 90 degrees
  Serial.println("getRotation ");
  Serial.println(tft.getRotation());
  tft.drawCircle(29, 119, 26, 0xffff);
  delay(500);
}



#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));
          } // 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;
}

Serial monitor output looks like this, looping (this is a section of it looping anyway):

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 93 ms
wid
0
hi
0

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 92 ms
wid
60
hi
60

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 69 ms
wid
120
hi
120

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 22 ms
wid
180
hi
180
getRotation
3

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 92 ms
wid
0
hi
0

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 93 ms
wid
60
hi
60

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 69 ms
wid
120
hi
120

Loading image 'parrot.bmp'
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
Loaded in 22 ms
wid
180
hi
180
getRotation
0
___

So... while I was posting this, the parrot appeared only once, and it's been running for about 15 minutes (it's just to the side of my computer screen so I'll catch it. Right in the middle. But it took 15 minutes and I'm supposed to be looping, or at least that's what the code looks like and my serial prints say. Sometimes, it will show the parrot after 10 seconds... sometimes it will show it repeatedly for a few iterations, then disappear again. Or not at all.

Argh!

And thanks, if anyone reads this far ;)
 
Ok - I've been including 2 screen libraries, even though I am using a ST7789 240x240 1.3" display:
#include<ST7735.h>
#include<ST7789.h>

This is because, in some earlier research I'd done to get the TFT working I'd come across library example code that was originally for the ST7735 and adapted for the ST7789 (I would need to dig around to find exactly what that was, not going to unless some wants to know, believe it had to do with getting it working on the T3.6 and T4.0... there is a lot to keep track of, sorry).

My testing with text and primitives was working fine with both included, however that came to be - and I hadn't thought about it in a while.
But if I remove ST7735.h?

:D:D:D I can display bitmaps! :D:D:D

Maybe this should have been obvious (?) but the earlier situation around just getting the screen to work, leaving it alone after my earlier display testing (trying to avoid fixing what didn't look broken), potentially getting libraries confused, testing Adafruit generic libraries too, reading too many partially but not fully relevant (to this) forum threads etc. made it really hard to see/try out as a fix.

Here are my final questions:
To get a full-screen bitmap to display (240x240), I have to use setRotation(2) or (3). 0 and 1 cause me to be partially off screen. I don't know if there is yet another problem I need to address, or this is normal behavior - I COULD just physically mount the display rotated 180/270 degrees etc clockwise to match one of the working orientations and call it a day. Bitmaps, primitives and text will display correctly across the entire TFT screen with rotations 3 or 4, and if I just avoid 0 or 1, it should be fine (and I see no reason I'd need to use them).

Conversely, with ST7735.h included, all four orientations work correctly for text and primitives but then I get no bitmaps. That's actually what concerns me - I don't know if orientations 0 and 1 not working is indicative of a problem with the ST7789 init call that will bite me later.

What do you guys think? I will try digging into the ST7789.h file to see what I can find in the meantime. But if anyone can put this to rest sooner, I would appreciate any tips.

Thanks.
 
Status
Not open for further replies.
Back
Top