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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. };
  73. typedef void (*screenFunc_t)();
  74. 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);
  75. 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); }
  76. 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); }
  77. typedef struct __attribute__((__packed__)) {
  78. TouchControlType type;
  79. uint16_t x;
  80. uint16_t y;
  81. uint16_t width;
  82. uint16_t height;
  83. int32_t data;
  84. } touch_control_t;
  85. typedef struct __attribute__((__packed__)) {
  86. int32_t x;
  87. int32_t y;
  88. int16_t offset_x;
  89. int16_t offset_y;
  90. uint8_t orientation;
  91. } touch_calibration_t;
  92. typedef struct __attribute__((__packed__)) {
  93. uint16_t x;
  94. uint16_t y;
  95. int16_t raw_x;
  96. int16_t raw_y;
  97. } touch_calibration_point_t;
  98. enum calibrationState : uint8_t {
  99. CALIBRATION_POINT_1 = 0x00,
  100. CALIBRATION_POINT_2,
  101. CALIBRATION_POINT_3,
  102. CALIBRATION_POINT_4,
  103. CALIBRATION_SUCCESS,
  104. CALIBRATION_FAIL,
  105. CALIBRATION_NONE,
  106. };
  107. #define MAX_CONTROLS 16
  108. #define MINIMUM_HOLD_TIME 15
  109. #define TOUCH_REPEAT_DELAY 75
  110. #define MIN_REPEAT_DELAY 25
  111. #define UBL_REPEAT_DELAY 125
  112. #define FREE_MOVE_RANGE 32
  113. class Touch {
  114. private:
  115. static TOUCH_DRIVER io;
  116. static int16_t x, y;
  117. static touch_control_t controls[MAX_CONTROLS];
  118. static touch_control_t *current_control;
  119. static uint16_t controls_count;
  120. static millis_t now;
  121. static millis_t time_to_hold;
  122. static millis_t repeat_delay;
  123. static millis_t touch_time;
  124. static TouchControlType touch_control_type;
  125. static inline bool get_point(int16_t *x, int16_t *y);
  126. static void touch(touch_control_t *control);
  127. static void hold(touch_control_t *control, millis_t delay = 0);
  128. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  129. static calibrationState calibration_state;
  130. static touch_calibration_point_t calibration_points[4];
  131. static bool validate_precision(int32_t a, int32_t b) { return (a > b ? (100 * b) / a : (100 * a) / b) > TOUCH_SCREEN_CALIBRATION_PRECISION; }
  132. static bool validate_precision_x(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_x, calibration_points[b].raw_x); }
  133. static bool validate_precision_y(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_y, calibration_points[b].raw_y); }
  134. #endif // TOUCH_SCREEN_CALIBRATION
  135. public:
  136. static void init();
  137. static void reset() { controls_count = 0; touch_time = -1; current_control = NULL; }
  138. static void clear() { controls_count = 0; }
  139. static void idle();
  140. static bool is_clicked() { return touch_control_type == CLICK; }
  141. static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, int32_t data = 0);
  142. static touch_calibration_t calibration;
  143. static void calibration_reset() { calibration = {TOUCH_CALIBRATION_X, TOUCH_CALIBRATION_Y, TOUCH_OFFSET_X, TOUCH_OFFSET_Y, TOUCH_ORIENTATION}; }
  144. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  145. static calibrationState calibration_start() { calibration = {0, 0, 0, 0, TOUCH_ORIENTATION_NONE}; return calibration_state = CALIBRATION_POINT_1; }
  146. static void calibration_end() { calibration_state = CALIBRATION_NONE; }
  147. static calibrationState get_calibration_state() { return calibration_state; }
  148. #endif // TOUCH_SCREEN_CALIBRATION
  149. };
  150. extern Touch touch;
  151. #endif // TOUCH_SCREEN