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.

ui_common.cpp 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/MarlinConfigPre.h"
  23. #if HAS_GRAPHICAL_TFT
  24. #include "ui_common.h"
  25. #include "../lcdprint.h"
  26. #include "../../libs/numtostr.h"
  27. #include "../menu/menu.h"
  28. void menu_pause_option();
  29. static xy_uint_t cursor;
  30. #if ENABLED(TOUCH_SCREEN)
  31. bool draw_menu_navigation = false;
  32. #endif
  33. #if HAS_TOUCH_SLEEP
  34. bool lcd_sleep_task() {
  35. static bool sleepCleared;
  36. if (touch.isSleeping()) {
  37. tft.queue.reset();
  38. if (!sleepCleared) {
  39. sleepCleared = true;
  40. ui.clear_lcd();
  41. tft.queue.async();
  42. }
  43. touch.idle();
  44. return true;
  45. }
  46. else
  47. sleepCleared = false;
  48. return false;
  49. }
  50. #endif
  51. void menu_line(const uint8_t row, uint16_t color) {
  52. cursor.set(0, row);
  53. tft.canvas(0, TFT_TOP_LINE_Y + cursor.y * MENU_LINE_HEIGHT, TFT_WIDTH, MENU_ITEM_HEIGHT);
  54. tft.set_background(color);
  55. }
  56. void menu_item(const uint8_t row, bool sel ) {
  57. #if ENABLED(TOUCH_SCREEN)
  58. if (row == 0) {
  59. touch.clear();
  60. draw_menu_navigation = TERN(ADVANCED_PAUSE_FEATURE, ui.currentScreen != menu_pause_option, true);
  61. }
  62. #endif
  63. menu_line(row, sel ? COLOR_SELECTION_BG : COLOR_BACKGROUND);
  64. #if ENABLED(TOUCH_SCREEN)
  65. const TouchControlType tct = TERN(SINGLE_TOUCH_NAVIGATION, true, sel) ? MENU_CLICK : MENU_ITEM;
  66. touch.add_control(tct, 0, TFT_TOP_LINE_Y + row * MENU_LINE_HEIGHT, TFT_WIDTH, MENU_ITEM_HEIGHT, encoderTopLine + row);
  67. #endif
  68. }
  69. //
  70. // lcdprint.h functions
  71. //
  72. #define TFT_COL_WIDTH ((TFT_WIDTH) / (LCD_WIDTH))
  73. void lcd_gotopixel(const uint16_t x, const uint16_t y) {
  74. if (x >= TFT_WIDTH) return;
  75. cursor.set(x / (TFT_COL_WIDTH), y / MENU_LINE_HEIGHT);
  76. tft.canvas(x, TFT_TOP_LINE_Y + y, (TFT_WIDTH) - x, MENU_ITEM_HEIGHT);
  77. tft.set_background(COLOR_BACKGROUND);
  78. }
  79. void lcd_moveto(const lcd_uint_t col, const lcd_uint_t row) {
  80. lcd_gotopixel(int(col) * (TFT_COL_WIDTH), int(row) * MENU_LINE_HEIGHT);
  81. }
  82. int lcd_put_wchar_max(wchar_t c, pixel_len_t max_length) {
  83. if (max_length < 1) return 0;
  84. tft_string.set();
  85. tft_string.add(c);
  86. tft.add_text(MENU_TEXT_X_OFFSET, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  87. lcd_gotopixel((cursor.x + 1) * (TFT_COL_WIDTH) + tft_string.width(), cursor.y * MENU_LINE_HEIGHT);
  88. return tft_string.width();
  89. }
  90. int lcd_put_u8str_max_P(PGM_P utf8_pstr, pixel_len_t max_length) {
  91. if (max_length < 1) return 0;
  92. tft_string.set(utf8_pstr);
  93. tft_string.trim();
  94. tft_string.truncate(max_length);
  95. tft.add_text(MENU_TEXT_X_OFFSET, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  96. lcd_gotopixel((cursor.x + 1) * (TFT_COL_WIDTH) + tft_string.width(), cursor.y * MENU_LINE_HEIGHT);
  97. return tft_string.width();
  98. }
  99. int lcd_put_u8str_max(const char * utf8_str, pixel_len_t max_length) {
  100. return lcd_put_u8str_max_P(utf8_str, max_length);
  101. }
  102. void lcd_put_int(const int i) {
  103. // 3 digits max for this one...
  104. const char* str = i16tostr3left(int16_t(i));
  105. lcd_put_u8str_max(str, 3);
  106. }
  107. //
  108. // Menu Item methods
  109. //
  110. // Draw a generic menu item with pre_char (if selected) and post_char
  111. void MenuItemBase::_draw(const bool sel, const uint8_t row, PGM_P const pstr, const char pre_char, const char post_char) {
  112. menu_item(row, sel);
  113. uint8_t *string = (uint8_t *)pstr;
  114. MarlinImage image = noImage;
  115. switch (*string) {
  116. case 0x01: image = imgRefresh; break; // LCD_STR_REFRESH
  117. case 0x02: image = imgDirectory; break; // LCD_STR_FOLDER
  118. }
  119. uint8_t offset = MENU_TEXT_X_OFFSET;
  120. if (image != noImage) {
  121. string++;
  122. offset = MENU_ITEM_ICON_SPACE;
  123. tft.add_image(MENU_ITEM_ICON_X, MENU_ITEM_ICON_Y, image, COLOR_MENU_TEXT, sel ? COLOR_SELECTION_BG : COLOR_BACKGROUND);
  124. }
  125. tft_string.set(string, itemIndex, itemString);
  126. tft.add_text(offset, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  127. }
  128. // Draw a menu item with a (potentially) editable value
  129. void MenuEditItemBase::draw(const bool sel, const uint8_t row, PGM_P const pstr, const char * const data, const bool pgm) {
  130. menu_item(row, sel);
  131. tft_string.set(pstr, itemIndex, itemString);
  132. tft.add_text(MENU_TEXT_X_OFFSET, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  133. if (data) {
  134. tft_string.set(data);
  135. tft.add_text(TFT_WIDTH - MENU_TEXT_X_OFFSET - tft_string.width(), MENU_TEXT_Y_OFFSET, COLOR_MENU_VALUE, tft_string);
  136. }
  137. }
  138. // Draw a static item with no left-right margin required. Centered by default.
  139. void MenuItem_static::draw(const uint8_t row, PGM_P const pstr, const uint8_t style/*=SS_DEFAULT*/, const char * const vstr/*=nullptr*/) {
  140. menu_item(row);
  141. tft_string.set(pstr, itemIndex, itemString);
  142. if (vstr)
  143. tft_string.add(vstr);
  144. tft.add_text(tft_string.center(TFT_WIDTH), MENU_TEXT_Y_OFFSET, COLOR_YELLOW, tft_string);
  145. }
  146. #if ENABLED(SDSUPPORT)
  147. void MenuItem_sdbase::draw(const bool sel, const uint8_t row, PGM_P const, CardReader &theCard, const bool isDir) {
  148. menu_item(row, sel);
  149. if (isDir)
  150. tft.add_image(MENU_ITEM_ICON_X, MENU_ITEM_ICON_Y, imgDirectory, COLOR_MENU_TEXT, sel ? COLOR_SELECTION_BG : COLOR_BACKGROUND);
  151. tft.add_text(MENU_ITEM_ICON_SPACE, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, theCard.longest_filename());
  152. }
  153. #endif
  154. //
  155. // MarlinUI methods
  156. //
  157. bool MarlinUI::detected() { return true; }
  158. void MarlinUI::init_lcd() {
  159. tft.init();
  160. tft.set_font(MENU_FONT_NAME);
  161. #ifdef SYMBOLS_FONT_NAME
  162. tft.add_glyphs(SYMBOLS_FONT_NAME);
  163. #endif
  164. TERN_(TOUCH_SCREEN, touch.init());
  165. clear_lcd();
  166. }
  167. void MarlinUI::clear_lcd() {
  168. #if ENABLED(TOUCH_SCREEN)
  169. touch.reset();
  170. draw_menu_navigation = false;
  171. #endif
  172. tft.queue.reset();
  173. tft.fill(0, 0, TFT_WIDTH, TFT_HEIGHT, COLOR_BACKGROUND);
  174. cursor.set(0, 0);
  175. }
  176. #if HAS_LCD_BRIGHTNESS
  177. void MarlinUI::_set_brightness() {
  178. #if PIN_EXISTS(TFT_BACKLIGHT)
  179. if (PWM_PIN(TFT_BACKLIGHT_PIN))
  180. analogWrite(pin_t(TFT_BACKLIGHT_PIN), brightness);
  181. #endif
  182. }
  183. #endif
  184. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  185. void MarlinUI::touch_calibration_screen() {
  186. uint16_t x, y;
  187. calibrationState calibration_stage = touch_calibration.get_calibration_state();
  188. if (calibration_stage == CALIBRATION_NONE) {
  189. defer_status_screen(true);
  190. clear_lcd();
  191. calibration_stage = touch_calibration.calibration_start();
  192. }
  193. else {
  194. x = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].x;
  195. y = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].y;
  196. tft.canvas(x - 15, y - 15, 31, 31);
  197. tft.set_background(COLOR_BACKGROUND);
  198. }
  199. touch.clear();
  200. if (calibration_stage < CALIBRATION_SUCCESS) {
  201. switch (calibration_stage) {
  202. case CALIBRATION_TOP_LEFT: tft_string.set(GET_TEXT(MSG_TOP_LEFT)); break;
  203. case CALIBRATION_BOTTOM_LEFT: tft_string.set(GET_TEXT(MSG_BOTTOM_LEFT)); break;
  204. case CALIBRATION_TOP_RIGHT: tft_string.set(GET_TEXT(MSG_TOP_RIGHT)); break;
  205. case CALIBRATION_BOTTOM_RIGHT: tft_string.set(GET_TEXT(MSG_BOTTOM_RIGHT)); break;
  206. default: break;
  207. }
  208. x = touch_calibration.calibration_points[calibration_stage].x;
  209. y = touch_calibration.calibration_points[calibration_stage].y;
  210. tft.canvas(x - 15, y - 15, 31, 31);
  211. tft.set_background(COLOR_BACKGROUND);
  212. tft.add_bar(0, 15, 31, 1, COLOR_TOUCH_CALIBRATION);
  213. tft.add_bar(15, 0, 1, 31, COLOR_TOUCH_CALIBRATION);
  214. touch.add_control(CALIBRATE, 0, 0, TFT_WIDTH, TFT_HEIGHT, uint32_t(x) << 16 | uint32_t(y));
  215. }
  216. else {
  217. tft_string.set(calibration_stage == CALIBRATION_SUCCESS ? GET_TEXT(MSG_CALIBRATION_COMPLETED) : GET_TEXT(MSG_CALIBRATION_FAILED));
  218. defer_status_screen(false);
  219. touch_calibration.calibration_end();
  220. touch.add_control(BACK, 0, 0, TFT_WIDTH, TFT_HEIGHT);
  221. }
  222. tft.canvas(0, (TFT_HEIGHT - tft_string.font_height()) >> 1, TFT_WIDTH, tft_string.font_height());
  223. tft.set_background(COLOR_BACKGROUND);
  224. tft.add_text(tft_string.center(TFT_WIDTH), 0, COLOR_MENU_TEXT, tft_string);
  225. }
  226. #endif // TOUCH_SCREEN_CALIBRATION
  227. #endif // HAS_GRAPHICAL_TFT