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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "../ultralcd.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::now = 0;
  36. millis_t Touch::time_to_hold;
  37. millis_t Touch::repeat_delay;
  38. millis_t 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. if (now == millis()) return;
  69. now = millis();
  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. return;
  77. }
  78. #endif
  79. #if LCD_TIMEOUT_TO_STATUS
  80. ui.return_to_status_ms = now + LCD_TIMEOUT_TO_STATUS;
  81. #endif
  82. if (touch_time) {
  83. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  84. if (touch_control_type == NONE && ELAPSED(now, touch_time + TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS) && ui.on_status_screen())
  85. ui.goto_screen(touch_screen_calibration);
  86. #endif
  87. return;
  88. }
  89. if (time_to_hold == 0) time_to_hold = now + MINIMUM_HOLD_TIME;
  90. if (PENDING(now, time_to_hold)) return;
  91. if (x != 0 && y != 0) {
  92. if (current_control) {
  93. 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)) {
  94. NOLESS(x, current_control->x);
  95. NOMORE(x, current_control->x + current_control->width);
  96. NOLESS(y, current_control->y);
  97. NOMORE(y, current_control->y + current_control->height);
  98. touch(current_control);
  99. }
  100. else {
  101. current_control = NULL;
  102. }
  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 == NULL)
  114. touch_time = now;
  115. }
  116. x = _x;
  117. y = _y;
  118. }
  119. else {
  120. x = y = 0;
  121. current_control = NULL;
  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. ui.refresh();
  133. if (calibration_state < CALIBRATION_SUCCESS) {
  134. calibration_points[calibration_state].x = int16_t(control->data >> 16);
  135. calibration_points[calibration_state].y = int16_t(control->data & 0xFFFF);
  136. calibration_points[calibration_state].raw_x = x;
  137. calibration_points[calibration_state].raw_y = y;
  138. }
  139. switch (calibration_state) {
  140. case CALIBRATION_POINT_1: calibration_state = CALIBRATION_POINT_2; break;
  141. case CALIBRATION_POINT_2: calibration_state = CALIBRATION_POINT_3; break;
  142. case CALIBRATION_POINT_3: calibration_state = CALIBRATION_POINT_4; break;
  143. case CALIBRATION_POINT_4:
  144. if (validate_precision_x(0, 1) && validate_precision_x(2, 3) && validate_precision_y(0, 2) && validate_precision_y(1, 3)) {
  145. calibration_state = CALIBRATION_SUCCESS;
  146. 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);
  147. 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);
  148. calibration.offset_x = calibration_points[0].x - int16_t(((calibration_points[0].raw_x + calibration_points[1].raw_x) * calibration.x) >> 17);
  149. calibration.offset_y = calibration_points[0].y - int16_t(((calibration_points[0].raw_y + calibration_points[2].raw_y) * calibration.y) >> 17);
  150. calibration.orientation = TOUCH_LANDSCAPE;
  151. }
  152. else if (validate_precision_y(0, 1) && validate_precision_y(2, 3) && validate_precision_x(0, 2) && validate_precision_x(1, 3)) {
  153. calibration_state = CALIBRATION_SUCCESS;
  154. 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);
  155. 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);
  156. calibration.offset_x = calibration_points[0].x - int16_t(((calibration_points[0].raw_y + calibration_points[1].raw_y) * calibration.x) >> 17);
  157. calibration.offset_y = calibration_points[0].y - int16_t(((calibration_points[0].raw_x + calibration_points[2].raw_x) * calibration.y) >> 17);
  158. calibration.orientation = TOUCH_PORTRAIT;
  159. }
  160. else {
  161. calibration_state = CALIBRATION_FAIL;
  162. calibration_reset();
  163. }
  164. if (calibration_state == CALIBRATION_SUCCESS) {
  165. SERIAL_ECHOLN("Touch screen calibration completed");
  166. SERIAL_ECHOLNPAIR("TOUCH_CALIBRATION_X ", calibration.x);
  167. SERIAL_ECHOLNPAIR("TOUCH_CALIBRATION_Y ", calibration.y);
  168. SERIAL_ECHOLNPAIR("TOUCH_OFFSET_X ", calibration.offset_x);
  169. SERIAL_ECHOLNPAIR("TOUCH_OFFSET_Y ", calibration.offset_y);
  170. SERIAL_ECHO("TOUCH_ORIENTATION "); if (calibration.orientation == TOUCH_LANDSCAPE) SERIAL_ECHOLN("TOUCH_LANDSCAPE"); else SERIAL_ECHOLN("TOUCH_PORTRAIT");
  171. }
  172. break;
  173. default: break;
  174. }
  175. break;
  176. #endif // TOUCH_SCREEN_CALIBRATION
  177. case MENU_SCREEN: ui.goto_screen((screenFunc_t)control->data); break;
  178. case BACK: ui.goto_previous_screen(); break;
  179. case CLICK: ui.lcd_clicked = true; break;
  180. #if HAS_RESUME_CONTINUE
  181. case RESUME_CONTINUE: extern bool wait_for_user; wait_for_user = false; break;
  182. #endif
  183. case CANCEL: ui.encoderPosition = 0; ui.selection = false; ui.lcd_clicked = true; break;
  184. case CONFIRM: ui.encoderPosition = 1; ui.selection = true; ui.lcd_clicked = true; break;
  185. case MENU_ITEM: ui.encoderPosition = control->data; ui.refresh(); break;
  186. case PAGE_UP:
  187. encoderTopLine = encoderTopLine > LCD_HEIGHT ? encoderTopLine - LCD_HEIGHT : 0;
  188. ui.encoderPosition = ui.encoderPosition > LCD_HEIGHT ? ui.encoderPosition - LCD_HEIGHT : 0;
  189. ui.refresh();
  190. break;
  191. case PAGE_DOWN:
  192. encoderTopLine = encoderTopLine + 2 * LCD_HEIGHT < screen_items ? encoderTopLine + LCD_HEIGHT : screen_items - LCD_HEIGHT;
  193. ui.encoderPosition = ui.encoderPosition + LCD_HEIGHT < (uint32_t)screen_items ? ui.encoderPosition + LCD_HEIGHT : screen_items;
  194. ui.refresh();
  195. break;
  196. case SLIDER: hold(control); ui.encoderPosition = (x - control->x) * control->data / control->width; break;
  197. case INCREASE: hold(control, repeat_delay - 5); TERN(AUTO_BED_LEVELING_UBL, ui.external_control ? ubl.encoder_diff++ : ui.encoderPosition++, ui.encoderPosition++); break;
  198. case DECREASE: hold(control, repeat_delay - 5); TERN(AUTO_BED_LEVELING_UBL, ui.external_control ? ubl.encoder_diff-- : ui.encoderPosition--, ui.encoderPosition--); break;
  199. case HEATER:
  200. int8_t heater;
  201. heater = control->data;
  202. ui.clear_lcd();
  203. if (heater >= 0) { // HotEnd
  204. #if HOTENDS == 1
  205. 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); });
  206. #else
  207. MenuItemBase::itemIndex = heater;
  208. 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); });
  209. #endif
  210. }
  211. #if HAS_HEATED_BED
  212. else if (heater == H_BED) {
  213. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_BED), &thermalManager.temp_bed.target, 0, BED_MAXTEMP - 10, thermalManager.start_watching_bed);
  214. }
  215. #endif
  216. #if HAS_HEATED_CHAMBER
  217. else if (heater == H_CHAMBER) {
  218. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_CHAMBER), &thermalManager.temp_chamber.target, 0, CHAMBER_MAXTEMP - 10, thermalManager.start_watching_chamber);
  219. }
  220. #endif
  221. break;
  222. case FAN:
  223. ui.clear_lcd();
  224. static uint8_t fan, fan_speed;
  225. fan = 0;
  226. fan_speed = thermalManager.fan_speed[fan];
  227. MenuItem_percent::action((const char *)GET_TEXT_F(MSG_FIRST_FAN_SPEED), &fan_speed, 0, 255, []{ thermalManager.set_fan_speed(fan, fan_speed); });
  228. break;
  229. case FEEDRATE:
  230. ui.clear_lcd();
  231. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_SPEED), &feedrate_percentage, 10, 999);
  232. break;
  233. case FLOWRATE:
  234. ui.clear_lcd();
  235. MenuItemBase::itemIndex = control->data;
  236. #if EXTRUDERS == 1
  237. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_FLOW), &planner.flow_percentage[MenuItemBase::itemIndex], 10, 999, []{ planner.refresh_e_factor(MenuItemBase::itemIndex); });
  238. #else
  239. MenuItem_int3::action((const char *)GET_TEXT_F(MSG_FLOW_N), &planner.flow_percentage[MenuItemBase::itemIndex], 10, 999, []{ planner.refresh_e_factor(MenuItemBase::itemIndex); });
  240. #endif
  241. break;
  242. #if ENABLED(AUTO_BED_LEVELING_UBL)
  243. case UBL: hold(control, UBL_REPEAT_DELAY); ui.encoderPosition += control->data; break;
  244. #endif
  245. case MOVE_AXIS:
  246. ui.goto_screen((screenFunc_t)ui.move_axis_screen);
  247. break;
  248. // TODO: TOUCH could receive data to pass to the callback
  249. case BUTTON: ((screenFunc_t)control->data)(); break;
  250. default: break;
  251. }
  252. }
  253. void Touch::hold(touch_control_t *control, millis_t delay) {
  254. current_control = control;
  255. if (delay) {
  256. repeat_delay = delay > MIN_REPEAT_DELAY ? delay : MIN_REPEAT_DELAY;
  257. time_to_hold = now + repeat_delay;
  258. }
  259. ui.refresh();
  260. }
  261. bool Touch::get_point(int16_t *x, int16_t *y) {
  262. bool is_touched = (calibration.orientation == TOUCH_PORTRAIT ? io.getRawPoint(y, x) : io.getRawPoint(x, y));
  263. if (is_touched && calibration.orientation != TOUCH_ORIENTATION_NONE) {
  264. *x = int16_t((int32_t(*x) * calibration.x) >> 16) + calibration.offset_x;
  265. *y = int16_t((int32_t(*y) * calibration.y) >> 16) + calibration.offset_y;
  266. #if (TFT_ROTATION & TFT_ROTATE_180)
  267. *x = TFT_WIDTH - *x;
  268. *y = TFT_HEIGHT - *y;
  269. #endif
  270. }
  271. return is_touched;
  272. }
  273. Touch touch;
  274. bool MarlinUI::touch_pressed() {
  275. return touch.is_clicked();
  276. }
  277. 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) {
  278. uint16_t width = Images[image].width;
  279. uint16_t height = Images[image].height;
  280. tft.canvas(x, y, width, height);
  281. tft.add_image(0, 0, image, is_enabled ? color_enabled : color_disabled);
  282. if (is_enabled)
  283. touch.add_control(control_type, x, y, width, height, data);
  284. }
  285. #endif // TOUCH_SCREEN