That was the goal, but the code uses too much SRAM to work on an Uno, and that was a basic program.
BTW, the image display is a bit slow.
Here is my new code. The original displayed the images upside down. This also has the touchscreen calibrated.
#include <SD.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
MCUFRIEND_kbv tft;
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
// const int XP=6,XM=A2,YP=A1,YM=7; //320x480 ID=0x6814
// const int TS_LEFT=945,TS_RT=157,TS_TOP=970,TS_BOT=164;
const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=136,TS_RT=916,TS_TOP=949,TS_BOT=88;
//PORTRAIT CALIBRATION 320 x 480
//x = map(p.x, LEFT=136, RT=916, 0, 320)
//y = map(p.y, TOP=949, BOT=88, 0, 480)
//LANDSCAPE CALIBRATION 480 x 320
//x = map(p.y, LEFT=949, RT=88, 0, 480)
//y = map(p.x, TOP=916, BOT=136, 0, 320)
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button one_btn, two_btn, three_btn,four_btn,five_btn,six_btn,left_btn,right_btn;
// test bmp files
char bmpFile[] = "img/bmp24.bmp";
char bmpFile2[] = "img/bmptest.bmp";
const char *filename = "/a_duino.bmp";
byte tBuf[512];
//char cBuf[1284];
File bmpFH;
int tCount;
void displayImage(char* fileName,int col,int row) {
long thisPoint = 0UL;
int thisRow = 0;
Serial.println(fileName);
if(!SD.exists(fileName)) {
Serial.println("File does not exist\r\n");
return;
}
bmpFH = SD.open(fileName);
if(!bmpFH) {
Serial.println("Open failed\r\n");
return;
}
bmpFH.seek(thisPoint);
tCount = bmpFH.read(tBuf,29);
if(tBuf[0] != 0x42 || tBuf[1] != 0x4D) {
Serial.println("File not bitmap\r\n");
bmpFH.close();
return;
}
// Serial.println((byte)tBuf[0],HEX);
// Serial.println((byte)tBuf[1],HEX);
int ThishighInt = (int)tBuf[12];
int ThislowInt = (int)tBuf[10];
long startPoint = ThishighInt;
startPoint = startPoint << 16;
startPoint = startPoint | ThislowInt;
Serial.print("Start Position: ");
Serial.println(startPoint,HEX);
thisPoint = startPoint;
int pixelSize = (int)tBuf[0x1c];
Serial.print("Pixel size ");
Serial.println(pixelSize);
if(pixelSize != 16) {
Serial.println("Not 16 bit format\r\n");
bmpFH.close();
return;
}
bmpFH.seek(thisPoint);
ThishighInt = (int)tBuf[20];
ThislowInt = (int)tBuf[18];
long bmpWidth = ThishighInt;
bmpWidth = bmpWidth << 16;
bmpWidth = bmpWidth | ThislowInt;
if(bmpWidth > 256) {
Serial.println("Too wide");
bmpFH.close();
return;
}
Serial.print("Width in pixels: ");
Serial.println(bmpWidth);
ThishighInt = (int)tBuf[24];
ThislowInt = (int)tBuf[22];
long bmpHt = ThishighInt;
bmpHt = bmpHt << 16;
bmpHt = bmpHt | ThislowInt;
if(bmpHt > 256) {
Serial.println("Too tall");
bmpFH.close();
return;
}
Serial.print("Height in pixels: ");
Serial.println(bmpHt);
thisRow = bmpHt -1;
int lineCount = 0;
while(bmpFH.available()) {
tCount = bmpFH.read(tBuf,bmpWidth*2);
thisPoint = thisPoint + (long)tCount;
for(int i=0;i<(tCount);i+=2) {
unsigned int thisColor = tBuf[i+1];
thisColor = thisColor << 8;
thisColor = thisColor | tBuf[i];
int thisCol = i/2;
// Output to screen
// thisRow + row
// thisCol + col
// thisColor is color
tft.drawPixel(thisCol+col,thisRow+row,thisColor);
// sprintf(cBuf,"%04x",thisColor);
// Serial.print(cBuf);
// Serial.print(" ");
}
// Serial.println();
thisRow-=1;
lineCount+=1;
}
bmpFH.close();
// Serial.println();
Serial.print(lineCount);
Serial.println(" lines");
Serial.println();
}
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0x2945
void setup(void)
{
Serial.begin(38400);
if(!SD.begin(10)) {
Serial.println("SD init failed");
while(1);
}
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK);
left_btn.initButton(&tft, 80, 20, 100, 30, WHITE, CYAN, BLACK, "LEFT", 2);
right_btn.initButton(&tft, 240, 20, 100, 30, WHITE, CYAN, BLACK, "RIGHT", 2);
one_btn.initButton(&tft, 80, 110, 100, 100, WHITE, CYAN, BLACK, "ONE", 2);
two_btn.initButton(&tft, 240, 110, 100, 100, WHITE, CYAN, BLACK, "TWO", 2);
three_btn.initButton(&tft, 80, 260, 100, 100, WHITE, CYAN, BLACK, "THREE", 2);
four_btn.initButton(&tft, 240, 260, 100, 100, WHITE, CYAN, BLACK, "FOUR", 2);
five_btn.initButton(&tft, 80, 410, 100, 100, WHITE, CYAN, BLACK, "FIVE", 2);
six_btn.initButton(&tft, 240, 410, 100, 100, WHITE, CYAN, BLACK, "SIX", 2);
one_btn.drawButton(false);
two_btn.drawButton(false);
three_btn.drawButton(false);
four_btn.drawButton(false);
five_btn.drawButton(false);
six_btn.drawButton(false);
left_btn.drawButton(false);
right_btn.drawButton(false);
tft.fillRect(150, 0, 20, 20, WHITE);
// Here is where the images are displayed
displayImage("img/PirateSM.bmp",20,50);
displayImage("img/CaptJack.bmp",180,50);
displayImage("img/PirateSM.bmp",20,200);
displayImage("img/CaptJack.bmp",180,200);
displayImage("img/PirateSM.bmp",20,350);
displayImage("img/CaptJack.bmp",180,350);
}
void loop(void)
{
bool down = Touch_getXY();
one_btn.press(down && one_btn.contains(pixel_x, pixel_y));
two_btn.press(down && two_btn.contains(pixel_x, pixel_y));
three_btn.press(down && three_btn.contains(pixel_x, pixel_y));
four_btn.press(down && four_btn.contains(pixel_x, pixel_y));
five_btn.press(down && five_btn.contains(pixel_x, pixel_y));
six_btn.press(down && six_btn.contains(pixel_x, pixel_y));
left_btn.press(down && left_btn.contains(pixel_x, pixel_y));
right_btn.press(down && right_btn.contains(pixel_x, pixel_y));
if (one_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, GREEN);
// displayImage(filename,40,180);
}
if (two_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, RED);
}
if (three_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, BLUE);
}
if (four_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, CYAN);
// displayImage(filename,40,180);
}
if (five_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, MAGENTA);
}
if (six_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, YELLOW);
}
if (left_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, WHITE);
}
if (right_btn.justPressed()) {
tft.fillRect(150, 0, 20, 20, GREY);
}
}