Trouble displaying images from SD Card, 2.4" TFT/LCD screen + shield Arduino DUE

Hello everybody !

I’m quite new to Arduino, so apologize in advance if I say monstrous things about Arduino’s operating mode…
English isn’t my native language… I do my best to write without mistakes, apologize if it’s awful to read…

I’ve got to lead a very simple project : displaying images on a 2.4" 320x240 screen from a SD Card (even if I’m working on DUE, there is not enough RAM to handle 20+ bitmaps, that’s why I need them on SD card…)

I already consulted you (especially Dennis & Graham, so many thanks to them) for an SPI issue. Everything is working well now.

I consult you one more time, because I’m out of ideas and it’s been weeks that I’m stuck with this problem. Let me explain my trouble now :

Hardware and configuration:

  • I’m working on a DUE with a TFT/LCD shield and a 2.4" TFT/LCD screen from Buydisplay.
    Both shield and screen possess a SD Card slot, i’m using the one on the screen, its accessibility is the most convenient.
  • SD Card is a SanDisk, 4GB, Class 4, micro SDHC.
  • Images are converted with Imageconverter565, a tool you find in UTFT library
  • The image I want to display is the one Dennis gave me in the previous post linked above. You can find it joined to this post.
  • I’m using software SPI, according to my shield :
[list][li]SCLK  2, MOSI  3, MISO  4[/li]
[li]TFT_CS  9, TFT_DC  7, TFT_RST  10[/li]
[li]Touch_CS  6, Touch_PEN  5[/li]
[li]SD_CS  8[/li][/list] [/li] 
[/list]

[b]Trouble :[/b]

I use UTFT_SdRaw library to load my image on the screen.
I started with LANDSCAPE orientation. If I just use the command
[code]myFiles.load( 0, 0, 320, 240, "pic101.raw",1,0);[/code]in my function, it results in a white blank screen. 
When I modify the command like this, the image is well displayed as a 240x180 image
[code] myFiles.load( 0, 0, 240, 180, "pic101.raw",180,0);[/code]As you can see, the bufmult is really HIGH ! I read many topics where Graham explained how this [color=teal]int load(int x, int y, int sx, int sy, char* filename, int bufmult, int iswap);[/color] function works and the highest bufmult I saw was 16 !!!!!
I tried every bufmult number from 1 to 180, and 180 is the only one displaying the picture entirely with no odd behavior (white lines, picture changing color before becoming white, starts to display well then stops, inverses colors, black and white moving lines appear before everything shades gently to white screen...)

