My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xpt2046.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "../../inc/MarlinConfigPre.h"
  20. #if ENABLED(TOUCH_BUTTONS)
  21. #include "xpt2046.h"
  22. #include "../../inc/MarlinConfig.h"
  23. #ifndef TOUCH_INT_PIN
  24. #define TOUCH_INT_PIN -1
  25. #endif
  26. #ifndef TOUCH_MISO_PIN
  27. #define TOUCH_MISO_PIN MISO_PIN
  28. #endif
  29. #ifndef TOUCH_MOSI_PIN
  30. #define TOUCH_MOSI_PIN MOSI_PIN
  31. #endif
  32. #ifndef TOUCH_SCK_PIN
  33. #define TOUCH_SCK_PIN SCK_PIN
  34. #endif
  35. #ifndef TOUCH_CS_PIN
  36. #define TOUCH_CS_PIN CS_PIN
  37. #endif
  38. XPT2046 touch;
  39. extern int8_t encoderDiff;
  40. void XPT2046::init() {
  41. SET_INPUT(TOUCH_MISO_PIN);
  42. SET_OUTPUT(TOUCH_MOSI_PIN);
  43. SET_OUTPUT(TOUCH_SCK_PIN);
  44. OUT_WRITE(TOUCH_CS_PIN, HIGH);
  45. #if PIN_EXISTS(TOUCH_INT)
  46. // Optional Pendrive interrupt pin
  47. SET_INPUT(TOUCH_INT_PIN);
  48. #endif
  49. // Read once to enable pendrive status pin
  50. getInTouch(XPT2046_X);
  51. }
  52. #include "../../lcd/ultralcd.h" // For EN_C bit mask
  53. uint8_t XPT2046::read_buttons() {
  54. int16_t tsoffsets[4] = { 0 };
  55. if (tsoffsets[0] + tsoffsets[1] == 0) {
  56. // Not yet set, so use defines as fallback...
  57. tsoffsets[0] = XPT2046_X_CALIBRATION;
  58. tsoffsets[1] = XPT2046_X_OFFSET;
  59. tsoffsets[2] = XPT2046_Y_CALIBRATION;
  60. tsoffsets[3] = XPT2046_Y_OFFSET;
  61. }
  62. // We rely on XPT2046 compatible mode to ADS7843, hence no Z1 and Z2 measurements possible.
  63. if (!isTouched()) return 0;
  64. const uint16_t x = uint16_t(((uint32_t(getInTouch(XPT2046_X))) * tsoffsets[0]) >> 16) + tsoffsets[1],
  65. y = uint16_t(((uint32_t(getInTouch(XPT2046_Y))) * tsoffsets[2]) >> 16) + tsoffsets[3];
  66. if (!isTouched()) return 0; // Fingers must still be on the TS for a valid read.
  67. if (y < 175 || y > 234) return 0;
  68. return WITHIN(x, 14, 77) ? EN_D
  69. : WITHIN(x, 90, 153) ? EN_A
  70. : WITHIN(x, 166, 229) ? EN_B
  71. : WITHIN(x, 242, 305) ? EN_C
  72. : 0;
  73. }
  74. bool XPT2046::isTouched() {
  75. return (
  76. #if PIN_EXISTS(TOUCH_INT)
  77. READ(TOUCH_INT_PIN) != HIGH
  78. #else
  79. getInTouch(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD
  80. #endif
  81. );
  82. }
  83. uint16_t XPT2046::getInTouch(const XPTCoordinate coordinate) {
  84. uint16_t data[3];
  85. OUT_WRITE(TOUCH_CS_PIN, LOW);
  86. const uint8_t coord = uint8_t(coordinate) | XPT2046_CONTROL | XPT2046_DFR_MODE;
  87. for (uint16_t i = 0; i < 3 ; i++) {
  88. for (uint8_t j = 0x80; j; j >>= 1) {
  89. WRITE(TOUCH_SCK_PIN, LOW);
  90. WRITE(TOUCH_MOSI_PIN, bool(coord & j));
  91. WRITE(TOUCH_SCK_PIN, HIGH);
  92. }
  93. data[i] = 0;
  94. for (uint16_t j = 0x8000; j; j >>= 1) {
  95. WRITE(TOUCH_SCK_PIN, LOW);
  96. if (READ(TOUCH_MISO_PIN)) data[i] |= j;
  97. WRITE(TOUCH_SCK_PIN, HIGH);
  98. }
  99. WRITE(TOUCH_SCK_PIN, LOW);
  100. data[i] >>= 4;
  101. }
  102. WRITE(TOUCH_CS_PIN, HIGH);
  103. uint16_t delta01 = _MAX(data[0], data[1]) - _MIN(data[0], data[1]),
  104. delta02 = _MAX(data[0], data[2]) - _MIN(data[0], data[2]),
  105. delta12 = _MAX(data[1], data[2]) - _MIN(data[1], data[2]);
  106. if (delta01 <= delta02 && delta01 <= delta12)
  107. return (data[0] + data[1]) >> 1;
  108. if (delta02 <= delta12)
  109. return (data[0] + data[2]) >> 1;
  110. return (data[1] + data[2]) >> 1;
  111. }
  112. bool XPT2046::getTouchPoint(uint16_t &x, uint16_t &y) {
  113. if (isTouched()) {
  114. x = getInTouch(XPT2046_X);
  115. y = getInTouch(XPT2046_Y);
  116. }
  117. return isTouched();
  118. }
  119. #endif // TOUCH_BUTTONS