Go Down

Topic: Arduino TFT library problem (Read 2539 times) previous topic - next topic

xun_yang

Hey guys! I'm using the new Arduino TFT screen, the BMP display feature seems to be faulty.
I'm using the following code to display multiple bitmap images, which is modified from TFTBitmapLogo example

Code: [Select]
#include <SPI.h>
#include <SD.h>
#include <TFT.h>  // Arduino LCD library

// pin definition for the Uno
#define sd_cs  4
#define lcd_cs 10
#define dc     9
#define rst    8 

// pin definition for the Leonardo
//#define sd_cs  8
//#define lcd_cs 7
//#define dc     0
//#define rst    1 

TFT TFTscreen = TFT(lcd_cs, dc, rst);

// this variable represents the image to be drawn on screen
PImage logo;


void setup() {
  // initialize the GLCD and show a message
  // asking the user to open the serial line
  TFTscreen.begin();
  TFTscreen.background(255, 255, 255);

  // initialize the serial port: it will be used to
  // print some diagnostic info 

  // clear the GLCD screen before starting
  TFTscreen.background(255, 255, 255);
 
  // try to access the SD card. If that fails (e.g.
  // no card present), the setup process will stop.
  if (!SD.begin(sd_cs)) {
    return;
  }
 
  // initialize and clear the GLCD screen
  TFTscreen.begin();
  TFTscreen.background(255, 255, 255);

  // now that the SD card can be access, try to load the
  // image file.
 
  while(true){
    logo = TFTscreen.loadImage("lg0.bmp");
    if (!logo.isValid()) {
    }
    TFTscreen.image(logo, 0, 0);
   
    logo = TFTscreen.loadImage("lg1.bmp");
    if (!logo.isValid()) {
    }
    TFTscreen.image(logo, 0, 0);
  }
}

void loop() {
  delay(1500);
}


Typically the "logo.isValid()" goes false after 3~4 times. If I put more "Serial.print()" or "TFTscreen.println()" in the code, it'll go false after 0~1 time. I suspect there's a memory leak somewhere.

In another project I'm doing, I use Fat16 for SD read and a customized image display function, I can switch images as many times as I've tried. So this shouldn't be a hardware issue.

Btw in this page http://arduino.cc/en/Tutorial/TFTBitmapLogo the connection diagram right above "Code" title is wrong, pins for SD are not connected, so it's impossible to display BMP if following the instruction.

WalterCarrionTorres

Es muy cierto, ya tambiƩn tengo el mismo problema y he intentado diversas opciones en software como emplear la misma variable para almacenar la ultima figura a mostrar sin lograr obtener el resultado esperado.
En la Laptop lo que me recepta del serial es: loadImage: file not found, cuando anteriormente si la pudo leer....

fsegovia

Hello, I am having the same issue. Has anyone found a solution for this? The loadimage fails after X times of do it successfully.

Thanks,
Fran

fsegovia

It seems that the problem is that the file resource created when calling loadImage is never released. Then after call several times to TFT.loadImage it goes out of resources to create more PImage/File objects.

The solution that I found is to create a close() method in the PImage class to close the file after draw it. I added this code in the public section of the class PImage (PImage.h):

class PImage {
public:
  PImage() :
    _valid(false),
    _bmpWidth(0),
    _bmpHeight(0) { }
 
  void draw(Adafruit_GFX & glcd, int16_t x, int16_t y);
 
  static PImage loadImage(const char * fileName);
 
 
  bool isValid() { return _valid; }
 
  int width() { return _bmpWidth; }
  int height() { return _bmpHeight; }

  void close() {_bmpFile.close(); } 

and I call this method after drawing the image, as in this example:

  logo = TFTscreen.loadImage("TEST.BMP");
  TFTscreen.image(logo, 2, 2);
  logo.close();

With this change it seems it is working ok. Anyway I expect for the official fix of this issue.

Regards,
Fran

Go Up