I noticed I have no trouble displaying 240x180 images with the last command I wrote, they are well displayed without color inversion.
If I want to display a 320x240 picture, it's impossible. High bufmult numbers aren't handled by load function, it results a white screen or the screen drawn previously stays on... or odd behavior as I described above (and it's just a snippet...)

I thought it could be an orientation issue, so I update my screen set up from LANDSCAPE to PORTRAIT.
To display an image, I had to comment the part of the code in UTFT_SdRaw library which handles PORTRAIT displaying :
[code]int UTFT_SdRaw::load(int x, int y, int sx, int sy, char *filename, int bufmult, bool iswap)
{
  char buf[2 * bufmult * sx];
  int cx, cy, cp;
  word result;
  if (dataFile.open(filename))
  {
    cbi(_UTFT->P_CS, _UTFT->B_CS);
    cx = 0;
    cy = 0;
    result = bufmult * sx;
   /*if (_UTFT->orient == PORTRAIT)                         //Need to be commented to display 
    {                                                                     //
      _UTFT->setXY(x, y, x + sx - 1, y + sy - 1);          //
    }*/                                                                  //
    for (int n = 0; n < sy; n += bufmult)
    {
      result = dataFile.read(&buf, (2 * bufmult) * sx);
     /*if (_UTFT->orient == PORTRAIT)                       //
      {                                                                   //  
        for (int i = 0; i < result; i += 2)                       //
        {                                                                 //  
          if (iswap == 1)                                            //
          {                                                               //
            _UTFT->LCD_Write_DATA(buf[i + 1], buf[i]);   //
          }                                                               //
          else                                                           //
          {                                                               //
            _UTFT->LCD_Write_DATA(buf[i], buf[i + 1]);   //
          }                                                               //
        }                                                                 // 
      }                                                                   //
      else                                                               // 
      {*/                                                                //
        cp = 0;
        while (cp < result)
        {
          if (((result - cp) / 2) < (sx - cx))
          {
            _UTFT->setXY(x + cx, y + cy, x + cx + ((result - cp) / 2) - 1, y + cy);
            for (int i = (result - cp) - 2; i >= 0; i -= 2)
            {
              if (iswap == 1)
              {
                _UTFT->LCD_Write_DATA(buf[cp + i + 1], buf[cp + i]);
              }
              else
              {
                _UTFT->LCD_Write_DATA(buf[cp + i], buf[cp + i + 1]);
              }
            }
            cx += ((result - cp) / 2);
            cp = result;
          }
          else
          {
            _UTFT->setXY(x + cx, y + cy, x + sx - 1, y + cy);
            for (int i = sx - cx - 1; i >= 0; i--)
            {
              if (iswap)
              {
                _UTFT->LCD_Write_DATA(buf[cp + (i * 2) + 1], buf[cp + (i * 2)]);
              }
              else
              {
                _UTFT->LCD_Write_DATA(buf[cp + (i * 2)], buf[cp + (i * 2) + 1]);
              }
            }
            cp += (sx - cx) * 2;
            cx = 0;
            cy++;
          //}                                                                                                   //
        }
      }
    }
    dataFile.close();
    _UTFT->setXY(0, 0, _UTFT->getDisplayXSize() - 1, _UTFT->getDisplayYSize() - 1);
    sbi(_UTFT->P_CS, _UTFT->B_CS);
    return 0;
  }
  else
  {
    return 99;
  }
}[/code]
And this was ONLY to display something on screen. If I uncomment those lines the screen stays on the one drawn previously and the sketch stops running. Nothing tries to display the picture on screen...

And it's the same command as LANDSCAPE orientation that allows to display the image, with a high number for bufmult...
[code]myFiles.load( 0, 0, 240, 180, "pic101.raw",180,0);[/code]

I'm not an expert, only a novice. It seems the bufmult number has to be [color=teal]int sy[/color]. It only works with 240x180 image. If I apply the same thing for 320x240 image (bufmult = 240) nothing appears on screen. If bufmult = 1, I only have a white screen... other numbers under 100 result in odd behavior... 
I don't know why but the load function isn't used correctly here... I don't have enough knowledge to understand what is going on here... That's why I'm asking for your help with this strange case... It's really impossible to display correctly a 320x240 sized image even if the screen size is 320x240 (I really tried EVERY number for bufmult...)

My post is too big, you'll get my sketch as well as the spec of my shield & screen on the next post ... (to be continued...)


[pic101.zip|attachment](upload://iNnpYICJG85P4XGwSNA6byZXdvn.zip) (31.9 KB)

Here is my sketch.

//#include <SPI.h>
#include <SdFat.h>
#include <UTFT_SdRaw.h>
#include <UTFT.h>
#include <URTouch.h>
#include <UTFT_Buttons.h>

// Settings pins for software SPI
#define TFTcs 9
#define TFTsclk 2 
#define TFTmosi 3 
#define TFTdc 7
#define TFTrst 10

// SD chip select pin
const uint8_t chipSelect = 8;

//Declare each fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

//Set up the screen
UTFT myGLCD(TFT01_24SP,TFTmosi,TFTsclk,TFTcs,TFTrst,TFTdc);
//UTFT myGLCD(ILI9341_4L, 3,2,9,10,7); //old definition of the screen

//Set up of the touch 
URTouch  myTouch(TFTsclk, 6, TFTmosi, 4, 5);

//Set up the buttons
UTFT_Buttons myButtons(&myGLCD, &myTouch);

ArduinoOutStream cout(Serial);


UTFT_SdRaw myFiles(&myGLCD);
File myPic;
SdFile  dataPic;
// file system
SdFat sd;


void screenHome()  // draw main home screen showing overview info
{ 
  myGLCD.setFont(SmallFont);
  myGLCD.clrScr();                                                 // clear the screen
  myGLCD.setBackColor(255,0,0);                             //Display a red Rect with White text
  myGLCD.setColor(0,0,0);
  myGLCD.fillRoundRect(0,0,240,100);
  myGLCD.print("Displaying client images",10,10);
  Serial.println(F("background displayed"));               //serial com to control everything went well
  delay(2000);
}

void fileSerialDisplay()                              //function to control if the image is the right one                                
{unsigned long int BufHex[100];                 //by comparison of HEX doc and serial info
int i=0, inputHex;

    myPic = sd.open("pictest.raw");
  if (myPic) {
  Serial.println("pictest.raw :");

      do
      {
        inputHex=myPic.read();           //Gets one byte from serial buffer
        BufHex[i]=inputHex;                //store it
        Serial.println(BufHex[i],HEX);
          i++;                                   //increment where to write next
      } 
      while(i<50);
      
 //     Serial.println(F("Stop file read"));
 
    // close the file:
    myPic.close();
  }else {
    // if the file didn't open, print an error:
    Serial.println("error opening pictest.raw");
  }
}



void screenSdDisplay()                         //Display the image on screen
{
  //Set CS pins other than SD device OUTPUT HIGH
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);
  pinMode(6,OUTPUT);
  digitalWrite(6,HIGH);
  pinMode(53,OUTPUT);
  digitalWrite(53,HIGH);
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  
 
  //check the files in SD Card
  cout << F("Files found (date time size name):\n");
  sd.ls(LS_R | LS_DATE | LS_SIZE);
 
  Serial.println(F("Start displaying"));                      //lot of serial but needed to know where 
  int mytime = millis();                                         //the sketch stops...
  Serial.println(F("try"));
  myFiles.load( 0, 0, 240, 180, "pic101.raw",180,0);
  Serial.println(F("after load command"));
  Serial.println(millis() - mytime);
  
  delay(5000);
  
}
  

void setup() {


//Set CS pins other than SD device OUTPUT HIGH
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);
  pinMode(6,OUTPUT);
  digitalWrite(6,HIGH);
  pinMode(53,OUTPUT);
  digitalWrite(53,HIGH);
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

// Open serial communications and wait for port to open:  
  Serial.begin(9600);
  while (!Serial) {
    ;                                   // wait for serial port to connect. Needed for DUE & Leonardo only
  }
  Serial.println(F("Initialising SD card..."));
  bool mysd = 0;
  // see if the card is present and can be initialized:
  while (!mysd)
  {
    if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
      
      Serial.println(F("Card failed, or not present"));
      Serial.println(F("Retrying...."));
    }
      else
    {
      mysd = 1;
      Serial.println(F("Card initialised."));
    }
  }
  
  //check the files in SD Card
  cout << F("Files found (date time size name):\n");
  sd.ls(LS_R | LS_DATE | LS_SIZE);

  
  Serial.println(F("Initialising LCD."));
  myGLCD.InitLCD(PORTRAIT);
  bool myglcd = 0;
  while(!myglcd)
  {
    if(myGLCD.orient!=PORTRAIT){
      Serial.println(F("LCD failed"));
      Serial.println(F("Retrying..."));
    }
    else
    {
      myglcd=1;
      myGLCD.clrScr();
      Serial.println(F("LCD initialised."));
      myGLCD.setColor(255, 255, 255);
      myGLCD.fillRect(0, 10, 240,15); // draw line on top of the screen

    }
  }

  Serial.println(F("Initialising Touch..."));
  myTouch.InitTouch();
  Serial.println(F("Touch Initialized"));
    


  screenHome();
  fileSerialDisplay();
  screenSdDisplay();
 
}

