mirror of
https://github.com/YuzuZensai/MCUFRIEND_kbv.git
synced 2026-01-31 14:57:48 +00:00
remove old Touch examples, add Touch_shield_new
This commit is contained in:
@@ -1,455 +0,0 @@
|
||||
// .kbv: previous sketch wasted too much SRAM with const strings
|
||||
// .kbv: replace all UTFT print const strings with F(string)
|
||||
// .kbv: correct UTFTGLUE settextcursor() for String print
|
||||
|
||||
// https://forum.arduino.cc/index.php?topic=473885.msg3245748#msg3245748
|
||||
// file attached 03 May 2017 21:15 BST
|
||||
|
||||
|
||||
// TouchScreen_Calibr_kbv for MCUFRIEND UNO Display Shields
|
||||
// adapted by David Prentice
|
||||
// for Adafruit's <TouchScreen.h> Resistive Touch Screen Library
|
||||
// from Henning Karlsen's original program. Many Thanks.
|
||||
|
||||
// UTouch_Calibration (C)2013-2014 Henning Karlsen
|
||||
// web: http://www.henningkarlsen.com/electronics
|
||||
//
|
||||
// This program can be used to calibrate the touchscreen
|
||||
// of the display modules.
|
||||
//
|
||||
// It is assumed that the display module is connected to an
|
||||
// appropriate shield or that you know how to change the pin
|
||||
// numbers in the setup.
|
||||
//
|
||||
// Instructions will be given on the display.
|
||||
|
||||
//#define TOUCH_ORIENTATION LANDSCAPE
|
||||
#define TOUCH_ORIENTATION PORTRAIT
|
||||
#define TITLE "TouchScreen.h Calibration"
|
||||
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <UTFTGLUE.h> //we are using UTFT display methods
|
||||
UTFTGLUE myGLCD(0x9341, A2, A1, A3, A4, A0);
|
||||
|
||||
// MCUFRIEND UNO shield shares pins with the TFT. Due does NOT work
|
||||
|
||||
//int XP = 6, XM = A2, YP = A1, YM = 7; //most common configuration (dead Due)
|
||||
int XP = 7, XM = A1, YP = A2, YM = 6; //most common configuration (WHITE Due)
|
||||
|
||||
//#include "TouchScreen_kbv.h" //Local Library
|
||||
//TouchScreen_kbv myTouch(XP, YP, XM, YM, 300);
|
||||
#include <TouchScreen.h> //Global Library
|
||||
TouchScreen myTouch(XP, YP, XM, YM, 300);
|
||||
TSPoint tp; //Touchscreen_due branch uses Point
|
||||
|
||||
void readResistiveTouch(void)
|
||||
{
|
||||
tp = myTouch.getPoint();
|
||||
pinMode(YP, OUTPUT); //restore shared pins
|
||||
pinMode(XM, OUTPUT);
|
||||
}
|
||||
|
||||
bool ISPRESSED(void)
|
||||
{
|
||||
// .kbv this was too sensitive !!
|
||||
// now touch has to be stable for 50ms
|
||||
int count = 0;
|
||||
bool state, oldstate;
|
||||
while (count < 10) {
|
||||
readResistiveTouch();
|
||||
state = tp.z > 180; // .kbv was >20 && < 1000
|
||||
if (state == oldstate) count++;
|
||||
else count = 0;
|
||||
oldstate = state;
|
||||
delay(5);
|
||||
}
|
||||
return oldstate;
|
||||
}
|
||||
|
||||
void showpoint(void)
|
||||
{
|
||||
Serial.print("\r\nx="); Serial.print(tp.x);
|
||||
Serial.print(" y="); Serial.print(tp.y);
|
||||
Serial.print(" z="); Serial.print(tp.z);
|
||||
}
|
||||
|
||||
// ************************************
|
||||
// DO NOT EDIT ANYTHING BELOW THIS LINE
|
||||
// ************************************
|
||||
|
||||
// Declare which fonts we will be using
|
||||
#if defined(SmallFont) //.kbv new GLUE defines FreeSmallFont
|
||||
#undef SmallFont //.kbv lose it
|
||||
#define SmallFont NULL //.kbv use System Font
|
||||
#else
|
||||
extern uint8_t SmallFont[]; //.kbv
|
||||
#endif
|
||||
|
||||
uint32_t cx, cy;
|
||||
uint32_t rx[8], ry[8];
|
||||
int32_t clx, crx, cty, cby;
|
||||
float px, py;
|
||||
int dispx, dispy, text_y_center, swapxy;
|
||||
uint32_t calx, caly, cals;
|
||||
char buf[13];
|
||||
|
||||
void showpins(int A, int D, int value, const char *msg)
|
||||
{
|
||||
char buf[40];
|
||||
sprintf(buf, "%s (A%d, D%d) = %d", msg, A - A0, D, value);
|
||||
Serial.println(buf);
|
||||
}
|
||||
|
||||
boolean diagnose_pins()
|
||||
{
|
||||
int i, j, value, Apins[2], Dpins[2], Values[2], found = 0;
|
||||
// Serial.begin(9600);
|
||||
Serial.println("Making all control and bus pins INPUT_PULLUP");
|
||||
Serial.println("Typical 30k Analog pullup with corresponding pin");
|
||||
Serial.println("would read low when digital is written LOW");
|
||||
Serial.println("e.g. reads ~25 for 300R X direction");
|
||||
Serial.println("e.g. reads ~30 for 500R Y direction");
|
||||
Serial.println("");
|
||||
for (i = A0; i < A5; i++) pinMode(i, INPUT_PULLUP);
|
||||
for (i = 2; i < 10; i++) pinMode(i, INPUT_PULLUP);
|
||||
for (i = A0; i < A4; i++) {
|
||||
for (j = 5; j < 10; j++) {
|
||||
pinMode(j, OUTPUT);
|
||||
digitalWrite(j, LOW);
|
||||
value = analogRead(i); // ignore first reading
|
||||
value = analogRead(i);
|
||||
if (value < 100) {
|
||||
showpins(i, j, value, "Testing :");
|
||||
if (found < 2) {
|
||||
Apins[found] = i;
|
||||
Dpins[found] = j;
|
||||
Values[found] = value;
|
||||
found++;
|
||||
}
|
||||
}
|
||||
pinMode(j, INPUT_PULLUP);
|
||||
}
|
||||
}
|
||||
if (found == 2) {
|
||||
Serial.println("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: ");
|
||||
}
|
||||
XM = Apins[!idx]; XP = Dpins[!idx]; YP = Apins[idx]; YM = Dpins[idx];
|
||||
// myTouch = TouchScreen_kbv(XP, YP, XM, YM, 300); //Local library
|
||||
myTouch = TouchScreen(XP, YP, XM, YM, 300); //Global library
|
||||
return true;
|
||||
}
|
||||
Serial.println("BROKEN TOUCHSCREEN");
|
||||
return false;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println(TITLE);
|
||||
bool ret = true;
|
||||
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega2560__)
|
||||
ret = diagnose_pins();
|
||||
#else
|
||||
Serial.println("Only AVR can diagnose Touch Pins");
|
||||
Serial.println("Other targets use default Pins");
|
||||
char buf[40];
|
||||
sprintf(buf, "Touch Pin Wiring XP=%d XM=A%d YP=A%d YM=%d",
|
||||
XP, XM - A0, YP - A0, YM);
|
||||
Serial.println(buf);
|
||||
#endif
|
||||
digitalWrite(A0, HIGH);
|
||||
pinMode(A0, OUTPUT);
|
||||
myGLCD.InitLCD(TOUCH_ORIENTATION);
|
||||
myGLCD.clrScr();
|
||||
myGLCD.setFont(SmallFont);
|
||||
dispx = myGLCD.getDisplayXSize();
|
||||
dispy = myGLCD.getDisplayYSize();
|
||||
text_y_center = (dispy / 2) - 6;
|
||||
if (ret == false) {
|
||||
myGLCD.print(F("BROKEN TOUCHSCREEN"), CENTER, dispy / 2);
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void drawCrossHair(int x, int y)
|
||||
{
|
||||
myGLCD.drawRect(x - 10, y - 10, x + 10, y + 10);
|
||||
myGLCD.drawLine(x - 5, y, x + 5, y);
|
||||
myGLCD.drawLine(x, y - 5, x, y + 5);
|
||||
}
|
||||
|
||||
void readCoordinates()
|
||||
{
|
||||
int iter = 5000;
|
||||
int failcount = 0;
|
||||
int cnt = 0;
|
||||
uint32_t tx = 0;
|
||||
uint32_t ty = 0;
|
||||
boolean OK = false;
|
||||
|
||||
while (OK == false)
|
||||
{
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
myGLCD.print(F("* PRESS *"), CENTER, text_y_center);
|
||||
while (ISPRESSED() == false) {}
|
||||
myGLCD.print(F("* HOLD! *"), CENTER, text_y_center);
|
||||
cnt = 0;
|
||||
iter = 400;
|
||||
do
|
||||
{
|
||||
readResistiveTouch();
|
||||
// showpoint(tp);
|
||||
if (tp.z > 20 && tp.z < 1000)
|
||||
{
|
||||
tx += tp.x;
|
||||
ty += tp.y;
|
||||
cnt++;
|
||||
}
|
||||
else
|
||||
failcount++;
|
||||
} while ((cnt < iter) && (failcount < 10000));
|
||||
if (cnt >= iter)
|
||||
{
|
||||
OK = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
tx = 0;
|
||||
ty = 0;
|
||||
cnt = 0;
|
||||
}
|
||||
if (failcount >= 10000)
|
||||
fail();
|
||||
}
|
||||
|
||||
cx = tx / iter;
|
||||
cy = ty / iter;
|
||||
}
|
||||
|
||||
void calibrate(int x, int y, int i)
|
||||
{
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
drawCrossHair(x, y);
|
||||
myGLCD.setBackColor(255, 0, 0);
|
||||
readCoordinates();
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
myGLCD.print(F("* RELEASE *"), CENTER, text_y_center);
|
||||
myGLCD.setColor(80, 80, 80);
|
||||
drawCrossHair(x, y);
|
||||
rx[i] = cx;
|
||||
ry[i] = cy;
|
||||
Serial.print("\r\ncx="); Serial.print(cx);
|
||||
Serial.print(" cy="); Serial.print(cy);
|
||||
while (ISPRESSED() == true) {}
|
||||
}
|
||||
|
||||
void waitForTouch()
|
||||
{
|
||||
while (ISPRESSED() == true) {}
|
||||
while (ISPRESSED() == false) {}
|
||||
while (ISPRESSED() == true) {}
|
||||
}
|
||||
|
||||
void toHex(uint32_t num)
|
||||
{
|
||||
buf[0] = '0';
|
||||
buf[1] = 'x';
|
||||
buf[10] = 'U';
|
||||
buf[11] = 'L';
|
||||
buf[12] = 0;
|
||||
for (int zz = 9; zz > 1; zz--)
|
||||
{
|
||||
if ((num & 0xF) > 9)
|
||||
buf[zz] = (num & 0xF) + 55;
|
||||
else
|
||||
buf[zz] = (num & 0xF) + 48;
|
||||
num = num >> 4;
|
||||
}
|
||||
}
|
||||
|
||||
void startup()
|
||||
{
|
||||
myGLCD.setColor(255, 0, 0);
|
||||
myGLCD.fillRect(0, 0, dispx - 1, 13);
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
myGLCD.setBackColor(255, 0, 0);
|
||||
myGLCD.drawLine(0, 14, dispx - 1, 14);
|
||||
myGLCD.print(TITLE, CENTER, 1);
|
||||
myGLCD.setBackColor(0, 0, 0);
|
||||
|
||||
myGLCD.print(F("#define NUMSAMPLES 3 in Library"), LEFT, 18);
|
||||
myGLCD.print(F("Use a stylus or something"), LEFT, 30);
|
||||
myGLCD.print(F("similar to touch as close"), LEFT, 42);
|
||||
myGLCD.print(F("to the center of the"), LEFT, 54);
|
||||
myGLCD.print(F("highlighted crosshair as"), LEFT, 66);
|
||||
myGLCD.print(F("possible. Keep as still as"), LEFT, 78);
|
||||
myGLCD.print(F("possible and keep holding"), LEFT, 90);
|
||||
myGLCD.print(F("until the highlight is"), LEFT, 102);
|
||||
myGLCD.print(F("removed. Repeat for all"), LEFT, 114);
|
||||
myGLCD.print(F("crosshairs in sequence."), LEFT, 126);
|
||||
myGLCD.print(F("Touch screen to continue"), CENTER, 162);
|
||||
|
||||
waitForTouch();
|
||||
}
|
||||
|
||||
void showNumI(char *msg, uint32_t val, int x, int y)
|
||||
{
|
||||
myGLCD.print(msg, x, y);
|
||||
myGLCD.printNumI(val, x + 50, y);
|
||||
}
|
||||
|
||||
void done()
|
||||
{
|
||||
uint16_t TS_LEFT, TS_RT, TS_TOP, TS_BOT, TS_WID, TS_HT, TS_SWAP;
|
||||
int16_t tmp;
|
||||
char buf[60];
|
||||
myGLCD.clrScr();
|
||||
myGLCD.setColor(255, 0, 0);
|
||||
myGLCD.fillRect(0, 0, dispx - 1, 13);
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
myGLCD.setBackColor(255, 0, 0);
|
||||
myGLCD.drawLine(0, 14, dispx - 1, 14);
|
||||
myGLCD.print(TITLE, CENTER, 1);
|
||||
myGLCD.setBackColor(0, 0, 0);
|
||||
myGLCD.print(F("To use the new calibration"), LEFT, 30);
|
||||
myGLCD.print(F("settings you must map the values"), LEFT, 42);
|
||||
myGLCD.print(F("from Point p = ts.getPoint() e.g. "), LEFT, 54);
|
||||
myGLCD.print(F("x = map(p.x, LEFT, RT, 0, tft.width());"), LEFT, 66);
|
||||
myGLCD.print(F("y = map(p.y, TOP, BOT, 0, tft.height());"), LEFT, 78);
|
||||
myGLCD.print(F("swap p.x and p.y if diff ORIENTATION"), LEFT, 90);
|
||||
|
||||
//.kbv show human values
|
||||
TS_LEFT = (calx >> 14) & 0x3FFF;
|
||||
TS_RT = (calx >> 0) & 0x3FFF;
|
||||
TS_TOP = (caly >> 14) & 0x3FFF;
|
||||
TS_BOT = (caly >> 0) & 0x3FFF;
|
||||
TS_WID = ((cals >> 12) & 0x0FFF) + 1;
|
||||
TS_HT = ((cals >> 0) & 0x0FFF) + 1;
|
||||
TS_SWAP = (cals >> 31);
|
||||
int y = 120;
|
||||
Serial.println("");
|
||||
sprintf(buf, "Sketch is %s %d x %d",
|
||||
TOUCH_ORIENTATION ? "LANDSCAPE" : "PORTRAIT", TS_WID, TS_HT);
|
||||
myGLCD.print(buf, 0, y);
|
||||
Serial.println(buf);
|
||||
if (TOUCH_ORIENTATION == LANDSCAPE) { //always show PORTRAIT first
|
||||
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;
|
||||
}
|
||||
sprintf(buf, "PORTRAIT CALIBRATION %d x %d", TS_WID, TS_HT);
|
||||
myGLCD.print(buf, 0, y += 24);
|
||||
Serial.println(buf);
|
||||
sprintf(buf, "x = map(p.x, LEFT=%d, RT=%d, 0, %d)", TS_LEFT, TS_RT, TS_WID);
|
||||
myGLCD.print(buf, 0, y += 12);
|
||||
Serial.println(buf);
|
||||
sprintf(buf, "y = map(p.y, TOP=%d, BOT=%d, 0, %d)", TS_TOP, TS_BOT, TS_HT);
|
||||
myGLCD.print(buf, 0, y += 12);
|
||||
Serial.println(buf);
|
||||
sprintf(buf, "Touch Pin Wiring XP=%d XM=A%d YP=A%d YM=%d",
|
||||
XP, XM - A0, YP - A0, YM);
|
||||
myGLCD.print(buf, 0, y += 24);
|
||||
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);
|
||||
myGLCD.print(buf, 0, y += 24);
|
||||
Serial.println(buf);
|
||||
sprintf(buf, "x = map(p.y, LEFT=%d, RT=%d, 0, %d)", TS_LEFT, TS_RT, TS_WID);
|
||||
myGLCD.print(buf, 0, y += 12);
|
||||
Serial.println(buf);
|
||||
sprintf(buf, "y = map(p.x, TOP=%d, BOT=%d, 0, %d)", TS_TOP, TS_BOT, TS_HT);
|
||||
myGLCD.print(buf, 0, y += 12);
|
||||
Serial.println(buf);
|
||||
}
|
||||
|
||||
void fail()
|
||||
{
|
||||
myGLCD.clrScr();
|
||||
myGLCD.setColor(255, 0, 0);
|
||||
myGLCD.fillRect(0, 0, dispx - 1, 13);
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
myGLCD.setBackColor(255, 0, 0);
|
||||
myGLCD.drawLine(0, 14, dispx - 1, 14);
|
||||
myGLCD.print(F("Touch Calibration FAILED"), CENTER, 1);
|
||||
myGLCD.setBackColor(0, 0, 0);
|
||||
|
||||
myGLCD.print(F("Unable to read the position"), LEFT, 30);
|
||||
myGLCD.print(F("of the press. This is a"), LEFT, 42);
|
||||
myGLCD.print(F("hardware issue and can"), LEFT, 54);
|
||||
myGLCD.print(F("not be corrected in"), LEFT, 66);
|
||||
myGLCD.print(F("software."), LEFT, 78);
|
||||
myGLCD.print(F("check XP, XM pins with a multimeter"), LEFT, 102);
|
||||
myGLCD.print(F("check YP, YM pins with a multimeter"), LEFT, 114);
|
||||
myGLCD.print(F("should be about 300 ohms"), LEFT, 126);
|
||||
|
||||
while (true) {};
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
startup();
|
||||
|
||||
delay(100);
|
||||
myGLCD.clrScr();
|
||||
myGLCD.setColor(80, 80, 80);
|
||||
drawCrossHair(dispx - 11, 10);
|
||||
drawCrossHair(dispx / 2, 10);
|
||||
drawCrossHair(10, 10);
|
||||
drawCrossHair(dispx - 11, dispy / 2);
|
||||
drawCrossHair(10, dispy / 2);
|
||||
drawCrossHair(dispx - 11, dispy - 11);
|
||||
drawCrossHair(dispx / 2, dispy - 11);
|
||||
drawCrossHair(10, dispy - 11);
|
||||
myGLCD.setColor(255, 255, 255);
|
||||
myGLCD.setBackColor(255, 0, 0);
|
||||
myGLCD.print(F("***********"), CENTER, text_y_center - 12);
|
||||
myGLCD.print(F("***********"), CENTER, text_y_center + 12);
|
||||
|
||||
calibrate(10, 10, 0);
|
||||
calibrate(10, dispy / 2, 1);
|
||||
calibrate(10, dispy - 11, 2);
|
||||
calibrate(dispx / 2, 10, 3);
|
||||
calibrate(dispx / 2, dispy - 11, 4);
|
||||
calibrate(dispx - 11, 10, 5);
|
||||
calibrate(dispx - 11, dispy / 2, 6);
|
||||
calibrate(dispx - 11, dispy - 11, 7);
|
||||
|
||||
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]) / 3;
|
||||
crx = (rx[5] + rx[6] + rx[7]) / 3;
|
||||
cty = (ry[0] + ry[3] + ry[5]) / 3;
|
||||
cby = (ry[2] + ry[4] + ry[7]) / 3;
|
||||
} else {
|
||||
clx = (ry[0] + ry[1] + ry[2]) / 3;
|
||||
crx = (ry[5] + ry[6] + ry[7]) / 3;
|
||||
cty = (rx[0] + rx[3] + rx[5]) / 3;
|
||||
cby = (rx[2] + rx[4] + rx[7]) / 3;
|
||||
}
|
||||
px = float(crx - clx) / (dispx - 20);
|
||||
py = float(cby - cty) / (dispy - 20);
|
||||
// px = 0;
|
||||
clx -= px * 10;
|
||||
crx += px * 10;
|
||||
cty -= py * 10;
|
||||
cby += py * 10;
|
||||
|
||||
calx = (long(clx) << 14) + long(crx);
|
||||
caly = (long(cty) << 14) + long(cby);
|
||||
if (swapxy)
|
||||
cals |= (1L << 31);
|
||||
|
||||
done();
|
||||
while (true) {}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,57 +2,25 @@
|
||||
|
||||
// different mcufriend shields have Touchscreen on different pins
|
||||
// and rotation.
|
||||
// Run the UTouch_calibr_kbv sketch for calibration of your shield
|
||||
// Run the TouchScreen_Calibr_native sketch for calibration of your shield
|
||||
|
||||
#include <Adafruit_GFX.h> // Core graphics library
|
||||
//#include <Adafruit_TFTLCD.h> // Hardware-specific library
|
||||
//Adafruit_TFTLCD tft(A3, A2, A1, A0, A4);
|
||||
#include <MCUFRIEND_kbv.h>
|
||||
MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway.
|
||||
#include <TouchScreen.h>
|
||||
|
||||
#if defined(__SAM3X8E__)
|
||||
#undef __FlashStringHelper::F(string_literal)
|
||||
#define F(string_literal) string_literal
|
||||
#endif
|
||||
char *name = "Please Calibrate."; //edit name of shield
|
||||
const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341
|
||||
const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139;
|
||||
|
||||
// These are the pins for some typical shields!
|
||||
// S6D0154: YP=A1, XM=A2, YM=7, XP=6
|
||||
// ST7783 : YP=A2, XM=A1, YM=6, XP=7
|
||||
// ILI9320: YP=A2, XM=A3, YM=8, XP=9
|
||||
// ILI9325: YP=A1, XM=A2, YM=7, XP=6
|
||||
// ILI9325BG: YP=A2, XM=A1, YM=6, XP=7
|
||||
// ILI9341: YP=A2, XM=A1, YM=7, XP=6
|
||||
// ILI9488: YP=A1, XM=A2, YM=7, XP=6
|
||||
// R65109V: YP=A2, XM=A1, YM=6, XP=7
|
||||
|
||||
// most mcufriend shields use these pins and Portrait mode:
|
||||
uint8_t YP = A1; // must be an analog pin, use "An" notation!
|
||||
uint8_t XM = A2; // must be an analog pin, use "An" notation!
|
||||
uint8_t YM = 7; // can be a digital pin
|
||||
uint8_t XP = 6; // can be a digital pin
|
||||
uint8_t SwapXY = 0;
|
||||
|
||||
uint16_t TS_LEFT = 920;
|
||||
uint16_t TS_RT = 150;
|
||||
uint16_t TS_TOP = 940;
|
||||
uint16_t TS_BOT = 120;
|
||||
char *name = "Unknown controller";
|
||||
|
||||
// For better pressure precision, we need to know the resistance
|
||||
// between X+ and X- Use any multimeter to read it
|
||||
// For the one we're using, its 300 ohms across the X plate
|
||||
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
|
||||
TSPoint tp;
|
||||
|
||||
#define MINPRESSURE 20
|
||||
#define MINPRESSURE 200
|
||||
#define MAXPRESSURE 1000
|
||||
|
||||
#define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}
|
||||
|
||||
int16_t BOXSIZE;
|
||||
int16_t PENRADIUS = 3;
|
||||
uint16_t identifier, oldcolor, currentcolor;
|
||||
int16_t PENRADIUS = 1;
|
||||
uint16_t ID, oldcolor, currentcolor;
|
||||
uint8_t Orientation = 0; //PORTRAIT
|
||||
|
||||
// Assign human-readable names to some common 16-bit color values:
|
||||
@@ -67,17 +35,19 @@ uint8_t Orientation = 0; //PORTRAIT
|
||||
|
||||
void show_Serial(void)
|
||||
{
|
||||
Serial.print(F("Found "));
|
||||
Serial.print(name);
|
||||
Serial.println(F(" LCD driver"));
|
||||
Serial.println(F("Most Touch Screens use pins 6, 7, A1, A2"));
|
||||
Serial.println(F("But they can be in ANY order"));
|
||||
Serial.println(F("e.g. right to left or bottom to top"));
|
||||
Serial.println(F("or wrong direction"));
|
||||
Serial.println(F("Edit name and calibration statements\n"));
|
||||
Serial.println(name);
|
||||
Serial.print(F("ID=0x"));
|
||||
Serial.println(identifier, HEX);
|
||||
Serial.println(ID, HEX);
|
||||
Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
|
||||
Serial.println("Calibration is: ");
|
||||
Serial.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
|
||||
Serial.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
|
||||
Serial.print("Wiring is: ");
|
||||
Serial.println(SwapXY ? "SWAPXY" : "PORTRAIT");
|
||||
Serial.println("Wiring is always PORTRAIT");
|
||||
Serial.println("YP=" + String(YP) + " XM=" + String(XM));
|
||||
Serial.println("YM=" + String(YM) + " XP=" + String(XP));
|
||||
}
|
||||
@@ -85,23 +55,18 @@ void show_Serial(void)
|
||||
void show_tft(void)
|
||||
{
|
||||
tft.setCursor(0, 0);
|
||||
tft.setTextSize(2);
|
||||
tft.print(F("Found "));
|
||||
tft.print(name);
|
||||
tft.println(F(" LCD"));
|
||||
tft.setTextSize(1);
|
||||
tft.print(F("ID=0x"));
|
||||
tft.println(identifier, HEX);
|
||||
tft.println(ID, HEX);
|
||||
tft.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
|
||||
tft.println("Calibration is: ");
|
||||
tft.println("");
|
||||
tft.setTextSize(2);
|
||||
tft.println(name);
|
||||
tft.setTextSize(1);
|
||||
tft.println("PORTRAIT Values:");
|
||||
tft.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
|
||||
tft.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
|
||||
tft.print("\nWiring is: ");
|
||||
if (SwapXY) {
|
||||
tft.setTextColor(CYAN);
|
||||
tft.setTextSize(2);
|
||||
}
|
||||
tft.println(SwapXY ? "SWAPXY" : "PORTRAIT");
|
||||
tft.println("\nWiring is: ");
|
||||
tft.println("YP=" + String(YP) + " XM=" + String(XM));
|
||||
tft.println("YM=" + String(YM) + " XP=" + String(XP));
|
||||
tft.setTextSize(2);
|
||||
@@ -115,8 +80,6 @@ void show_tft(void)
|
||||
tp = ts.getPoint();
|
||||
pinMode(XM, OUTPUT);
|
||||
pinMode(YP, OUTPUT);
|
||||
pinMode(XP, OUTPUT);
|
||||
pinMode(YM, OUTPUT);
|
||||
if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
|
||||
if (tp.x > 450 && tp.x < 570 && tp.y > 450 && tp.y < 570) break;
|
||||
tft.setCursor(0, (tft.height() * 3) / 4);
|
||||
@@ -128,80 +91,11 @@ void show_tft(void)
|
||||
void setup(void)
|
||||
{
|
||||
uint16_t tmp;
|
||||
Serial.begin(9600);
|
||||
|
||||
tft.reset();
|
||||
identifier = tft.readID();
|
||||
// if (identifier == 0) identifier = 0x9341;
|
||||
if (0) {
|
||||
} else if (identifier == 0x0154) {
|
||||
name = "S6D0154";
|
||||
TS_LEFT = 914; TS_RT = 181; TS_TOP = 957; TS_BOT = 208;
|
||||
} else if (identifier == 0x5408) { //thanks gazialankus
|
||||
name = "SPFD5408";
|
||||
TS_LEFT = 150; TS_RT = 960; TS_TOP = 155; TS_BOT = 925;
|
||||
SwapXY = 1;
|
||||
} else if (identifier == 0x7783) {
|
||||
name = "ST7781";
|
||||
TS_LEFT = 865; TS_RT = 155; TS_TOP = 942; TS_BOT = 153;
|
||||
SwapXY = 1;
|
||||
} else if (identifier == 0x7789) {
|
||||
name = "ST7789V";
|
||||
YP = A2; XM = A1; YM = 7; XP = 6;
|
||||
TS_LEFT = 906; TS_RT = 169; TS_TOP = 161; TS_BOT = 919;
|
||||
} else if (identifier == 0x9320) {
|
||||
name = "ILI9320";
|
||||
YP = A3; XM = A2; YM = 9; XP = 8;
|
||||
TS_LEFT = 902; TS_RT = 137; TS_TOP = 941; TS_BOT = 134;
|
||||
} else if (identifier == 0x9325) {
|
||||
name = "ILI9325";
|
||||
TS_LEFT = 900; TS_RT = 103; TS_TOP = 96; TS_BOT = 904;
|
||||
} else if (identifier == 0x9325) {
|
||||
name = "ILI9325 Green Dog";
|
||||
TS_LEFT = 900; TS_RT = 130; TS_TOP = 940; TS_BOT = 130;
|
||||
} else if (identifier == 0x9327) {
|
||||
name = "ILI9327";
|
||||
TS_LEFT = 899; TS_RT = 135; TS_TOP = 935; TS_BOT = 79;
|
||||
SwapXY = 1;
|
||||
} else if (identifier == 0x9329) {
|
||||
name = "ILI9329";
|
||||
TS_LEFT = 143; TS_RT = 885; TS_TOP = 941; TS_BOT = 131;
|
||||
SwapXY = 1;
|
||||
} else if (identifier == 0x9341) {
|
||||
name = "ILI9341 BLUE";
|
||||
TS_LEFT = 920; TS_RT = 139; TS_TOP = 944; TS_BOT = 150;
|
||||
SwapXY = 0;
|
||||
} else if (identifier == 0) {
|
||||
name = "ILI9341 DealExtreme";
|
||||
TS_LEFT = 893; TS_RT = 145; TS_TOP = 930; TS_BOT = 135;
|
||||
SwapXY = 1;
|
||||
} else if (identifier == 0 || identifier == 0x9341) {
|
||||
name = "ILI9341";
|
||||
TS_LEFT = 128; TS_RT = 911; TS_TOP = 105; TS_BOT = 908;
|
||||
SwapXY = 1;
|
||||
} else if (identifier == 0x9486) {
|
||||
name = "ILI9486";
|
||||
TS_LEFT = 904; TS_RT = 170; TS_TOP = 950; TS_BOT = 158;
|
||||
} else if (identifier == 0x9488) {
|
||||
name = "ILI9488";
|
||||
TS_LEFT = 904; TS_RT = 170; TS_TOP = 950; TS_BOT = 158;
|
||||
} else if (identifier == 0xB509) {
|
||||
name = "R61509V";
|
||||
TS_LEFT = 889; TS_RT = 149; TS_TOP = 106; TS_BOT = 975;
|
||||
SwapXY = 1;
|
||||
} else {
|
||||
name = "unknown";
|
||||
}
|
||||
switch (Orientation) { // adjust for different aspects
|
||||
case 0: break; //no change, calibrated for PORTRAIT
|
||||
case 1: tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp; break;
|
||||
case 2: SWAP(TS_LEFT, TS_RT); SWAP(TS_TOP, TS_BOT); break;
|
||||
case 3: tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp; break;
|
||||
}
|
||||
|
||||
ID = tft.readID();
|
||||
tft.begin(ID);
|
||||
Serial.begin(9600);
|
||||
ts = TouchScreen(XP, YP, XM, YM, 300); //call the constructor AGAIN with new values.
|
||||
tft.begin(identifier);
|
||||
show_Serial();
|
||||
tft.setRotation(Orientation);
|
||||
tft.fillScreen(BLACK);
|
||||
@@ -230,22 +124,34 @@ void loop()
|
||||
// if sharing pins, you'll need to fix the directions of the touchscreen pins
|
||||
pinMode(XM, OUTPUT);
|
||||
pinMode(YP, OUTPUT);
|
||||
pinMode(XP, OUTPUT);
|
||||
pinMode(YM, OUTPUT);
|
||||
// digitalWrite(XM, HIGH);
|
||||
// digitalWrite(YP, HIGH);
|
||||
// we have some minimum pressure we consider 'valid'
|
||||
// pressure of 0 means no pressing!
|
||||
|
||||
if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
|
||||
// is controller wired for Landscape ? or are we oriented in Landscape?
|
||||
if (SwapXY != (Orientation & 1)) SWAP(tp.x, tp.y);
|
||||
// scale from 0->1023 to tft.width i.e. left = 0, rt = width
|
||||
// most mcufriend have touch (with icons) that extends below the TFT
|
||||
// screens without icons need to reserve a space for "erase"
|
||||
// scale the ADC values from ts.getPoint() to screen values e.g. 0-239
|
||||
xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
|
||||
ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
|
||||
//
|
||||
// Calibration is true for PORTRAIT. tp.y is always long dimension
|
||||
// map to your current pixel orientation
|
||||
switch (Orientation) {
|
||||
case 0:
|
||||
xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
|
||||
ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
|
||||
break;
|
||||
case 1:
|
||||
xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width());
|
||||
ypos = map(tp.x, TS_RT, TS_LEFT, 0, tft.height());
|
||||
break;
|
||||
case 2:
|
||||
xpos = map(tp.x, TS_RT, TS_LEFT, 0, tft.width());
|
||||
ypos = map(tp.y, TS_BOT, TS_TOP, 0, tft.height());
|
||||
break;
|
||||
case 3:
|
||||
xpos = map(tp.y, TS_BOT, TS_TOP, 0, tft.width());
|
||||
ypos = map(tp.y, TS_LEFT, TS_RT, 0, tft.height());
|
||||
break;
|
||||
}
|
||||
|
||||
// are we in top color box area ?
|
||||
if (ypos < BOXSIZE) { //draw white border on selected color box
|
||||
@@ -285,6 +191,8 @@ void loop()
|
||||
tft.fillCircle(xpos, ypos, PENRADIUS, currentcolor);
|
||||
}
|
||||
// are we in erase area ?
|
||||
// Plain Touch panels use bottom 10 pixels e.g. > h - 10
|
||||
// Touch panels with icon area e.g. > h - 0
|
||||
if (ypos > tft.height() - 10) {
|
||||
// press the bottom of the screen to erase
|
||||
tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
|
||||
Reference in New Issue
Block a user