PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

172 lines
4.7KB

  1. /* Touchscreen library for XPT2046 Touch Controller Chip
  2. * Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice, development funding notice, and this permission
  12. * notice shall be included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "XPT2046_Touchscreen.h"
  23. #define Z_THRESHOLD 400
  24. #define Z_THRESHOLD_INT 75
  25. #define MSEC_THRESHOLD 3
  26. #define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0)
  27. static XPT2046_Touchscreen *isrPinptr;
  28. void isrPin(void);
  29. bool XPT2046_Touchscreen::begin()
  30. {
  31. SPI.begin();
  32. pinMode(csPin, OUTPUT);
  33. digitalWrite(csPin, HIGH);
  34. if (255 != tirqPin) {
  35. pinMode( tirqPin, INPUT );
  36. attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
  37. isrPinptr = this;
  38. }
  39. return true;
  40. }
  41. ISR_PREFIX
  42. void isrPin( void )
  43. {
  44. XPT2046_Touchscreen *o = isrPinptr;
  45. o->isrWake = true;
  46. }
  47. TS_Point XPT2046_Touchscreen::getPoint()
  48. {
  49. update();
  50. return TS_Point(xraw, yraw, zraw);
  51. }
  52. bool XPT2046_Touchscreen::tirqTouched()
  53. {
  54. return (isrWake);
  55. }
  56. bool XPT2046_Touchscreen::touched()
  57. {
  58. update();
  59. return (zraw >= Z_THRESHOLD);
  60. }
  61. void XPT2046_Touchscreen::readData(uint16_t *x, uint16_t *y, uint8_t *z)
  62. {
  63. update();
  64. *x = xraw;
  65. *y = yraw;
  66. *z = zraw;
  67. }
  68. bool XPT2046_Touchscreen::bufferEmpty()
  69. {
  70. return ((millis() - msraw) < MSEC_THRESHOLD);
  71. }
  72. static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) {
  73. int16_t da, db, dc;
  74. int16_t reta = 0;
  75. if ( x > y ) da = x - y; else da = y - x;
  76. if ( x > z ) db = x - z; else db = z - x;
  77. if ( z > y ) dc = z - y; else dc = y - z;
  78. if ( da <= db && da <= dc ) reta = (x + y) >> 1;
  79. else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
  80. else reta = (y + z) >> 1; // else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;
  81. return (reta);
  82. }
  83. // TODO: perhaps a future version should offer an option for more oversampling,
  84. // with the RANSAC algorithm https://en.wikipedia.org/wiki/RANSAC
  85. void XPT2046_Touchscreen::update()
  86. {
  87. int16_t data[6];
  88. if (!isrWake) return;
  89. uint32_t now = millis();
  90. if (now - msraw < MSEC_THRESHOLD) return;
  91. SPI.beginTransaction(SPI_SETTING);
  92. digitalWrite(csPin, LOW);
  93. SPI.transfer(0xB1 /* Z1 */);
  94. int16_t z1 = SPI.transfer16(0xC1 /* Z2 */) >> 3;
  95. int z = z1 + 4095;
  96. int16_t z2 = SPI.transfer16(0x91 /* X */) >> 3;
  97. z -= z2;
  98. if (z >= Z_THRESHOLD) {
  99. SPI.transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
  100. data[0] = SPI.transfer16(0xD1 /* Y */) >> 3;
  101. data[1] = SPI.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
  102. data[2] = SPI.transfer16(0xD1 /* Y */) >> 3;
  103. data[3] = SPI.transfer16(0x91 /* X */) >> 3;
  104. }
  105. else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
  106. data[4] = SPI.transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
  107. data[5] = SPI.transfer16(0) >> 3;
  108. digitalWrite(csPin, HIGH);
  109. SPI.endTransaction();
  110. //Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2);
  111. if (z < 0) z = 0;
  112. if (z < Z_THRESHOLD) { // if ( !touched ) {
  113. // Serial.println();
  114. zraw = 0;
  115. if (z < Z_THRESHOLD_INT) { // if ( !touched ) {
  116. if (255 != tirqPin) isrWake = false;
  117. }
  118. return;
  119. }
  120. zraw = z;
  121. // Average pair with least distance between each measured x then y
  122. //Serial.printf(" z1=%d,z2=%d ", z1, z2);
  123. //Serial.printf("p=%d, %d,%d %d,%d %d,%d", zraw,
  124. //data[0], data[1], data[2], data[3], data[4], data[5]);
  125. int16_t x = besttwoavg( data[0], data[2], data[4] );
  126. int16_t y = besttwoavg( data[1], data[3], data[5] );
  127. //Serial.printf(" %d,%d", x, y);
  128. //Serial.println();
  129. if (z >= Z_THRESHOLD) {
  130. msraw = now; // good read completed, set wait
  131. switch (rotation) {
  132. case 0:
  133. xraw = 4095 - y;
  134. yraw = x;
  135. break;
  136. case 1:
  137. xraw = x;
  138. yraw = y;
  139. break;
  140. case 2:
  141. xraw = y;
  142. yraw = 4095 - x;
  143. break;
  144. default: // 3
  145. xraw = 4095 - x;
  146. yraw = 4095 - y;
  147. }
  148. }
  149. }