void loop(){}

I really want to say sorry if I did a stupid mistake and didn’t see it…

I hope somebody would be able to help me, I’ll really appreciate and be very thankful ! It’s been weeks I’m working on this, I want to do a last try before changing my material (as I said in the other post). I didn’t find any people reporting those kind of troubles with this shield nor this screen…

I thank you for your help !

Gara

P.S: just in case, here is the Serial I have when the sketch runs well. HEX data are correct.

Initialising SD card...
Card initialised
Files found :
2016-10-26 09:50:10      86400 pictest.raw
2016-11-02 15:39:42     153600 rottest.raw
2016-11-02 11:20:20      86400 rot101.raw
2016-11-02 15:32:00      86400 rot102.raw
2016-11-02 15:40:28      86400 rot103.raw
2016-11-02 16:55:56      86400 rotjpeg.raw
2016-10-19 12:58:56     153600 pic101.raw
Initialising LCD.
LCD initialised.
Initialising Touch...
Touch Initialized
background displayed
pictest.raw :
FFFFFFFF
F5
A
FD
A
F4
C9
F4
C9
F4
C8
F4
E8
F4
C8
F4
C8
F4
C8
F4
C8
F4
C8
F4
C8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
E8
F4
Files found (date time size name):
2016-10-26 09:50:10      86400 pictest.raw
2016-11-02 15:39:42     153600 rottest.raw
2016-11-02 11:20:20      86400 rot101.raw
2016-11-02 15:32:00      86400 rot102.raw
2016-11-02 15:40:28      86400 rot103.raw
2016-11-02 16:55:56      86400 rotjpeg.raw
2016-10-19 12:58:56     153600 pic101.raw
Start displaying
try
after load command
597

ER-AS-ILI9341_Datasheet.pdf (828 KB)

ER-AS-ILI9341-Arduino-Schematic-Diagram.pdf (206 KB)

ER-TFTM024-3_Datasheet.pdf (764 KB)

And I forgot to say that the images I want to display are 320x240 sized, they come from a client that's why I can't share them with you.
I can't display them as 240x180 sized images.
To display them as PORTRAIt oriented I rotated and transformed them, so they are 240x320 images now.