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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(void) {
  41. SET_INPUT(TOUCH_MISO_PIN);
  42. SET_OUTPUT(TOUCH_MOSI_PIN);
  43. SET_OUTPUT(TOUCH_SCK_PIN);
  44. OUT_WRITE(TOUCH_CS_PIN, 1);
  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. static uint32_t timeout = 0;
  56. if (PENDING(millis(), timeout)) return 0;
  57. timeout = millis() + 250;
  58. if (tsoffsets[0] + tsoffsets[1] == 0) {
  59. // Not yet set, so use defines as fallback...
  60. tsoffsets[0] = XPT2046_X_CALIBRATION;
  61. tsoffsets[1] = XPT2046_X_OFFSET;
  62. tsoffsets[2] = XPT2046_Y_CALIBRATION;
  63. tsoffsets[3] = XPT2046_Y_OFFSET;
  64. }
  65. // We rely on XPT2046 compatible mode to ADS7843, hence no Z1 and Z2 measurements possible.
  66. if (!isTouched()) return 0;
  67. const uint16_t x = uint16_t(((uint32_t(getInTouch(XPT2046_X))) * tsoffsets[0]) >> 16) + tsoffsets[1],
  68. y = uint16_t(((uint32_t(getInTouch(XPT2046_Y))) * tsoffsets[2]) >> 16) + tsoffsets[3];
  69. if (!isTouched()) return 0; // Fingers must still be on the TS for a valid read.
  70. if (y < 185 || y > 224) return 0;
  71. if (WITHIN(x, 21, 98)) encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * ENCODER_PULSES_PER_STEP;
  72. else if (WITHIN(x, 121, 198)) encoderDiff = ENCODER_STEPS_PER_MENU_ITEM * ENCODER_PULSES_PER_STEP;
  73. else if (WITHIN(x, 221, 298)) return EN_C;
  74. return 0;
  75. }
  76. bool XPT2046::isTouched() {
  77. return (
  78. #if PIN_EXISTS(TOUCH_INT)
  79. READ(TOUCH_INT_PIN) != HIGH
  80. #else
  81. getInTouch(XPT2046_Z1) >= XPT2046_Z1_TRESHHOLD
  82. #endif
  83. );
  84. }
  85. uint16_t XPT2046::getInTouch(const XPTCoordinate coordinate) {
  86. uint16_t data[3];
  87. OUT_WRITE(TOUCH_CS_PIN, LOW);
  88. const uint8_t coord = uint8_t(coordinate) | XPT2046_CONTROL | XPT2046_DFR_MODE;
  89. for (uint16_t i = 0; i < 3 ; i++) {
  90. for (uint8_t j = 0x80; j; j >>= 1) {
  91. WRITE(TOUCH_SCK_PIN, LOW);
  92. WRITE(TOUCH_MOSI_PIN, bool(coord & j));
  93. WRITE(TOUCH_SCK_PIN, HIGH);
  94. }
  95. data[i] = 0;
  96. for (uint16_t j = 0x8000; j; j >>= 1) {
  97. WRITE(TOUCH_SCK_PIN, LOW);
  98. if (READ(TOUCH_MISO_PIN)) data[i] |= j;
  99. WRITE(TOUCH_SCK_PIN, HIGH);
  100. }
  101. WRITE(TOUCH_SCK_PIN, LOW);
  102. data[i] >>= 4;
  103. }
  104. WRITE(TOUCH_CS_PIN, HIGH);
  105. uint16_t delta01 = _MAX(data[0], data[1]) - _MIN(data[0], data[1]),
  106. delta02 = _MAX(data[0], data[2]) - _MIN(data[0], data[2]),
  107. delta12 = _MAX(data[1], data[2]) - _MIN(data[1], data[2]);
  108. if (delta01 <= delta02 && delta01 <= delta12)
  109. return (data[0] + data[1]) >> 1;
  110. if (delta02 <= delta12)
  111. return (data[0] + data[2]) >> 1;
  112. return (data[1] + data[2]) >> 1;
  113. }
  114. #endif // TOUCH_BUTTONS