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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #pragma once
  20. #include "../../inc/MarlinConfigPre.h"
  21. #if ENABLED(TOUCH_SCREEN)
  22. #include "tft_color.h"
  23. #include "tft_image.h"
  24. #include HAL_PATH(../../HAL, tft/xpt2046.h)
  25. #define TOUCH_DRIVER XPT2046
  26. #ifndef TOUCH_SCREEN_CALIBRATION_PRECISION
  27. #define TOUCH_SCREEN_CALIBRATION_PRECISION 80
  28. #endif
  29. #ifndef TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS
  30. #define TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS 2500
  31. #endif
  32. #define TOUCH_ORIENTATION_NONE 0
  33. #define TOUCH_LANDSCAPE 1
  34. #define TOUCH_PORTRAIT 2
  35. #if !(defined(TOUCH_CALIBRATION_X) || defined(TOUCH_CALIBRATION_Y) || defined(TOUCH_OFFSET_X) || defined(TOUCH_OFFSET_Y) || defined(TOUCH_ORIENTATION))
  36. #if defined(XPT2046_X_CALIBRATION) && defined(XPT2046_Y_CALIBRATION) && defined(XPT2046_X_OFFSET) && defined(XPT2046_Y_OFFSET)
  37. #define TOUCH_CALIBRATION_X XPT2046_X_CALIBRATION
  38. #define TOUCH_CALIBRATION_Y XPT2046_Y_CALIBRATION
  39. #define TOUCH_OFFSET_X XPT2046_X_OFFSET
  40. #define TOUCH_OFFSET_Y XPT2046_Y_OFFSET
  41. #define TOUCH_ORIENTATION TOUCH_LANDSCAPE
  42. #else
  43. #define TOUCH_CALIBRATION_X 0
  44. #define TOUCH_CALIBRATION_Y 0
  45. #define TOUCH_OFFSET_X 0
  46. #define TOUCH_OFFSET_Y 0
  47. #define TOUCH_ORIENTATION TOUCH_ORIENTATION_NONE
  48. #endif
  49. #endif
  50. // Menu Navigation
  51. extern int8_t encoderTopLine, encoderLine, screen_items;
  52. enum TouchControlType : uint16_t {
  53. NONE = 0x0000,
  54. CALIBRATE,
  55. MENU_SCREEN,
  56. MENU_ITEM,
  57. BACK,
  58. PAGE_UP,
  59. PAGE_DOWN,
  60. CLICK,
  61. RESUME_CONTINUE,
  62. SLIDER,
  63. INCREASE,
  64. DECREASE,
  65. CANCEL,
  66. CONFIRM,
  67. HEATER,
  68. FAN,
  69. FEEDRATE,
  70. FLOWRATE,
  71. UBL,
  72. MOVE_AXIS,
  73. BUTTON,
  74. };
  75. typedef void (*screenFunc_t)();
  76. void add_control(uint16_t x, uint16_t y, TouchControlType control_type, int32_t data, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED);
  77. inline void add_control(uint16_t x, uint16_t y, TouchControlType control_type, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED) { add_control(x, y, control_type, 0, image, is_enabled, color_enabled, color_disabled); }
  78. inline void add_control(uint16_t x, uint16_t y, screenFunc_t screen, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED) { add_control(x, y, MENU_SCREEN, (int32_t)screen, image, is_enabled, color_enabled, color_disabled); }
  79. typedef struct __attribute__((__packed__)) {
  80. TouchControlType type;
  81. uint16_t x;
  82. uint16_t y;
  83. uint16_t width;
  84. uint16_t height;
  85. int32_t data;
  86. } touch_control_t;
  87. typedef struct __attribute__((__packed__)) {
  88. int32_t x;
  89. int32_t y;
  90. int16_t offset_x;
  91. int16_t offset_y;
  92. uint8_t orientation;
  93. } touch_calibration_t;
  94. typedef struct __attribute__((__packed__)) {
  95. uint16_t x;
  96. uint16_t y;
  97. int16_t raw_x;
  98. int16_t raw_y;
  99. } touch_calibration_point_t;
  100. enum calibrationState : uint8_t {
  101. CALIBRATION_POINT_1 = 0x00,
  102. CALIBRATION_POINT_2,
  103. CALIBRATION_POINT_3,
  104. CALIBRATION_POINT_4,
  105. CALIBRATION_SUCCESS,
  106. CALIBRATION_FAIL,
  107. CALIBRATION_NONE,
  108. };
  109. #define MAX_CONTROLS 16
  110. #define MINIMUM_HOLD_TIME 15
  111. #define TOUCH_REPEAT_DELAY 75
  112. #define MIN_REPEAT_DELAY 25
  113. #define UBL_REPEAT_DELAY 125
  114. #define FREE_MOVE_RANGE 32
  115. class Touch {
  116. private:
  117. static TOUCH_DRIVER io;
  118. static int16_t x, y;
  119. static bool enabled;
  120. static touch_control_t controls[MAX_CONTROLS];
  121. static touch_control_t *current_control;
  122. static uint16_t controls_count;
  123. static millis_t now;
  124. static millis_t time_to_hold;
  125. static millis_t repeat_delay;
  126. static millis_t touch_time;
  127. static TouchControlType touch_control_type;
  128. static inline bool get_point(int16_t *x, int16_t *y);
  129. static void touch(touch_control_t *control);
  130. static void hold(touch_control_t *control, millis_t delay = 0);
  131. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  132. static calibrationState calibration_state;
  133. static touch_calibration_point_t calibration_points[4];
  134. static bool validate_precision(int32_t a, int32_t b) { return (a > b ? (100 * b) / a : (100 * a) / b) > TOUCH_SCREEN_CALIBRATION_PRECISION; }
  135. static bool validate_precision_x(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_x, calibration_points[b].raw_x); }
  136. static bool validate_precision_y(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_y, calibration_points[b].raw_y); }
  137. #endif // TOUCH_SCREEN_CALIBRATION
  138. public:
  139. static void init();
  140. static void reset() { controls_count = 0; touch_time = -1; current_control = NULL; }
  141. static void clear() { controls_count = 0; }
  142. static void idle();
  143. static bool is_clicked() { return touch_control_type == CLICK; }
  144. static void disable() { enabled = false; }
  145. static void enable() { enabled = true; }
  146. static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, int32_t data = 0);
  147. static touch_calibration_t calibration;
  148. static void calibration_reset() { calibration = {TOUCH_CALIBRATION_X, TOUCH_CALIBRATION_Y, TOUCH_OFFSET_X, TOUCH_OFFSET_Y, TOUCH_ORIENTATION}; }
  149. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  150. static calibrationState calibration_start() { calibration = {0, 0, 0, 0, TOUCH_ORIENTATION_NONE}; return calibration_state = CALIBRATION_POINT_1; }
  151. static void calibration_end() { calibration_state = CALIBRATION_NONE; }
  152. static calibrationState get_calibration_state() { return calibration_state; }
  153. #endif // TOUCH_SCREEN_CALIBRATION
  154. };
  155. extern Touch touch;
  156. #endif // TOUCH_SCREEN