添加触摸方向切换功能
This commit is contained in:
parent
c124688ab1
commit
5b8db52f74
|
|
@ -19,7 +19,10 @@ inline void TFT_eSPI::begin_touch_read_write(void){
|
|||
DMA_BUSY_CHECK;
|
||||
CS_H; // Just in case it has been left low
|
||||
#if defined(SPI_HAS_TRANSACTION) && defined(SUPPORT_TRANSACTIONS)
|
||||
if (locked) {locked = false; spi.beginTransaction(SPISettings(SPI_TOUCH_FREQUENCY, MSBFIRST, SPI_MODE0));}
|
||||
if (locked) {
|
||||
locked = false;
|
||||
spi.beginTransaction(SPISettings(SPI_TOUCH_FREQUENCY, MSBFIRST, SPI_MODE0));
|
||||
}
|
||||
#else
|
||||
spi.setFrequency(SPI_TOUCH_FREQUENCY);
|
||||
#endif
|
||||
|
|
@ -34,7 +37,12 @@ inline void TFT_eSPI::begin_touch_read_write(void){
|
|||
inline void TFT_eSPI::end_touch_read_write(void) {
|
||||
T_CS_H;
|
||||
#if defined(SPI_HAS_TRANSACTION) && defined(SUPPORT_TRANSACTIONS)
|
||||
if(!inTransaction) {if (!locked) {locked = true; spi.endTransaction();}}
|
||||
if (!inTransaction) {
|
||||
if (!locked) {
|
||||
locked = true;
|
||||
spi.endTransaction();
|
||||
}
|
||||
}
|
||||
#else
|
||||
spi.setFrequency(SPI_FREQUENCY);
|
||||
#endif
|
||||
|
|
@ -45,15 +53,21 @@ inline void TFT_eSPI::end_touch_read_write(void){
|
|||
** Function name: Legacy - deprecated
|
||||
** Description: Start/end transaction
|
||||
***************************************************************************************/
|
||||
void TFT_eSPI::spi_begin_touch() {begin_touch_read_write();}
|
||||
void TFT_eSPI::spi_end_touch() { end_touch_read_write();}
|
||||
|
||||
void TFT_eSPI::spi_begin_touch() {
|
||||
begin_touch_read_write();
|
||||
}
|
||||
void TFT_eSPI::spi_end_touch() {
|
||||
end_touch_read_write();
|
||||
}
|
||||
void TFT_eSPI::set_touch_rotation(uint8_t rotation) {
|
||||
_rotation = rotation;
|
||||
}
|
||||
/***************************************************************************************
|
||||
** Function name: getTouchRaw
|
||||
** Description: read raw touch position. Always returns true.
|
||||
***************************************************************************************/
|
||||
uint8_t TFT_eSPI::getTouchRaw(uint16_t* x, uint16_t* y) {
|
||||
uint16_t tmp;
|
||||
uint16_t tmp1, tmp2;
|
||||
|
||||
begin_touch_read_write();
|
||||
|
||||
|
|
@ -66,11 +80,11 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
|
|||
spi.transfer(0); // Read first 8 bits
|
||||
spi.transfer(0xd0); // Read last 8 bits and start new YP conversion
|
||||
|
||||
tmp = spi.transfer(0); // Read first 8 bits
|
||||
tmp = tmp <<5;
|
||||
tmp |= 0x1f & (spi.transfer(0x90)>>3); // Read last 8 bits and start new XP conversion
|
||||
tmp1 = spi.transfer(0); // Read first 8 bits
|
||||
tmp1 = tmp1 << 5;
|
||||
tmp1 |= 0x1f & (spi.transfer(0x90) >> 3); // Read last 8 bits and start new XP conversion
|
||||
|
||||
*x = tmp;
|
||||
//*x = tmp;
|
||||
|
||||
// Start XP sample request for y position, read 4 times and keep last sample
|
||||
spi.transfer(0); // Read first 8 bits
|
||||
|
|
@ -80,14 +94,30 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
|
|||
spi.transfer(0); // Read first 8 bits
|
||||
spi.transfer(0x90); // Read last 8 bits and start new XP conversion
|
||||
|
||||
tmp = spi.transfer(0); // Read first 8 bits
|
||||
tmp = tmp <<5;
|
||||
tmp |= 0x1f & (spi.transfer(0)>>3); // Read last 8 bits
|
||||
tmp2 = spi.transfer(0); // Read first 8 bits
|
||||
tmp2 = tmp2 << 5;
|
||||
tmp2 |= 0x1f & (spi.transfer(0) >> 3); // Read last 8 bits
|
||||
|
||||
*y = tmp;
|
||||
//*y = tmp;
|
||||
|
||||
switch (_rotation) {
|
||||
case 0:
|
||||
*x = 4095 - tmp2;
|
||||
*y = tmp1;
|
||||
break;
|
||||
case 1:
|
||||
*x = tmp1;
|
||||
*y = tmp2;
|
||||
break;
|
||||
case 2:
|
||||
*x = tmp2;
|
||||
*y = 4095 - tmp1;
|
||||
break;
|
||||
default: // 3
|
||||
*x = 4095 - tmp1;
|
||||
*y = 4095 - tmp2;
|
||||
}
|
||||
end_touch_read_write();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -96,7 +126,6 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
|
|||
** Description: read raw pressure on touchpad and return Z value.
|
||||
***************************************************************************************/
|
||||
uint16_t TFT_eSPI::getTouchRawZ(void) {
|
||||
|
||||
begin_touch_read_write();
|
||||
|
||||
// Z sample request
|
||||
|
|
@ -121,8 +150,7 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||
// Wait until pressure stops increasing to debounce pressure
|
||||
uint16_t z1 = 1;
|
||||
uint16_t z2 = 0;
|
||||
while (z1 > z2)
|
||||
{
|
||||
while (z1 > z2) {
|
||||
z2 = z1;
|
||||
z1 = getTouchRawZ();
|
||||
delay(1);
|
||||
|
|
@ -130,7 +158,8 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||
|
||||
// Serial.print("Z = ");Serial.println(z1);
|
||||
|
||||
if (z1 <= threshold) return false;
|
||||
if (z1 <= threshold)
|
||||
return false;
|
||||
|
||||
getTouchRaw(&x_tmp, &y_tmp);
|
||||
|
||||
|
|
@ -138,7 +167,8 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||
// Serial.print(", Z = ");Serial.println(z1);
|
||||
|
||||
delay(1); // Small delay to the next sample
|
||||
if (getTouchRawZ() <= threshold) return false;
|
||||
if (getTouchRawZ() <= threshold)
|
||||
return false;
|
||||
|
||||
delay(2); // Small delay to the next sample
|
||||
getTouchRaw(&x_tmp2, &y_tmp2);
|
||||
|
|
@ -146,8 +176,10 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||
// Serial.print("Sample 2 x,y = "); Serial.print(x_tmp2);Serial.print(",");Serial.println(y_tmp2);
|
||||
// Serial.print("Sample difference = ");Serial.print(abs(x_tmp - x_tmp2));Serial.print(",");Serial.println(abs(y_tmp - y_tmp2));
|
||||
|
||||
if (abs(x_tmp - x_tmp2) > _RAWERR) return false;
|
||||
if (abs(y_tmp - y_tmp2) > _RAWERR) return false;
|
||||
if (abs(x_tmp - x_tmp2) > _RAWERR)
|
||||
return false;
|
||||
if (abs(y_tmp - y_tmp2) > _RAWERR)
|
||||
return false;
|
||||
|
||||
*x = x_tmp;
|
||||
*y = y_tmp;
|
||||
|
|
@ -163,23 +195,30 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||
uint8_t TFT_eSPI::getTouch(uint16_t* x, uint16_t* y, uint16_t threshold) {
|
||||
uint16_t x_tmp, y_tmp;
|
||||
|
||||
if (threshold<20) threshold = 20;
|
||||
if (_pressTime > millis()) threshold=20;
|
||||
if (threshold < 20)
|
||||
threshold = 20;
|
||||
if (_pressTime > millis())
|
||||
threshold = 20;
|
||||
|
||||
uint8_t n = 5;
|
||||
uint8_t valid = 0;
|
||||
while (n--)
|
||||
{
|
||||
if (validTouch(&x_tmp, &y_tmp, threshold)) valid++;;
|
||||
while (n--) {
|
||||
if (validTouch(&x_tmp, &y_tmp, threshold))
|
||||
valid++;
|
||||
;
|
||||
}
|
||||
|
||||
if (valid<1) { _pressTime = 0; return false; }
|
||||
if (valid < 1) {
|
||||
_pressTime = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
_pressTime = millis() + 50;
|
||||
|
||||
convertRawXY(&x_tmp, &y_tmp);
|
||||
|
||||
if (x_tmp >= _width || y_tmp >= _height) return false;
|
||||
if (x_tmp >= _width || y_tmp >= _height)
|
||||
return false;
|
||||
|
||||
_pressX = x_tmp;
|
||||
_pressY = y_tmp;
|
||||
|
|
@ -192,8 +231,7 @@ uint8_t TFT_eSPI::getTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||
** Function name: convertRawXY
|
||||
** Description: convert raw touch x,y values to screen coordinates
|
||||
***************************************************************************************/
|
||||
void TFT_eSPI::convertRawXY(uint16_t *x, uint16_t *y)
|
||||
{
|
||||
void TFT_eSPI::convertRawXY(uint16_t* x, uint16_t* y) {
|
||||
uint16_t x_tmp = *x, y_tmp = *y, xx, yy;
|
||||
|
||||
if (!touchCalibration_rotate) {
|
||||
|
|
@ -223,15 +261,14 @@ void TFT_eSPI::calibrateTouch(uint16_t *parameters, uint32_t color_fg, uint32_t
|
|||
int16_t values[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
uint16_t x_tmp, y_tmp;
|
||||
|
||||
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
fillRect(0, 0, size + 1, size + 1, color_bg);
|
||||
fillRect(0, _height - size - 1, size + 1, size + 1, color_bg);
|
||||
fillRect(_width - size - 1, 0, size + 1, size + 1, color_bg);
|
||||
fillRect(_width - size - 1, _height - size - 1, size + 1, size + 1, color_bg);
|
||||
|
||||
if (i == 5) break; // used to clear the arrows
|
||||
if (i == 5)
|
||||
break; // used to clear the arrows
|
||||
|
||||
switch (i) {
|
||||
case 0: // up left
|
||||
|
|
@ -257,11 +294,13 @@ void TFT_eSPI::calibrateTouch(uint16_t *parameters, uint32_t color_fg, uint32_t
|
|||
}
|
||||
|
||||
// user has to get the chance to release
|
||||
if(i>0) delay(1000);
|
||||
if (i > 0)
|
||||
delay(1000);
|
||||
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
// Use a lower detect threshold as corners tend to be less sensitive
|
||||
while(!validTouch(&x_tmp, &y_tmp, Z_THRESHOLD/2));
|
||||
while (!validTouch(&x_tmp, &y_tmp, Z_THRESHOLD / 2))
|
||||
;
|
||||
values[i * 2] += x_tmp;
|
||||
values[i * 2 + 1] += y_tmp;
|
||||
}
|
||||
|
|
@ -269,7 +308,6 @@ void TFT_eSPI::calibrateTouch(uint16_t *parameters, uint32_t color_fg, uint32_t
|
|||
values[i * 2 + 1] /= 8;
|
||||
}
|
||||
|
||||
|
||||
// from case 0 to case 1, the y value changed.
|
||||
// If the measured delta of the touch x axis is bigger than the delta of the y axis, the touch and TFT axes are switched.
|
||||
touchCalibration_rotate = false;
|
||||
|
|
@ -306,10 +344,14 @@ void TFT_eSPI::calibrateTouch(uint16_t *parameters, uint32_t color_fg, uint32_t
|
|||
touchCalibration_x1 -= touchCalibration_x0;
|
||||
touchCalibration_y1 -= touchCalibration_y0;
|
||||
|
||||
if(touchCalibration_x0 == 0) touchCalibration_x0 = 1;
|
||||
if(touchCalibration_x1 == 0) touchCalibration_x1 = 1;
|
||||
if(touchCalibration_y0 == 0) touchCalibration_y0 = 1;
|
||||
if(touchCalibration_y1 == 0) touchCalibration_y1 = 1;
|
||||
if (touchCalibration_x0 == 0)
|
||||
touchCalibration_x0 = 1;
|
||||
if (touchCalibration_x1 == 0)
|
||||
touchCalibration_x1 = 1;
|
||||
if (touchCalibration_y0 == 0)
|
||||
touchCalibration_y0 = 1;
|
||||
if (touchCalibration_y1 == 0)
|
||||
touchCalibration_y1 = 1;
|
||||
|
||||
// export parameters, if pointer valid
|
||||
if (parameters != NULL) {
|
||||
|
|
@ -321,7 +363,6 @@ void TFT_eSPI::calibrateTouch(uint16_t *parameters, uint32_t color_fg, uint32_t
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************************
|
||||
** Function name: setTouch
|
||||
** Description: imports calibration parameters for touchscreen.
|
||||
|
|
@ -332,10 +373,14 @@ void TFT_eSPI::setTouch(uint16_t *parameters){
|
|||
touchCalibration_y0 = parameters[2];
|
||||
touchCalibration_y1 = parameters[3];
|
||||
|
||||
if(touchCalibration_x0 == 0) touchCalibration_x0 = 1;
|
||||
if(touchCalibration_x1 == 0) touchCalibration_x1 = 1;
|
||||
if(touchCalibration_y0 == 0) touchCalibration_y0 = 1;
|
||||
if(touchCalibration_y1 == 0) touchCalibration_y1 = 1;
|
||||
if (touchCalibration_x0 == 0)
|
||||
touchCalibration_x0 = 1;
|
||||
if (touchCalibration_x1 == 0)
|
||||
touchCalibration_x1 = 1;
|
||||
if (touchCalibration_y0 == 0)
|
||||
touchCalibration_y0 = 1;
|
||||
if (touchCalibration_y1 == 0)
|
||||
touchCalibration_y1 = 1;
|
||||
|
||||
touchCalibration_rotate = parameters[4] & 0x01;
|
||||
touchCalibration_invert_x = parameters[4] & 0x02;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,9 @@
|
|||
/*
|
||||
* @Date: 2020-07-23 12:53:53
|
||||
* @LastEditors: xfdr0805
|
||||
* @LastEditTime: 2020-07-23 13:04:02
|
||||
* @FilePath: \ESP8266_Weather_Station_Color\.pio\libdeps\esp12e\TFT_eSPI\Extensions\Touch.h
|
||||
*/
|
||||
// Coded by Bodmer 10/2/18, see license in root directory.
|
||||
// This is part of the TFT_eSPI class and is associated with the Touch Screen handlers
|
||||
|
||||
|
|
@ -16,6 +22,7 @@
|
|||
void calibrateTouch(uint16_t* data, uint32_t color_fg, uint32_t color_bg, uint8_t size);
|
||||
// Set the screen calibration values
|
||||
void setTouch(uint16_t* data);
|
||||
void set_touch_rotation(uint8_t rotation);
|
||||
|
||||
private:
|
||||
// Legacy support only - deprecated TODO: delete
|
||||
|
|
@ -32,6 +39,6 @@
|
|||
// Initialise with example calibration values so processor does not crash if setTouch() not called in setup()
|
||||
uint16_t touchCalibration_x0 = 300, touchCalibration_x1 = 3600, touchCalibration_y0 = 300, touchCalibration_y1 = 3600;
|
||||
uint8_t touchCalibration_rotate = 1, touchCalibration_invert_x = 2, touchCalibration_invert_y = 0;
|
||||
|
||||
uint8_t _rotation = 0; //
|
||||
uint32_t _pressTime; // Press and hold time-out
|
||||
uint16_t _pressX, _pressY; // For future use (last sampled calibrated coordinates)
|
||||
|
|
|
|||
Loading…
Reference in New Issue