From 15916bec4b135075e21cf24558bdc1189f68e370 Mon Sep 17 00:00:00 2001 From: prenticedavid Date: Wed, 11 Mar 2020 16:08:24 +0000 Subject: [PATCH] detect X, Y from correct pins on ARM. report cosmetics. --- .../TouchScreen_Calibr_native.ino | 206 ++++++++++-------- 1 file changed, 111 insertions(+), 95 deletions(-) diff --git a/examples/TouchScreen_Calibr_native/TouchScreen_Calibr_native.ino b/examples/TouchScreen_Calibr_native/TouchScreen_Calibr_native.ino index b688cee..ec9a5f8 100644 --- a/examples/TouchScreen_Calibr_native/TouchScreen_Calibr_native.ino +++ b/examples/TouchScreen_Calibr_native/TouchScreen_Calibr_native.ino @@ -6,32 +6,56 @@ #define PORTRAIT 0 #define LANDSCAPE 1 +#define USE_XPT2046 0 +#define USE_LOCAL_KBV 1 #define TOUCH_ORIENTATION PORTRAIT -#define TITLE "TouchScreen.h GFX Calibration" - -#include -#include -MCUFRIEND_kbv tft; #if defined(USB_PID) && USB_PID == 0x804E // Arduino M0 Native #define Serial SerialUSB #endif +#define SWAP(x, y) { int t = x; x = y; y = t; } + +#define TITLE "TouchScreen.h GFX Calibration" +#include +#include +MCUFRIEND_kbv tft; + // MCUFRIEND UNO shield shares pins with the TFT. #if defined(ESP32) int XP = 27, YP = 4, XM = 15, YM = 14; //most common configuration #else -int XP = 6, YP = A1, XM = A2, YM = 7; //most common configuration -//int XP = 7, YP = A2, XM = A1, YM = 6; //next common configuration +//int XP = 6, YP = A1, XM = A2, YM = 7; //most common configuration +int XP = 7, YP = A2, XM = A1, YM = 6; //next common configuration //int XP=PB7,XM=PA6,YP=PA7,YM=PB6; //BLUEPILL must have Analog for YP, XM #endif -//#include //Adafruit Library -//TouchScreen ts(XP, YP, XM, YM, 300); //re-initialised after diagnose -//TSPoint tp; //global point +#if USE_LOCAL_KBV #include "TouchScreen_kbv.h" //my hacked version -TouchScreen_kbv ts(XP, YP, XM, YM, 300); //re-initialised after diagnose -TSPoint_kbv tp; //global point +#define TouchScreen TouchScreen_kbv +#define TSPoint TSPoint_kbv +#else +#include //Adafruit Library +#endif +TouchScreen ts(XP, YP, XM, YM, 300); //re-initialised after diagnose +TSPoint tp; //global point + +void readResistiveTouch(void) +{ + tp = ts.getPoint(); + pinMode(YP, OUTPUT); //restore shared pins + pinMode(XM, OUTPUT); + //digitalWrite(YP, HIGH); //because TFT control pins + //digitalWrite(XM, HIGH); + // Serial.println("tp.x=" + String(tp.x) + ", tp.y=" + String(tp.y) + ", tp.z =" + String(tp.z)); +} + +uint16_t readID(void) { + uint16_t ID = tft.readID(); + if (ID == 0xD3D3) ID = 0x9486; + return ID; +} +#define TFT_BEGIN() tft.begin(ID) #define WHITE 0xFFFF #define RED 0xF800 @@ -43,16 +67,6 @@ TSPoint_kbv tp; //global point #define GRAY BLUE //idle cross-hair colour #define GRAY_DONE RED //finished cross-hair -void readResistiveTouch(void) -{ - tp = ts.getPoint(); - pinMode(YP, OUTPUT); //restore shared pins - pinMode(XM, OUTPUT); - digitalWrite(YP, HIGH); //because TFT control pins - digitalWrite(XM, HIGH); - // Serial.println("tp.x=" + String(tp.x) + ", tp.y=" + String(tp.y) + ", tp.z =" + String(tp.z)); -} - bool ISPRESSED(void) { // .kbv this was too sensitive !! @@ -96,9 +110,17 @@ void showpins(int A, int D, int value, const char *msg) Serial.println(buf); } -boolean diagnose_pins() +void bofe(char *buf) { - int i, j, value, Apins[2], Dpins[2], Values[2], found = 0; + tft.println(buf); + Serial.println(buf); +} + +#if USE_XPT2046 == 0 +bool diagnose_pins() +{ + uint8_t i, j, Apins[2], Dpins[2], found = 0; + uint16_t value, Values[2]; Serial.println(F("Making all control and bus pins INPUT_PULLUP")); Serial.println(F("Typical 30k Analog pullup with corresponding pin")); @@ -130,27 +152,29 @@ boolean diagnose_pins() pinMode(i, INPUT_PULLUP); } if (found == 2) { - Serial.println(F("Diagnosing as:-")); int idx = Values[0] < Values[1]; - for (i = 0; i < 2; i++) { - showpins(Apins[i], Dpins[i], Values[i], - (Values[i] < Values[!i]) ? "XM,XP: " : "YP,YM: "); - } + /* + Serial.println(F("Diagnosing as:-")); + for (i = 0; i < 2; i++) { + showpins(Apins[i], Dpins[i], Values[i], + (Values[i] < Values[!i]) ? "XM,XP: " : "YP,YM: "); + } + */ XM = Apins[!idx]; XP = Dpins[!idx]; YP = Apins[idx]; YM = Dpins[idx]; - //ts = TouchScreen(XP, YP, XM, YM, 300); //re-initialise with pins - ts = TouchScreen_kbv(XP, YP, XM, YM, 300); //re-initialise with pins + ts = TouchScreen(XP, YP, XM, YM, 300); //re-initialise with pins return true; //success } if (found == 0) Serial.println(F("MISSING TOUCHSCREEN")); //else Serial.println(F("BROKEN TOUCHSCREEN")); return false; } +#endif void setup() { char buf[40]; - uint16_t ID = tft.readID(); - tft.begin(ID); + uint16_t ID = readID(); + TFT_BEGIN(); tft.fillScreen(TFT_NAVY); tft.println("Waiting for Serial"); delay(1000); @@ -159,13 +183,12 @@ void setup() tft.fillScreen(TFT_BLUE); Serial.println(TITLE); bool ret = true; -#if defined(__arm__) || defined(ESP32) +#if USE_XPT2046 || defined(__arm__) || defined(ESP32) Serial.println(F("Not possible to diagnose Touch pins on ARM or ESP32")); #else ret = diagnose_pins(); //destroys TFT pin modes + TFT_BEGIN(); //start again #endif - //uint16_t ID = tft.readID(); - tft.begin(ID); tft.setRotation(TOUCH_ORIENTATION); dispx = tft.width(); dispy = tft.height(); @@ -181,44 +204,36 @@ void setup() void loop() { startup(); - int lft = 10, rt = dispx - 11, top = 10, bot = dispy - 11; - int mdx = dispx / 2, mdy = dispy / 2, tmp; + int x, y, cnt, idx = 0; tft.fillScreen(BLACK); - drawCrossHair(lft, top, GRAY); - drawCrossHair(mdx, top, GRAY); - drawCrossHair(rt, top, GRAY); - drawCrossHair(lft, mdy, GRAY); - drawCrossHair(rt, mdy, GRAY); - drawCrossHair(lft, bot, GRAY); - drawCrossHair(mdx, bot, GRAY); - drawCrossHair(rt, bot, GRAY); + for (x = 10, cnt = 0; x < dispx; x += (dispx - 20) / 2) { + for (y = 10; y < dispy; y += (dispy - 20) / 2) { + if (++cnt != 5) drawCrossHair(x, y, GRAY); + } + } centerprint("***********", text_y_center - 12); centerprint("***********", text_y_center + 12); - - calibrate(lft, top, 0, F(" LEFT, TOP, Pressure")); - calibrate(lft, mdy, 1, F(" LEFT, MIDH, Pressure")); - calibrate(lft, bot, 2, F(" LEFT, BOT, Pressure")); - calibrate(mdx, top, 3, F(" MIDW, TOP, Pressure")); - calibrate(mdx, bot, 4, F(" MIDW, BOT, Pressure")); - calibrate(rt, top, 5, F(" RT, TOP, Pressure")); - calibrate(rt, mdy, 6, F(" RT, MIDH, Pressure")); - calibrate(rt, bot, 7, F(" RT, BOT, Pressure")); + for (x = 10, cnt = 0; x < dispx; x += (dispx - 20) / 2) { + for (y = 10; y < dispy; y += (dispy - 20) / 2) { + if (++cnt != 5) calibrate(x, y, idx++, F(" X, Y, Pressure")); + } + } cals = (long(dispx - 1) << 12) + (dispy - 1); - if (TOUCH_ORIENTATION == PORTRAIT) swapxy = rx[2] - rx[0]; - else swapxy = ry[2] - ry[0]; - swapxy = (swapxy < -500 || swapxy > 500); - if ((TOUCH_ORIENTATION == PORTRAIT) ^ (swapxy != 0)) { - clx = (rx[0] + rx[1] + rx[2]); - crx = (rx[5] + rx[6] + rx[7]); - cty = (ry[0] + ry[3] + ry[5]); - cby = (ry[2] + ry[4] + ry[7]); - } else { - clx = (ry[0] + ry[1] + ry[2]); + swapxy = rx[2] - rx[0]; + //else swapxy = ry[2] - ry[0]; + swapxy = (swapxy < -400 || swapxy > 400); + if (swapxy != 0) { + clx = (ry[0] + ry[1] + ry[2]); //rotate 90 crx = (ry[5] + ry[6] + ry[7]); cty = (rx[0] + rx[3] + rx[5]); cby = (rx[2] + rx[4] + rx[7]); + } else { + clx = (rx[0] + rx[1] + rx[2]); //regular + crx = (rx[5] + rx[6] + rx[7]); + cty = (ry[0] + ry[3] + ry[5]); + cby = (ry[2] + ry[4] + ry[7]); } clx /= 3; crx /= 3; @@ -249,7 +264,7 @@ void readCoordinates() uint32_t tx = 0; uint32_t ty = 0; uint32_t tz = 0; - boolean OK = false; + bool OK = false; while (OK == false) { @@ -299,10 +314,9 @@ void calibrate(int x, int y, int i, String msg) drawCrossHair(x, y, GRAY_DONE); rx[i] = cx; ry[i] = cy; - Serial.print("\r\ncx="); Serial.print(cx); - Serial.print(" cy="); Serial.print(cy); - Serial.print(" cz="); Serial.print(cz); - if (msg) Serial.print(msg); + char buf[40]; + sprintf(buf, "\r\ncx=%ld cy=%ld cz=%ld %s", cx, cy, cz, msg.c_str()); + Serial.print(buf); while (ISPRESSED() == true) {} } @@ -332,15 +346,26 @@ void report() tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp; tmp = TS_WID, TS_WID = TS_HT, TS_HT = tmp; } - tft.setCursor(0, 120); - Serial.println("\n"); - sprintf(buf, "MCUFRIEND_kbv ID=0x%04X %d x %d", - tft.readID(), TS_WID, TS_HT); + sprintf(buf, "\n\n*** COPY-PASTE from Serial Terminal:"); + bofe(buf); + bool ts_landscape = (TOUCH_ORIENTATION == LANDSCAPE) ^ swapxy; +#if (USE_XPT2046) + sprintf(buf, "const int TS_LANDSCAPE=%d; //%s", ts_landscape, TITLE); + bofe(buf); +#else + if (ts_landscape) { + SWAP(XM, YP); + SWAP(XP, YM); + SWAP(TS_LEFT, TS_RT); + SWAP(TS_TOP, TS_BOT); + } + sprintf(buf, "const int XP=%d,XM=%s,YP=%s,YM=%d; //%dx%d ID=0x%04X", + XP, Aval(XM), Aval(YP), YM, TS_WID, TS_HT, readID()); + Serial.println(buf); + sprintf(buf, "\nTouch Pin Wiring XP=%d XM=%s YP=%s YM=%d", + XP, Aval(XM), Aval(YP), YM); tft.println(buf); - Serial.println(buf); - sprintf(buf, "const int XP=%d,XM=A%d,YP=A%d,YM=%d; //%dx%d ID=0x%04X", - XP, XM - A0, YP - A0, YM, TS_WID, TS_HT, tft.readID()); - Serial.println(buf); +#endif sprintf(buf, "const int TS_LEFT=%d,TS_RT=%d,TS_TOP=%d,TS_BOT=%d;", TS_LEFT, TS_RT, TS_TOP, TS_BOT); Serial.println(buf); @@ -349,30 +374,22 @@ void report() for (int orient = 0; orient < 2; orient++) { sprintf(buf, "\n%s CALIBRATION %d x %d", orient ? "LANDSCAPE" : "PORTRAIT ", TS_WID, TS_HT); - tft.println(buf); - Serial.println(buf); + bofe(buf); sprintf(buf, "x = map(p.%s, LEFT=%d, RT=%d, 0, %d)", orient ? "y" : "x", TS_LEFT, TS_RT, TS_WID); - tft.println(buf); - Serial.println(buf); + bofe(buf); sprintf(buf, "y = map(p.%s, TOP=%d, BOT=%d, 0, %d)", orient ? "x" : "y", TS_TOP, TS_BOT, TS_HT); - tft.println(buf); - Serial.println(buf); + bofe(buf); tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp; tmp = TS_WID, TS_WID = TS_HT, TS_HT = tmp; } - sprintf(buf, "\nTouch Pin Wiring XP=%d XM=%s YP=%s YM=%d", - XP, Aval(XM), Aval(YP), YM); - tft.println(buf); - Serial.println(buf); int16_t x_range = TS_LEFT - TS_RT, y_range = TS_TOP - TS_BOT; - if (abs(x_range) > 650 && abs(y_range) > 750) //LANDSCAPE + if (abs(x_range) > 500 && abs(y_range) > 650) //LANDSCAPE return; sprintf(buf, "\n*** UNUSUAL CALIBRATION RANGES %d %d", x_range, y_range); - tft.println(buf); - Serial.println(buf); + bofe(buf); #endif } @@ -411,7 +428,7 @@ void startup() tft.println(F("to the center of the WHITE")); tft.println(F("crosshair. Keep holding")); tft.println(F("until crosshair turns RED.")); - tft.println(F("Repeat for all crosshairs.")); + tft.println(F("Repeat for all crosshairs.\n")); tft.println(F("Report can be pasted from Serial\n")); tft.println(F("Touch screen to continue")); @@ -426,9 +443,8 @@ void fail() tft.println(F("Unable to read the position")); tft.println(F("of the press. This is a")); - tft.println(F("hardware issue and can")); - tft.println(F("not be corrected in")); - tft.println(F("software.")); + tft.println(F("hardware issue and can not")); + tft.println(F("be corrected in software.")); tft.println(F("check XP, XM pins with a multimeter")); tft.println(F("check YP, YM pins with a multimeter")); tft.println(F("should be about 300 ohms"));