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.h 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #pragma once
  23. #include "../../inc/MarlinConfigPre.h"
  24. #include "tft_io.h"
  25. #ifndef TOUCH_SCREEN_CALIBRATION_PRECISION
  26. #define TOUCH_SCREEN_CALIBRATION_PRECISION 80
  27. #endif
  28. #ifndef TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS
  29. #define TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS 2500
  30. #endif
  31. typedef struct __attribute__((__packed__)) {
  32. int32_t x, y;
  33. int16_t offset_x, offset_y;
  34. uint8_t orientation;
  35. } touch_calibration_t;
  36. typedef struct __attribute__((__packed__)) {
  37. uint16_t x, y;
  38. int16_t raw_x, raw_y;
  39. } touch_calibration_point_t;
  40. enum calibrationState : uint8_t {
  41. CALIBRATION_TOP_LEFT = 0x00,
  42. CALIBRATION_BOTTOM_LEFT,
  43. CALIBRATION_TOP_RIGHT,
  44. CALIBRATION_BOTTOM_RIGHT,
  45. CALIBRATION_SUCCESS,
  46. CALIBRATION_FAIL,
  47. CALIBRATION_NONE,
  48. };
  49. class TouchCalibration {
  50. public:
  51. static calibrationState calibration_state;
  52. static touch_calibration_point_t calibration_points[4];
  53. static bool validate_precision(int32_t a, int32_t b) { return (a > b ? (100 * b) / a : (100 * a) / b) > TOUCH_SCREEN_CALIBRATION_PRECISION; }
  54. static bool validate_precision_x(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_x, calibration_points[b].raw_x); }
  55. static bool validate_precision_y(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_y, calibration_points[b].raw_y); }
  56. static void validate_calibration();
  57. static touch_calibration_t calibration;
  58. static uint8_t failed_count;
  59. static void calibration_reset() { calibration = { TOUCH_CALIBRATION_X, TOUCH_CALIBRATION_Y, TOUCH_OFFSET_X, TOUCH_OFFSET_Y, TOUCH_ORIENTATION }; }
  60. static bool need_calibration() { return !calibration.offset_x && !calibration.offset_y && !calibration.x && !calibration.y; }
  61. static calibrationState calibration_start() {
  62. calibration = { 0, 0, 0, 0, TOUCH_ORIENTATION_NONE };
  63. calibration_state = CALIBRATION_TOP_LEFT;
  64. calibration_points[CALIBRATION_TOP_LEFT].x = 30;
  65. calibration_points[CALIBRATION_TOP_LEFT].y = 30;
  66. calibration_points[CALIBRATION_BOTTOM_LEFT].x = 30;
  67. calibration_points[CALIBRATION_BOTTOM_LEFT].y = TFT_HEIGHT - 31;
  68. calibration_points[CALIBRATION_TOP_RIGHT].x = TFT_WIDTH - 31;
  69. calibration_points[CALIBRATION_TOP_RIGHT].y = 30;
  70. calibration_points[CALIBRATION_BOTTOM_RIGHT].x = TFT_WIDTH - 31;
  71. calibration_points[CALIBRATION_BOTTOM_RIGHT].y = TFT_HEIGHT - 31;
  72. failed_count = 0;
  73. return calibration_state;
  74. }
  75. static void calibration_end() { calibration_state = CALIBRATION_NONE; }
  76. static calibrationState get_calibration_state() { return calibration_state; }
  77. static bool calibration_loaded() {
  78. if (need_calibration()) calibration_reset();
  79. return !need_calibration();
  80. }
  81. static bool handleTouch(uint16_t x, uint16_t y);
  82. };
  83. extern TouchCalibration touch_calibration;