My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

touch_calibration.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  24. #include "touch_calibration.h"
  25. #define TOUCH_CALIBRATION_MAX_RETRIES 5
  26. #define DEBUG_OUT ENABLED(DEBUG_TOUCH_CALIBRATION)
  27. #include "../../core/debug_out.h"
  28. #if ENABLED(TOUCH_CALIBRATION_AUTO_SAVE)
  29. #include "../../module/settings.h"
  30. #endif
  31. TouchCalibration touch_calibration;
  32. touch_calibration_t TouchCalibration::calibration;
  33. calibrationState TouchCalibration::calibration_state = CALIBRATION_NONE;
  34. touch_calibration_point_t TouchCalibration::calibration_points[4];
  35. uint8_t TouchCalibration::failed_count;
  36. void TouchCalibration::validate_calibration() {
  37. #define VALIDATE_PRECISION(XY, A, B) validate_precision_##XY(CALIBRATION_##A, CALIBRATION_##B)
  38. const bool landscape = VALIDATE_PRECISION(x, TOP_LEFT, BOTTOM_LEFT)
  39. && VALIDATE_PRECISION(x, TOP_RIGHT, BOTTOM_RIGHT)
  40. && VALIDATE_PRECISION(y, TOP_LEFT, TOP_RIGHT)
  41. && VALIDATE_PRECISION(y, BOTTOM_LEFT, BOTTOM_RIGHT);
  42. const bool portrait = VALIDATE_PRECISION(y, TOP_LEFT, BOTTOM_LEFT)
  43. && VALIDATE_PRECISION(y, TOP_RIGHT, BOTTOM_RIGHT)
  44. && VALIDATE_PRECISION(x, TOP_LEFT, TOP_RIGHT)
  45. && VALIDATE_PRECISION(x, BOTTOM_LEFT, BOTTOM_RIGHT);
  46. #undef VALIDATE_PRECISION
  47. #define CAL_PTS(N) calibration_points[CALIBRATION_##N]
  48. if (landscape) {
  49. calibration_state = CALIBRATION_SUCCESS;
  50. calibration.x = ((CAL_PTS(TOP_RIGHT).x - CAL_PTS(TOP_LEFT).x) << 17) / (CAL_PTS(BOTTOM_RIGHT).raw_x + CAL_PTS(TOP_RIGHT).raw_x - CAL_PTS(BOTTOM_LEFT).raw_x - CAL_PTS(TOP_LEFT).raw_x);
  51. calibration.y = ((CAL_PTS(BOTTOM_LEFT).y - CAL_PTS(TOP_LEFT).y) << 17) / (CAL_PTS(BOTTOM_RIGHT).raw_y - CAL_PTS(TOP_RIGHT).raw_y + CAL_PTS(BOTTOM_LEFT).raw_y - CAL_PTS(TOP_LEFT).raw_y);
  52. calibration.offset_x = CAL_PTS(TOP_LEFT).x - int16_t(((CAL_PTS(TOP_LEFT).raw_x + CAL_PTS(BOTTOM_LEFT).raw_x) * calibration.x) >> 17);
  53. calibration.offset_y = CAL_PTS(TOP_LEFT).y - int16_t(((CAL_PTS(TOP_LEFT).raw_y + CAL_PTS(TOP_RIGHT).raw_y) * calibration.y) >> 17);
  54. calibration.orientation = TOUCH_LANDSCAPE;
  55. }
  56. else if (portrait) {
  57. calibration_state = CALIBRATION_SUCCESS;
  58. calibration.x = ((CAL_PTS(TOP_RIGHT).x - CAL_PTS(TOP_LEFT).x) << 17) / (CAL_PTS(BOTTOM_RIGHT).raw_y + CAL_PTS(TOP_RIGHT).raw_y - CAL_PTS(BOTTOM_LEFT).raw_y - CAL_PTS(TOP_LEFT).raw_y);
  59. calibration.y = ((CAL_PTS(BOTTOM_LEFT).y - CAL_PTS(TOP_LEFT).y) << 17) / (CAL_PTS(BOTTOM_RIGHT).raw_x - CAL_PTS(TOP_RIGHT).raw_x + CAL_PTS(BOTTOM_LEFT).raw_x - CAL_PTS(TOP_LEFT).raw_x);
  60. calibration.offset_x = CAL_PTS(TOP_LEFT).x - int16_t(((CAL_PTS(TOP_LEFT).raw_y + CAL_PTS(BOTTOM_LEFT).raw_y) * calibration.x) >> 17);
  61. calibration.offset_y = CAL_PTS(TOP_LEFT).y - int16_t(((CAL_PTS(TOP_LEFT).raw_x + CAL_PTS(TOP_RIGHT).raw_x) * calibration.y) >> 17);
  62. calibration.orientation = TOUCH_PORTRAIT;
  63. }
  64. else {
  65. calibration_state = CALIBRATION_FAIL;
  66. calibration_reset();
  67. if (need_calibration() && failed_count++ < TOUCH_CALIBRATION_MAX_RETRIES) calibration_state = CALIBRATION_TOP_LEFT;
  68. }
  69. #undef CAL_PTS
  70. if (calibration_state == CALIBRATION_SUCCESS) {
  71. SERIAL_ECHOLNPGM("Touch screen calibration completed");
  72. SERIAL_ECHOLNPGM("TOUCH_CALIBRATION_X ", calibration.x);
  73. SERIAL_ECHOLNPGM("TOUCH_CALIBRATION_Y ", calibration.y);
  74. SERIAL_ECHOLNPGM("TOUCH_OFFSET_X ", calibration.offset_x);
  75. SERIAL_ECHOLNPGM("TOUCH_OFFSET_Y ", calibration.offset_y);
  76. SERIAL_ECHO_TERNARY(calibration.orientation == TOUCH_LANDSCAPE, "TOUCH_ORIENTATION ", "TOUCH_LANDSCAPE", "TOUCH_PORTRAIT", "\n");
  77. TERN_(TOUCH_CALIBRATION_AUTO_SAVE, settings.save());
  78. }
  79. }
  80. bool TouchCalibration::handleTouch(uint16_t x, uint16_t y) {
  81. static millis_t next_button_update_ms = 0;
  82. const millis_t now = millis();
  83. if (PENDING(now, next_button_update_ms)) return false;
  84. next_button_update_ms = now + BUTTON_DELAY_MENU;
  85. if (calibration_state < CALIBRATION_SUCCESS) {
  86. calibration_points[calibration_state].raw_x = x;
  87. calibration_points[calibration_state].raw_y = y;
  88. DEBUG_ECHOLNPGM("TouchCalibration - State: ", calibration_state, ", x: ", calibration_points[calibration_state].x, ", raw_x: ", x, ", y: ", calibration_points[calibration_state].y, ", raw_y: ", y);
  89. }
  90. switch (calibration_state) {
  91. case CALIBRATION_TOP_LEFT: calibration_state = CALIBRATION_BOTTOM_LEFT; break;
  92. case CALIBRATION_BOTTOM_LEFT: calibration_state = CALIBRATION_TOP_RIGHT; break;
  93. case CALIBRATION_TOP_RIGHT: calibration_state = CALIBRATION_BOTTOM_RIGHT; break;
  94. case CALIBRATION_BOTTOM_RIGHT: validate_calibration(); break;
  95. default: break;
  96. }
  97. return true;
  98. }
  99. #endif // TOUCH_SCREEN_CALIBRATION