I have an SD card with 5 files on it. It will never have more than 5 files.
What I want to be able to do, from the TFT screen, is access the SD card, read the file names and display on the TFT screen, select one of the files from the touch pad and run it.
I have found a sketch that does exactly what I need by using the Arduino Serial monitor.
I have left the Serial.print, Serial.write etc calls in there and mimiced them with TFT calls so I have identical displays on the Arduino Serial monitor and my TFT display.
What I cant do is get the touch control to mimic the Serial monitor in putting in a value (eg. 1 for File 1), pressing the send button or pressing enter, and getting it to open the file.
Code is as follows.
I have highlighted and commented out the touch section in the sketch. If that is commented out, the program runs perfectly via the Serial Monitor. If left in, the Serial monitor prints the number "1" but states its an" invalid number" and will not open the file.
Any help would be appreciated.
#include<SPI.h>
#include "SdFat.h"
#include "FreeStack.h"
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <XPT2046_Touchscreen.h> //.kbv
#include <Fonts/FreeSansBold24pt7b.h>
//#include <SD.h>
MCUFRIEND_kbv tft;
XPT2046_Touchscreen ts(53, 255);
const int TS_LANDSCAPE = 1; //XPT2046_TouchScreen.h
const int TS_LEFT = 338, TS_RT = 3879, TS_TOP = 3767, TS_BOT = 237; //Red ST7796 Shield (landscape)
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
bool pressed = ts.touched();
if (pressed) {
TS_Point p = ts.getPoint();
if (TS_LANDSCAPE) mapxy(p.y, p.x);
else mapxy(p.x, p.y);
extern int x, y; //to suit your global variables
x = pixel_x;
y = pixel_y;
}
return pressed;
}
void mapxy(int x, int y)
{
// int aspect = tft.getRotation(); //LANDSCAPE
int tft_width = tft.width();
int tft_height = tft.height();
//LANDSCAPE
pixel_x = map(y, TS_TOP, TS_BOT, 0, tft_width);
pixel_y = map(x, TS_RT, TS_LEFT, 0, tft_height);
}
File root;
#define BLACK 0x0000
#define WHITE 0xFFFF
// SD card chip select pin.
const uint8_t SD_CS_PIN = 48;
int x, y;
SdFat sd;
SdFile dirFile;
SdFile file;
// Number of files found.
uint16_t numberOfFiles = 0;
// Position of file's directory entry.
uint16_t dirIndex[50];
SdFile parIndex[50];
//------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
tft.reset();
tft.begin(0x9488);
ts.begin(); //.kbv XPT2046
tft.setRotation(1); //0 Landscape
tft.fillScreen(BLACK);
while (!Serial) {}
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50)))
{
sd.initErrorHalt();
}
if (dirFile.open("/", O_READ))
{
printDirectory(dirFile, 0);
}
}
void printDirectory (SdFile CFile, int numTabs)
{
SdFile file;
while (file.openNext(&CFile, O_READ))
{
if (file.isHidden() || false)
{
//file hidden, skip
}
else
{
for (uint8_t i = 0; i < numTabs; i++)
{
//create tabs for spacing
Serial.print('\t');
}
if (file.isSubDir())
{
SdFile SubDirFile;
printDirectory(SubDirFile, numTabs + 1);
}
else
{
// Save dirIndex of file in directory.
dirIndex[numberOfFiles] = file.dirIndex();
parIndex[numberOfFiles] = CFile;
// Print the file number and name.
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print(numberOfFiles);
Serial.print(numberOfFiles);
tft.write(' ');
Serial.write(' ');
file.printName(&tft);
file.printName(&Serial);
tft.println();
Serial.println();
numberOfFiles++;
}
}
file.close();
}
}
void loop() {
int c;
char rx_byte = 1;
// Read any existing Serial data.
do {
delay(10);
} while (Serial.available() && Serial.read() >= 0);
tft.fillRect(380, 10, 60, 60, WHITE);
tft.setCursor (400, 30);
tft.setTextColor(BLACK);
tft.print("1");
tft.setTextColor(WHITE);
tft.setCursor (10, 100);
tft.setTextSize(2);
tft.print(F("\r\nEnter File Number: "));
Serial.print(F("\r\nEnter File Number: "));
while (!Serial.available()) {
SysCall::yield();
/*
///////////////////////////////////////////comment out below and use Serial Screen and it works
while (true)
{
if (Touch_getXY()) //.kbv call a single function
{
//Serial.print(x); Serial.print(","); Serial.println(y); //added 3march22
if (x > 380 && x < 440)
{
if (y > 10 && y < 70)
{
Serial.print("1 ");
delay(100);
}
/////////////////////////////////////////comment out above and use Serial Screen and it works
*/
}
c = Serial.read();
uint8_t i = c - '0';
if (!isdigit(c) || i >= numberOfFiles) {
Serial.println(F("Invalid number"));
return;
}
tft.println(i);
Serial.println(i);
if (!file.open(&parIndex[i], dirIndex[i], O_READ)) {
sd.errorHalt(F("open"));
}
tft.println();
Serial.println();
char last = 0;
// Copy up to 500 characters to Serial.
for (int k = 0; k < 500 && (c = file.read()) > 0; k++) {
tft.write(last = (char)c);
Serial.write(last = (char)c);
}
// Add new line if missing from last line.
if (last != '\n') {
tft.println();
Serial.println();
}
file.close();
Serial.flush();
delay(100);
}
// } ////////////////////////////////////comment out for Serial Screen
// } //////////////////////////////// comment out for Serial Screen
//} /////////////////////////////////// comment out for Serial Screen