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 13KB

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