I puzzled some hours with exactly the same hardware setup and made a quick & dirty, but successfully test script, combining LCD, Touch and SD Card Features.
While googeling I read some hints about wrong Hardware pin mistakes made by the SainSmart V1 Display boards.
I can NOT agree These discusions.
My board is a SainSmart V1.0 and everything works properly with maximum SPI Speed.
The hardes part was to get the SD Card filenames on my TFT Display. (not possible with the sd.h library.)
But with the <tinyFAT.h> it works!
It seems to be a good choice to use These libs.
SD.h comfortable Serial file System features
tinyFAT.h the only way to get filenames on the tft display
UTFT.h easy going Display library
UTouch.h a perfect and easy to use touch screen scanning lib
UTFT_tinyFAT an addon for the tinyFAT.h lib, comes with a Picture Frame demo
and here Comes the quich & dirty test hack:
I hope you apologise my real dirty hack style
// UTFT_ViewFont (C)2012 Henning Karlsen
// web: Electronics - Henning Karlsen
//
// This program is a demo of the included fonts.
//
// This demo was made for modules with a screen resolution
// of 320x240 pixels.
//
// This program requires the UTFT library.
//
// set up variables using the SD utility library functions:
#include <SD.h>
#include <tinyFAT.h>
#include <UTFT.h>
#include <UTouch.h>
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 53;
int x, y;
int bg[] = {
0, 0, 255};
int fg[] = {
255, 255, 255};
// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
// Uncomment the next line for Arduino 2009/Uno
//UTFT myGLCD(ITDB32S,19,18,17,16); // Remember to change the model parameter to suit your display module!
// Uncomment the next line for Arduino Mega
UTFT myGLCD(ITDB32S,38,39,40,41); // Remember to change the model parameter to suit your display module!
UTouch myTouch(6,5,4,3,2);
byte initres;
byte res;
byte input;
void setup()
{
START:
// Initialize serial communication
Serial.begin(115200);
Serial.println("press the ENTER button on your Touch Screen!");
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Initializing SD card...", CENTER, 0);
pinMode(chipSelect, OUTPUT);
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
myGLCD.print("initialization failed. Things to check:", CENTER, 16);
myGLCD.print("* is a card inserted?", CENTER, 32);
myGLCD.print("* Is your wiring correct?", CENTER, 48);
myGLCD.print("* Is your CS pin setting correct?", CENTER, 64);
delay(5000);
goto START;
}
else {
myGLCD.print("Wiring is correct and a card is present.", CENTER, 16);
String CT = "Card type: ";
switch(card.type()) {
case SD_CARD_TYPE_SD1:
CT = CT + " SD1";
break;
case SD_CARD_TYPE_SD2:
CT = CT + " SD2";
break;
case SD_CARD_TYPE_SDHC:
CT = CT + " SDHC";
break;
default:
CT = CT + "Unknown";
}
myGLCD.print(CT, CENTER, 32);
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
myGLCD.print("Could not find FAT16/FAT32 partition.Make sure you've formatted the card", CENTER, 48);
delay (5000);
goto START;
}
else{
// print the type and size of the first FAT-type volume
myGLCD.print("Volume type is FAT "+(String)volume.fatType(), CENTER, 48);
uint32_t volumesize;
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
myGLCD.print("Volume size (bytes): "+(String)volumesize, CENTER, 64);
volumesize /= 1024;
myGLCD.print("Volume size (Kbytes): "+(String)volumesize, CENTER, 80);
volumesize /= 1024;
myGLCD.print("Volume size (Mbytes): "+(String)volumesize, CENTER, 96);
myGLCD.print("Files found on the card", CENTER, 112);
myGLCD.print("Open the Serial Monitor", CENTER, 128);
myGLCD.print("and wait for instructions!", CENTER, 144);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(bg[0], bg[1], bg[2]);
initres=file.initFAT();
if (initres!=NO_ERROR)
{
myGLCD.print("***** ERROR: ",CENTER,144);
while (true) {
};
}
drawButtons();
}
}
}
void drawButtons()
{
// Draw the ENTER buttons
myGLCD.setColor(bg[0], bg[1], bg[2]);
myGLCD.fillRoundRect (90, 180, 220, 230);
myGLCD.setColor(fg[0], fg[1], fg[2]);
myGLCD.drawRoundRect (90, 180, 220, 230);
myGLCD.print("Enter", 115, 195);
myGLCD.setBackColor (0, 0, 0);
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
}
// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable()){
}
delay(20);
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
ListFiles();
myGLCD.setColor(fg[0], fg[1], fg[2]);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
void loop()
{
if(Serial.available()){
int ib = Serial.read();
String is = (String)(char)ib;
myGLCD.print(" ",CENTER,160);
myGLCD.print(is, CENTER,160);
}
if (myTouch.dataAvailable())
{
delay(10);
int xt;
int yt;
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if (x>=90 && x<=220 && y >=180 && y<=230) // Button: Enter
{
waitForIt(90, 180, 220, 230);
}
else {
if (y >= 5 && y <= 235 && x >= 5 && x <= 315){
myGLCD.print(" ",260,160);
int x1 = ((x-5)/72+1)*72-72+4;
int x2 = x1 + 65;
int y1 = ((y-5)/10+1)*10-10+5;
int y2 = y1 + 10;
myGLCD.print((String)((y-5)/10+1),260,160);
myGLCD.print((String)((x-5)/72+1),280,160);
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while(myTouch.dataAvailable()){
}
delay(20);
myGLCD.setColor(0, 255, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
}
}
}
void ListFiles(){
int x = 5;
int y = 5;
String FN;
String EX;
myGLCD.clrScr();
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
res = file.findFirstFile(&file.DE);
if (res==NO_ERROR)
{
EX = (String)file.DE.fileext;
EX.trim();
if(EX == "RAW"){
myGLCD.print(file.DE.filename, x, y);
myGLCD.drawRoundRect (x-1, y, x+64, y+10);
y+=10;
}
}
else
{
myGLCD.print("No files found...",CENTER,144);
}
while (res==NO_ERROR)
{
res = file.findNextFile(&file.DE);
if (res==NO_ERROR)
{
EX = (String)file.DE.fileext;
EX.trim();
if(EX == "RAW"){
myGLCD.print(file.DE.filename, x, y);
myGLCD.drawRoundRect (x-1, y, x+64, y+10);
y+=10;
if (y > 230){
y = 5;
x+=72;
}
}
}
}
}