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.

touch_calibration.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "../../inc/MarlinConfig.h"
  20. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  21. #include "touch_calibration.h"
  22. #define TOUCH_CALIBRATION_MAX_RETRIES 5
  23. #define DEBUG_OUT ENABLED(DEBUG_TOUCH_CALIBRATION)
  24. #include "../../core/debug_out.h"
  25. #if ENABLED(TOUCH_CALIBRATION_AUTO_SAVE)
  26. #include "../../module/settings.h"
  27. #endif
  28. TouchCalibration touch_calibration;
  29. touch_calibration_t TouchCalibration::calibration;
  30. calibrationState TouchCalibration::calibration_state = CALIBRATION_NONE;
  31. touch_calibration_point_t TouchCalibration::calibration_points[4];
  32. uint8_t TouchCalibration::failed_count;
  33. void TouchCalibration::validate_calibration() {
  34. #define VALIDATE_PRECISION(XY, A, B) validate_precision_##XY(CALIBRATION_##A, CALIBRATION_##B)
  35. const bool landscape = VALIDATE_PRECISION(x, TOP_LEFT, BOTTOM_LEFT)
  36. && VALIDATE_PRECISION(x, TOP_RIGHT, BOTTOM_RIGHT)
  37. && VALIDATE_PRECISION(y, TOP_LEFT, TOP_RIGHT)
  38. && VALIDATE_PRECISION(y, BOTTOM_LEFT, BOTTOM_RIGHT);
  39. const bool portrait = VALIDATE_PRECISION(y, TOP_LEFT, BOTTOM_LEFT)
  40. && VALIDATE_PRECISION(y, TOP_RIGHT, BOTTOM_RIGHT)
  41. && VALIDATE_PRECISION(x, TOP_LEFT, TOP_RIGHT)
  42. && VALIDATE_PRECISION(x, BOTTOM_LEFT, BOTTOM_RIGHT);
  43. #undef VALIDATE_PRECISION
  44. #define CAL_PTS(N) calibration_points[CALIBRATION_##N]
  45. if (landscape) {
  46. calibration_state = CALIBRATION_SUCCESS;
  47. 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);
  48. 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);
  49. 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);
  50. 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);
  51. calibration.orientation = TOUCH_LANDSCAPE;
  52. }
  53. else if (portrait) {
  54. calibration_state = CALIBRATION_SUCCESS;
  55. 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);
  56. 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);
  57. 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);
  58. 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);
  59. calibration.orientation = TOUCH_PORTRAIT;
  60. }
  61. else {
  62. calibration_state = CALIBRATION_FAIL;
  63. calibration_reset();
  64. if (need_calibration() && failed_count++ < TOUCH_CALIBRATION_MAX_RETRIES) calibration_state = CALIBRATION_TOP_LEFT;
  65. }
  66. #undef CAL_PTS
  67. if (calibration_state == CALIBRATION_SUCCESS) {
  68. SERIAL_ECHOLNPGM("Touch screen calibration completed");
  69. SERIAL_ECHOLNPAIR("TOUCH_CALIBRATION_X ", calibration.x);
  70. SERIAL_ECHOLNPAIR("TOUCH_CALIBRATION_Y ", calibration.y);
  71. SERIAL_ECHOLNPAIR("TOUCH_OFFSET_X ", calibration.offset_x);
  72. SERIAL_ECHOLNPAIR("TOUCH_OFFSET_Y ", calibration.offset_y);
  73. SERIAL_ECHO_TERNARY(calibration.orientation == TOUCH_LANDSCAPE, "TOUCH_ORIENTATION ", "TOUCH_LANDSCAPE", "TOUCH_PORTRAIT", "\n");
  74. TERN_(TOUCH_CALIBRATION_AUTO_SAVE, settings.save());
  75. }
  76. }
  77. bool TouchCalibration::handleTouch(uint16_t x, uint16_t y) {
  78. static millis_t next_button_update_ms = 0;
  79. const millis_t now = millis();
  80. if (PENDING(now, next_button_update_ms)) return false;
  81. next_button_update_ms = now + BUTTON_DELAY_MENU;
  82. if (calibration_state < CALIBRATION_SUCCESS) {
  83. calibration_points[calibration_state].raw_x = x;
  84. calibration_points[calibration_state].raw_y = y;
  85. DEBUG_ECHOLNPAIR("TouchCalibration - State: ", calibration_state, ", x: ", calibration_points[calibration_state].x, ", raw_x: ", x, ", y: ", calibration_points[calibration_state].y, ", raw_y: ", y);
  86. }
  87. switch (calibration_state) {
  88. case CALIBRATION_TOP_LEFT: calibration_state = CALIBRATION_BOTTOM_LEFT; break;
  89. case CALIBRATION_BOTTOM_LEFT: calibration_state = CALIBRATION_TOP_RIGHT; break;
  90. case CALIBRATION_TOP_RIGHT: calibration_state = CALIBRATION_BOTTOM_RIGHT; break;
  91. case CALIBRATION_BOTTOM_RIGHT: validate_calibration(); break;
  92. default: break;
  93. }
  94. return true;
  95. }
  96. #endif // TOUCH_SCREEN_CALIBRATION