conditional smaller for Leonardo, tidy up loop(), report()

This commit is contained in:
prenticedavid
2020-03-02 15:39:58 +00:00
committed by GitHub
parent 9449aec688
commit 0d740a9dbe

View File

@@ -14,6 +14,10 @@
#include <MCUFRIEND_kbv.h> #include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft; MCUFRIEND_kbv tft;
#if defined(USB_PID) && USB_PID == 0x804E // Arduino M0 Native
#define Serial SerialUSB
#endif
// MCUFRIEND UNO shield shares pins with the TFT. // MCUFRIEND UNO shield shares pins with the TFT.
#if defined(ESP32) #if defined(ESP32)
int XP = 27, YP = 4, XM = 15, YM = 14; //most common configuration int XP = 27, YP = 4, XM = 15, YM = 14; //most common configuration
@@ -95,12 +99,14 @@ void showpins(int A, int D, int value, const char *msg)
boolean diagnose_pins() boolean diagnose_pins()
{ {
int i, j, value, Apins[2], Dpins[2], Values[2], found = 0; int i, j, value, Apins[2], Dpins[2], Values[2], found = 0;
Serial.println(F("Making all control and bus pins INPUT_PULLUP")); Serial.println(F("Making all control and bus pins INPUT_PULLUP"));
Serial.println(F("Typical 30k Analog pullup with corresponding pin")); Serial.println(F("Typical 30k Analog pullup with corresponding pin"));
Serial.println(F("would read low when digital is written LOW")); Serial.println(F("would read low when digital is written LOW"));
Serial.println(F("e.g. reads ~25 for 300R X direction")); Serial.println(F("e.g. reads ~25 for 300R X direction"));
Serial.println(F("e.g. reads ~30 for 500R Y direction")); Serial.println(F("e.g. reads ~30 for 500R Y direction"));
Serial.println(F("")); Serial.println(F(""));
for (i = A0; i < A5; i++) pinMode(i, INPUT_PULLUP); for (i = A0; i < A5; i++) pinMode(i, INPUT_PULLUP);
for (i = 2; i < 10; i++) pinMode(i, INPUT_PULLUP); for (i = 2; i < 10; i++) pinMode(i, INPUT_PULLUP);
for (i = A0; i < A4; i++) { for (i = A0; i < A4; i++) {
@@ -131,79 +137,93 @@ boolean diagnose_pins()
(Values[i] < Values[!i]) ? "XM,XP: " : "YP,YM: "); (Values[i] < Values[!i]) ? "XM,XP: " : "YP,YM: ");
} }
XM = Apins[!idx]; XP = Dpins[!idx]; YP = Apins[idx]; YM = Dpins[idx]; 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(XP, YP, XM, YM, 300); //re-initialise with pins
ts = TouchScreen_kbv(XP, YP, XM, YM, 300); //re-initialise with pins ts = TouchScreen_kbv(XP, YP, XM, YM, 300); //re-initialise with pins
return true; //success return true; //success
} }
if (found == 0) Serial.println(F("MISSING TOUCHSCREEN")); if (found == 0) Serial.println(F("MISSING TOUCHSCREEN"));
else Serial.println(F("BROKEN TOUCHSCREEN")); //else Serial.println(F("BROKEN TOUCHSCREEN"));
return false; return false;
} }
void setup() void setup()
{ {
char buf[40];
uint16_t ID = tft.readID();
tft.begin(ID);
tft.fillScreen(TFT_NAVY);
tft.println("Waiting for Serial");
delay(1000);
Serial.begin(9600); Serial.begin(9600);
while (!Serial);
tft.fillScreen(TFT_BLUE);
Serial.println(TITLE); Serial.println(TITLE);
bool ret = true; bool ret = true;
#if defined(__arm__) || defined(ESP32) #if defined(__arm__) || defined(ESP32)
Serial.println(F("Not possible to diagnose Touch pins on ARM or ESP32")); Serial.println(F("Not possible to diagnose Touch pins on ARM or ESP32"));
#else #else
ret = diagnose_pins(); ret = diagnose_pins(); //destroys TFT pin modes
#endif #endif
uint16_t ID = tft.readID(); //uint16_t ID = tft.readID();
Serial.print("ID = 0x");
Serial.println(ID, HEX);
tft.begin(ID); tft.begin(ID);
tft.setRotation(TOUCH_ORIENTATION); tft.setRotation(TOUCH_ORIENTATION);
dispx = tft.width(); dispx = tft.width();
dispy = tft.height(); dispy = tft.height();
text_y_center = (dispy / 2) - 6; text_y_center = (dispy / 2) - 6;
sprintf(buf, "ID = 0x%04x", ID);
Serial.println(buf);
if (ret == false) { if (ret == false) {
centerprint("BROKEN TOUCHSCREEN", text_y_center); centerprint("BROKEN TOUCHSCREEN", text_y_center);
while (true); //just tread water fail();
} }
} }
void loop() void loop()
{ {
startup(); startup();
int lft = 10, rt = dispx - 11, top = 10, bot = dispy - 11;
int mdx = dispx / 2, mdy = dispy / 2, tmp;
tft.fillScreen(BLACK); tft.fillScreen(BLACK);
drawCrossHair(dispx - 11, 10, GRAY); drawCrossHair(lft, top, GRAY);
drawCrossHair(dispx / 2, 10, GRAY); drawCrossHair(mdx, top, GRAY);
drawCrossHair(10, 10, GRAY); drawCrossHair(rt, top, GRAY);
drawCrossHair(dispx - 11, dispy / 2, GRAY); drawCrossHair(lft, mdy, GRAY);
drawCrossHair(10, dispy / 2, GRAY); drawCrossHair(rt, mdy, GRAY);
drawCrossHair(dispx - 11, dispy - 11, GRAY); drawCrossHair(lft, bot, GRAY);
drawCrossHair(dispx / 2, dispy - 11, GRAY); drawCrossHair(mdx, bot, GRAY);
drawCrossHair(10, dispy - 11, GRAY); drawCrossHair(rt, bot, GRAY);
centerprint("***********", text_y_center - 12); centerprint("***********", text_y_center - 12);
centerprint("***********", text_y_center + 12); centerprint("***********", text_y_center + 12);
calibrate(10, 10, 0, F(" LEFT, TOP, Pressure")); calibrate(lft, top, 0, F(" LEFT, TOP, Pressure"));
calibrate(10, dispy / 2, 1, F(" LEFT, MIDH, Pressure")); calibrate(lft, mdy, 1, F(" LEFT, MIDH, Pressure"));
calibrate(10, dispy - 11, 2, F(" LEFT, BOT, Pressure")); calibrate(lft, bot, 2, F(" LEFT, BOT, Pressure"));
calibrate(dispx / 2, 10, 3, F(" MIDW, TOP, Pressure")); calibrate(mdx, top, 3, F(" MIDW, TOP, Pressure"));
calibrate(dispx / 2, dispy - 11, 4, F(" MIDW, BOT, Pressure")); calibrate(mdx, bot, 4, F(" MIDW, BOT, Pressure"));
calibrate(dispx - 11, 10, 5, F(" RT, TOP, Pressure")); calibrate(rt, top, 5, F(" RT, TOP, Pressure"));
calibrate(dispx - 11, dispy / 2, 6, F(" RT, MIDH, Pressure")); calibrate(rt, mdy, 6, F(" RT, MIDH, Pressure"));
calibrate(dispx - 11, dispy - 11, 7, F(" RT, BOT, Pressure")); calibrate(rt, bot, 7, F(" RT, BOT, Pressure"));
cals = (long(dispx - 1) << 12) + (dispy - 1); cals = (long(dispx - 1) << 12) + (dispy - 1);
if (TOUCH_ORIENTATION == PORTRAIT) swapxy = rx[2] - rx[0]; if (TOUCH_ORIENTATION == PORTRAIT) swapxy = rx[2] - rx[0];
else swapxy = ry[2] - ry[0]; else swapxy = ry[2] - ry[0];
swapxy = (swapxy < -500 || swapxy > 500); swapxy = (swapxy < -500 || swapxy > 500);
if ((TOUCH_ORIENTATION == PORTRAIT) ^ (swapxy != 0)) { if ((TOUCH_ORIENTATION == PORTRAIT) ^ (swapxy != 0)) {
clx = (rx[0] + rx[1] + rx[2]) / 3; clx = (rx[0] + rx[1] + rx[2]);
crx = (rx[5] + rx[6] + rx[7]) / 3; crx = (rx[5] + rx[6] + rx[7]);
cty = (ry[0] + ry[3] + ry[5]) / 3; cty = (ry[0] + ry[3] + ry[5]);
cby = (ry[2] + ry[4] + ry[7]) / 3; cby = (ry[2] + ry[4] + ry[7]);
} else { } else {
clx = (ry[0] + ry[1] + ry[2]) / 3; clx = (ry[0] + ry[1] + ry[2]);
crx = (ry[5] + ry[6] + ry[7]) / 3; crx = (ry[5] + ry[6] + ry[7]);
cty = (rx[0] + rx[3] + rx[5]) / 3; cty = (rx[0] + rx[3] + rx[5]);
cby = (rx[2] + rx[4] + rx[7]) / 3; cby = (rx[2] + rx[4] + rx[7]);
} }
clx /= 3;
crx /= 3;
cty /= 3;
cby /= 3;
px = float(crx - clx) / (dispx - 20); px = float(crx - clx) / (dispx - 20);
py = float(cby - cty) / (dispy - 20); py = float(cby - cty) / (dispy - 20);
// px = 0; // px = 0;
@@ -228,6 +248,7 @@ void readCoordinates()
int cnt = 0; int cnt = 0;
uint32_t tx = 0; uint32_t tx = 0;
uint32_t ty = 0; uint32_t ty = 0;
uint32_t tz = 0;
boolean OK = false; boolean OK = false;
while (OK == false) while (OK == false)
@@ -240,10 +261,11 @@ void readCoordinates()
do do
{ {
readResistiveTouch(); readResistiveTouch();
if (tp.z > 20) if (tp.z > 200) //.kbv
{ {
tx += tp.x; tx += tp.x;
ty += tp.y; ty += tp.y;
tz += tp.z;
cnt++; cnt++;
} }
else else
@@ -257,6 +279,7 @@ void readCoordinates()
{ {
tx = 0; tx = 0;
ty = 0; ty = 0;
tz = 0;
cnt = 0; cnt = 0;
} }
if (failcount >= 10000) if (failcount >= 10000)
@@ -265,7 +288,7 @@ void readCoordinates()
cx = tx / iter; cx = tx / iter;
cy = ty / iter; cy = ty / iter;
cz = tp.z; cz = tz / iter;
} }
void calibrate(int x, int y, int i, String msg) void calibrate(int x, int y, int i, String msg)
@@ -310,53 +333,47 @@ void report()
tmp = TS_WID, TS_WID = TS_HT, TS_HT = tmp; tmp = TS_WID, TS_WID = TS_HT, TS_HT = tmp;
} }
tft.setCursor(0, 120); tft.setCursor(0, 120);
Serial.println(""); Serial.println("\n");
sprintf(buf, "MCUFRIEND_kbv ID=0x%04X %d x %d", sprintf(buf, "MCUFRIEND_kbv ID=0x%04X %d x %d",
tft.readID(), TS_WID, TS_HT); tft.readID(), TS_WID, TS_HT);
tft.println(buf); tft.println(buf);
Serial.println(buf); Serial.println(buf);
sprintf(buf, "\nconst int XP=%d,XM=A%d,YP=A%d,YM=%d; //%dx%d ID=0x%04X", 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()); XP, XM - A0, YP - A0, YM, TS_WID, TS_HT, tft.readID());
Serial.println(buf); Serial.println(buf);
sprintf(buf, "const int TS_LEFT=%d,TS_RT=%d,TS_TOP=%d,TS_BOT=%d;", sprintf(buf, "const int TS_LEFT=%d,TS_RT=%d,TS_TOP=%d,TS_BOT=%d;",
TS_LEFT, TS_RT, TS_TOP, TS_BOT); TS_LEFT, TS_RT, TS_TOP, TS_BOT);
Serial.println(buf); Serial.println(buf);
sprintf(buf, "PORTRAIT CALIBRATION %d x %d", TS_WID, TS_HT);
tft.println(""); #if !defined(ARDUINO_AVR_LEONARDO)
tft.println(buf); for (int orient = 0; orient < 2; orient++) {
Serial.println(buf); sprintf(buf, "\n%s CALIBRATION %d x %d",
sprintf(buf, "x = map(p.x, LEFT=%d, RT=%d, 0, %d)", TS_LEFT, TS_RT, TS_WID); orient ? "LANDSCAPE" : "PORTRAIT ", TS_WID, TS_HT);
tft.println(buf); tft.println(buf);
Serial.println(buf); Serial.println(buf);
sprintf(buf, "y = map(p.y, TOP=%d, BOT=%d, 0, %d)", TS_TOP, TS_BOT, TS_HT); sprintf(buf, "x = map(p.%s, LEFT=%d, RT=%d, 0, %d)",
tft.println(buf); orient ? "y" : "x", TS_LEFT, TS_RT, TS_WID);
Serial.println(buf); tft.println(buf);
sprintf(buf, "Touch Pin Wiring XP=%d XM=%s YP=%s YM=%d", Serial.println(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);
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); XP, Aval(XM), Aval(YP), YM);
tft.println("");
tft.println(buf);
Serial.println(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, "LANDSCAPE CALIBRATION %d x %d", TS_WID, TS_HT);
tft.println("");
tft.println(buf);
Serial.println(buf);
sprintf(buf, "x = map(p.y, LEFT=%d, RT=%d, 0, %d)", TS_LEFT, TS_RT, TS_WID);
tft.println(buf);
Serial.println(buf);
sprintf(buf, "y = map(p.x, TOP=%d, BOT=%d, 0, %d)", TS_TOP, TS_BOT, TS_HT);
tft.println(buf); tft.println(buf);
Serial.println(buf); Serial.println(buf);
int16_t x_range = TS_LEFT - TS_RT, y_range = TS_TOP - TS_BOT; int16_t x_range = TS_LEFT - TS_RT, y_range = TS_TOP - TS_BOT;
if (abs(x_range) > 800 && abs(y_range) > 700) //LANDSCAPE if (abs(x_range) > 650 && abs(y_range) > 750) //LANDSCAPE
return; return;
sprintf(buf, "\n*** UNUSUAL CALIBRATION RANGES %d %d", x_range, y_range); sprintf(buf, "\n*** UNUSUAL CALIBRATION RANGES %d %d", x_range, y_range);
tft.println(buf); tft.println(buf);
Serial.println(buf); Serial.println(buf);
#endif
} }
void drawCrossHair(int x, int y, uint16_t color) void drawCrossHair(int x, int y, uint16_t color)
@@ -391,13 +408,10 @@ void startup()
tft.println(F("#define NUMSAMPLES 3 in Library\n")); tft.println(F("#define NUMSAMPLES 3 in Library\n"));
tft.println(F("Use a stylus or something")); tft.println(F("Use a stylus or something"));
tft.println(F("similar to touch as close")); tft.println(F("similar to touch as close"));
tft.println(F("to the center of the")); tft.println(F("to the center of the WHITE"));
tft.println(F("highlighted crosshair as")); tft.println(F("crosshair. Keep holding"));
tft.println(F("possible. Keep as still as")); tft.println(F("until crosshair turns RED."));
tft.println(F("possible and keep holding")); tft.println(F("Repeat for all crosshairs."));
tft.println(F("until the highlight is"));
tft.println(F("removed. Repeat for all"));
tft.println(F("crosshairs in sequence.\n"));
tft.println(F("Report can be pasted from Serial\n")); tft.println(F("Report can be pasted from Serial\n"));
tft.println(F("Touch screen to continue")); tft.println(F("Touch screen to continue"));