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.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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)
  24. #include "touch.h"
  25. #include "../marlinui.h" // for ui methods
  26. #include "../menu/menu_item.h" // for touch_screen_calibration
  27. #include "../../module/temperature.h"
  28. #include "../../module/planner.h"
  29. #if ENABLED(AUTO_BED_LEVELING_UBL)
  30. #include "../../feature/bedlevel/bedlevel.h"
  31. #endif
  32. #include "tft.h"
  33. bool Touch::enabled = true;
  34. int16_t Touch::x, Touch::y;
  35. touch_control_t Touch::controls[];
  36. touch_control_t *Touch::current_control;
  37. uint16_t Touch::controls_count;
  38. millis_t Touch::last_touch_ms = 0,
  39. Touch::time_to_hold,
  40. Touch::repeat_delay,
  41. Touch::touch_time;
  42. TouchControlType Touch::touch_control_type = NONE;
  43. #if HAS_RESUME_CONTINUE
  44. extern bool wait_for_user;
  45. #endif
  46. void Touch::init() {
  47. TERN_(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration_reset());
  48. reset();
  49. io.Init();
  50. enable();
  51. }
  52. void Touch::add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data) {
  53. if (controls_count == MAX_CONTROLS) return;
  54. controls[controls_count].type = type;
  55. controls[controls_count].x = x;
  56. controls[controls_count].y = y;
  57. controls[controls_count].width = width;
  58. controls[controls_count].height = height;
  59. controls[controls_count].data = data;
  60. controls_count++;
  61. }
  62. void Touch::idle() {
  63. uint16_t i;
  64. int16_t _x, _y;
  65. if (!enabled) return;
  66. // Return if Touch::idle is called within the same millisecond
  67. const millis_t now = millis();
  68. if (last_touch_ms == now) return;
  69. last_touch_ms = now;
  70. if (get_point(&_x, &_y)) {
  71. #if HAS_RESUME_CONTINUE
  72. // UI is waiting for a click anywhere?
  73. if (wait_for_user) {
  74. touch_control_type = CLICK;
  75. ui.lcd_clicked = true;
  76. if (ui.external_control) wait_for_user = false;
  77. return;
  78. }
  79. #endif
  80. #if LCD_TIMEOUT_TO_STATUS
  81. ui.return_to_status_ms = last_touch_ms + LCD_TIMEOUT_TO_STATUS;
  82. #endif
  83. if (touch_time) {
  84. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  85. if (touch_control_type == NONE && ELAPSED(last_touch_ms, touch_time + TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS) && ui.on_status_screen())
  86. ui.goto_screen(touch_screen_calibration);
  87. #endif
  88. return;
  89. }
  90. if (time_to_hold == 0) time_to_hold = last_touch_ms + MINIMUM_HOLD_TIME;
  91. if (PENDING(last_touch_ms, time_to_hold)) return;
  92. if (x != 0 && y != 0) {
  93. if (current_control) {
  94. if (WITHIN(x, current_control->x - FREE_MOVE_RANGE, current_control->x + current_control->width + FREE_MOVE_RANGE) && WITHIN(y, current_control->y - FREE_MOVE_RANGE, current_control->y + current_control->height + FREE_MOVE_RANGE)) {
  95. NOLESS(x, current_control->x);
  96. NOMORE(x, current_control->x + current_control->width);
  97. NOLESS(y, current_control->y);
  98. NOMORE(y, current_control->y + current_control->height);
  99. touch(current_control);
  100. }
  101. else
  102. current_control = nullptr;
  103. }
  104. else {
  105. for (i = 0; i < controls_count; i++) {
  106. if ((WITHIN(x, controls[i].x, controls[i].x + controls[i].width) && WITHIN(y, controls[i].y, controls[i].y + controls[i].height)) || (TERN(TOUCH_SCREEN_CALIBRATION, controls[i].type == CALIBRATE, false))) {
  107. touch_control_type = controls[i].type;
  108. touch(&controls[i]);
  109. break;
  110. }
  111. }
  112. }
  113. if (!current_control)
  114. touch_time = last_touch_ms;
  115. }
  116. x = _x;
  117. y = _y;
  118. }
  119. else {
  120. x = y = 0;
  121. current_control = nullptr;
  122. touch_time = 0;
  123. touch_control_type = NONE;
  124. time_to_hold = 0;
  125. repeat_delay = TOUCH_REPEAT_DELAY;
  126. }
  127. }
  128. void Touch::touch(touch_control_t *control) {
  129. switch (control->type) {
  130. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  131. case CALIBRATE:
  132. if (touch_calibration.handleTouch(x, y)) ui.refresh();
  133. break;
  134. #endif // TOUCH_SCREEN_CALIBRATION
  135. case MENU_SCREEN: ui.goto_screen((screenFunc_t)control->data); break;
  136. case BACK: ui.goto_previous_screen(); break;
  137. case MENU_CLICK:
  138. TERN_(SINGLE_TOUCH_NAVIGATION, ui.encoderPosition = control->data);
  139. ui.lcd_clicked = true;
  140. break;
  141. case CLICK: ui.lcd_clicked = true; break;
  142. #if HAS_RESUME_CONTINUE
  143. case RESUME_CONTINUE: extern bool wait_for_user; wait_for_user = false; break;
  144. #endif
  145. case CANCEL: ui.encoderPosition = 0; ui.selection = false; ui.lcd_clicked = true; break;
  146. case CONFIRM: ui.encoderPosition = 1; ui.selection = true; ui.lcd_clicked = true; break;
  147. case MENU_ITEM: ui.encoderPosition = control->data; ui.refresh(); break;
  148. case PAGE_UP:
  149. encoderTopLine = encoderTopLine > LCD_HEIGHT ? encoderTopLine - LCD_HEIGHT : 0;
  150. ui.encoderPosition = ui.encoderPosition > LCD_HEIGHT ? ui.encoderPosition - LCD_HEIGHT : 0;
  151. ui.refresh();
  152. break;
  153. case PAGE_DOWN:
  154. encoderTopLine = encoderTopLine + 2 * LCD_HEIGHT < screen_items ? encoderTopLine + LCD_HEIGHT : screen_items - LCD_HEIGHT;
  155. ui.encoderPosition = ui.encoderPosition + LCD_HEIGHT < (uint32_t)screen_items ? ui.encoderPosition + LCD_HEIGHT : screen_items;
  156. ui.refresh();
  157. break;
  158. case SLIDER: hold(control); ui.encoderPosition = (x - control->x) * control->data / control->width; break;
  159. case INCREASE: hold(control, repeat_delay - 5); TERN(AUTO_BED_LEVELING_UBL, ui.external_control ? ubl.encoder_diff++ : ui.encoderPosition++, ui.encoderPosition++); break;
  160. case DECREASE: hold(control, repeat_delay - 5); TERN(AUTO_BED_LEVELING_UBL, ui.external_control ? ubl.encoder_diff-- : ui.encoderPosition--, ui.encoderPosition--); break;
  161. case HEATER:
  162. int8_t heater;
  163. heater = control->data;
  164. ui.clear_lcd();
  165. #if HAS_HOTEND
  166. if (heater >= 0) { // HotEnd
  167. #if HOTENDS == 1
  168. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_NOZZLE), &thermalManager.temp_hotend[0].target, 0, thermalManager.hotend_max_target(0), []{ thermalManager.start_watching_hotend(0); });
  169. #else
  170. MenuItemBase::itemIndex = heater;
  171. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_NOZZLE_N), &thermalManager.temp_hotend[heater].target, 0, thermalManager.hotend_max_target(heater), []{ thermalManager.start_watching_hotend(MenuItemBase::itemIndex); });
  172. #endif
  173. }
  174. #endif
  175. #if HAS_HEATED_BED
  176. else if (heater == H_BED) {
  177. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_BED), &thermalManager.temp_bed.target, 0, BED_MAX_TARGET, thermalManager.start_watching_bed);
  178. }
  179. #endif
  180. #if HAS_HEATED_CHAMBER
  181. else if (heater == H_CHAMBER) {
  182. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_CHAMBER), &thermalManager.temp_chamber.target, 0, CHAMBER_MAX_TARGET, thermalManager.start_watching_chamber);
  183. }
  184. #endif
  185. #if HAS_COOLER
  186. else if (heater == H_COOLER) {
  187. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_COOLER), &thermalManager.temp_cooler.target, 0, COOLER_MAX_TARGET, thermalManager.start_watching_cooler);
  188. }
  189. #endif
  190. break;
  191. case FAN:
  192. ui.clear_lcd();
  193. static uint8_t fan, fan_speed;
  194. fan = 0;
  195. fan_speed = thermalManager.fan_speed[fan];
  196. MenuItem_percent::action((const char *)GET_TEXT_F(MSG_FIRST_FAN_SPEED), &fan_speed, 0, 255, []{ thermalManager.set_fan_speed(fan, fan_speed); });
  197. break;
  198. case FEEDRATE:
  199. ui.clear_lcd();
  200. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_SPEED), &feedrate_percentage, 10, 999);
  201. break;
  202. case FLOWRATE:
  203. ui.clear_lcd();
  204. MenuItemBase::itemIndex = control->data;
  205. #if EXTRUDERS == 1
  206. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_FLOW), &planner.flow_percentage[MenuItemBase::itemIndex], 10, 999, []{ planner.refresh_e_factor(MenuItemBase::itemIndex); });
  207. #else
  208. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_FLOW_N), &planner.flow_percentage[MenuItemBase::itemIndex], 10, 999, []{ planner.refresh_e_factor(MenuItemBase::itemIndex); });
  209. #endif
  210. break;
  211. #if ENABLED(AUTO_BED_LEVELING_UBL)
  212. case UBL: hold(control, UBL_REPEAT_DELAY); ui.encoderPosition += control->data; break;
  213. #endif
  214. case MOVE_AXIS:
  215. ui.goto_screen((screenFunc_t)ui.move_axis_screen);
  216. break;
  217. // TODO: TOUCH could receive data to pass to the callback
  218. case BUTTON: ((screenFunc_t)control->data)(); break;
  219. default: break;
  220. }
  221. }
  222. void Touch::hold(touch_control_t *control, millis_t delay) {
  223. current_control = control;
  224. if (delay) {
  225. repeat_delay = delay > MIN_REPEAT_DELAY ? delay : MIN_REPEAT_DELAY;
  226. time_to_hold = last_touch_ms + repeat_delay;
  227. }
  228. ui.refresh();
  229. }
  230. bool Touch::get_point(int16_t *x, int16_t *y) {
  231. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  232. bool is_touched = (touch_calibration.calibration.orientation == TOUCH_PORTRAIT ? io.getRawPoint(y, x) : io.getRawPoint(x, y));
  233. if (is_touched && touch_calibration.calibration.orientation != TOUCH_ORIENTATION_NONE) {
  234. *x = int16_t((int32_t(*x) * touch_calibration.calibration.x) >> 16) + touch_calibration.calibration.offset_x;
  235. *y = int16_t((int32_t(*y) * touch_calibration.calibration.y) >> 16) + touch_calibration.calibration.offset_y;
  236. }
  237. #else
  238. bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? io.getRawPoint(y, x) : io.getRawPoint(x, y));
  239. *x = uint16_t((uint32_t(*x) * TOUCH_CALIBRATION_X) >> 16) + TOUCH_OFFSET_X;
  240. *y = uint16_t((uint32_t(*y) * TOUCH_CALIBRATION_Y) >> 16) + TOUCH_OFFSET_Y;
  241. #endif
  242. return is_touched;
  243. }
  244. Touch touch;
  245. bool MarlinUI::touch_pressed() {
  246. return touch.is_clicked();
  247. }
  248. void add_control(uint16_t x, uint16_t y, TouchControlType control_type, intptr_t data, MarlinImage image, bool is_enabled, uint16_t color_enabled, uint16_t color_disabled) {
  249. uint16_t width = Images[image].width;
  250. uint16_t height = Images[image].height;
  251. tft.canvas(x, y, width, height);
  252. tft.add_image(0, 0, image, is_enabled ? color_enabled : color_disabled);
  253. if (is_enabled)
  254. touch.add_control(control_type, x, y, width, height, data);
  255. }
  256. #endif // TOUCH_SCREEN