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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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(const wchar_t c, const pixel_len_t max_length) {
  83. if (max_length < 1) return 0;
  84. tft_string.set(c);
  85. tft.add_text(MENU_TEXT_X_OFFSET, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  86. lcd_gotopixel((cursor.x + 1) * (TFT_COL_WIDTH) + tft_string.width(), cursor.y * MENU_LINE_HEIGHT);
  87. return tft_string.width();
  88. }
  89. int lcd_put_u8str_max_P(PGM_P utf8_pstr, const pixel_len_t max_length) {
  90. if (max_length < 1) return 0;
  91. tft_string.set(utf8_pstr);
  92. tft_string.trim();
  93. tft_string.truncate(max_length);
  94. tft.add_text(MENU_TEXT_X_OFFSET, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  95. lcd_gotopixel((cursor.x + 1) * (TFT_COL_WIDTH) + tft_string.width(), cursor.y * MENU_LINE_HEIGHT);
  96. return tft_string.width();
  97. }
  98. int lcd_put_u8str_max(const char * utf8_str, const pixel_len_t max_length) {
  99. return lcd_put_u8str_max_P(utf8_str, max_length);
  100. }
  101. void lcd_put_int(const int i) {
  102. // 3 digits max for this one...
  103. const char* str = i16tostr3left(int16_t(i));
  104. lcd_put_u8str_max(str, 3);
  105. }
  106. //
  107. // Menu Item methods
  108. //
  109. // Draw a generic menu item with pre_char (if selected) and post_char
  110. void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const fstr, const char pre_char, const char post_char) {
  111. menu_item(row, sel);
  112. const char *string = FTOP(fstr);
  113. MarlinImage image = noImage;
  114. switch (*string) {
  115. case 0x01: image = imgRefresh; break; // LCD_STR_REFRESH
  116. case 0x02: image = imgDirectory; break; // LCD_STR_FOLDER
  117. }
  118. uint8_t offset = MENU_TEXT_X_OFFSET;
  119. if (image != noImage) {
  120. string++;
  121. offset = MENU_ITEM_ICON_SPACE;
  122. tft.add_image(MENU_ITEM_ICON_X, MENU_ITEM_ICON_Y, image, COLOR_MENU_TEXT, sel ? COLOR_SELECTION_BG : COLOR_BACKGROUND);
  123. }
  124. tft_string.set(string, itemIndex, itemStringC, itemStringF);
  125. tft.add_text(offset, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  126. }
  127. // Draw a menu item with a (potentially) editable value
  128. void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const fstr, const char * const inStr, const bool pgm) {
  129. menu_item(row, sel);
  130. tft_string.set(fstr, itemIndex, itemStringC, itemStringF);
  131. tft.add_text(MENU_TEXT_X_OFFSET, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, tft_string);
  132. if (inStr) {
  133. tft_string.set(inStr);
  134. tft.add_text(TFT_WIDTH - MENU_TEXT_X_OFFSET - tft_string.width(), MENU_TEXT_Y_OFFSET, COLOR_MENU_VALUE, tft_string);
  135. }
  136. }
  137. // Draw a static item with no left-right margin required. Centered by default.
  138. void MenuItem_static::draw(const uint8_t row, FSTR_P const fstr, const uint8_t style/*=SS_DEFAULT*/, const char * const vstr/*=nullptr*/) {
  139. menu_item(row);
  140. tft_string.set(fstr, itemIndex, itemStringC, itemStringF);
  141. if (vstr) tft_string.add(vstr);
  142. tft.add_text(tft_string.center(TFT_WIDTH), MENU_TEXT_Y_OFFSET, COLOR_YELLOW, tft_string);
  143. }
  144. #if ENABLED(SDSUPPORT)
  145. void MenuItem_sdbase::draw(const bool sel, const uint8_t row, FSTR_P const, CardReader &theCard, const bool isDir) {
  146. menu_item(row, sel);
  147. if (isDir) tft.add_image(MENU_ITEM_ICON_X, MENU_ITEM_ICON_Y, imgDirectory, COLOR_MENU_TEXT, sel ? COLOR_SELECTION_BG : COLOR_BACKGROUND);
  148. constexpr uint8_t maxlen = (MENU_ITEM_HEIGHT) - (MENU_TEXT_Y_OFFSET) + 1;
  149. tft.add_text(MENU_ITEM_ICON_SPACE, MENU_TEXT_Y_OFFSET, COLOR_MENU_TEXT, ui.scrolled_filename(theCard, maxlen, row, sel));
  150. }
  151. #endif
  152. //
  153. // MarlinUI methods
  154. //
  155. bool MarlinUI::detected() { return true; }
  156. void MarlinUI::init_lcd() {
  157. tft.init();
  158. tft.set_font(MENU_FONT_NAME);
  159. #ifdef SYMBOLS_FONT_NAME
  160. tft.add_glyphs(SYMBOLS_FONT_NAME);
  161. #endif
  162. TERN_(TOUCH_SCREEN, touch.init());
  163. clear_lcd();
  164. }
  165. void MarlinUI::clear_lcd() {
  166. #if ENABLED(TOUCH_SCREEN)
  167. touch.reset();
  168. draw_menu_navigation = false;
  169. #endif
  170. tft.queue.reset();
  171. tft.fill(0, 0, TFT_WIDTH, TFT_HEIGHT, COLOR_BACKGROUND);
  172. cursor.set(0, 0);
  173. }
  174. #if HAS_LCD_BRIGHTNESS
  175. void MarlinUI::_set_brightness() {
  176. #if PIN_EXISTS(TFT_BACKLIGHT)
  177. if (PWM_PIN(TFT_BACKLIGHT_PIN))
  178. analogWrite(pin_t(TFT_BACKLIGHT_PIN), backlight ? brightness : 0);
  179. #endif
  180. }
  181. #endif
  182. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  183. void MarlinUI::touch_calibration_screen() {
  184. uint16_t x, y;
  185. calibrationState calibration_stage = touch_calibration.get_calibration_state();
  186. if (calibration_stage == CALIBRATION_NONE) {
  187. defer_status_screen(true);
  188. clear_lcd();
  189. calibration_stage = touch_calibration.calibration_start();
  190. }
  191. else {
  192. x = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].x;
  193. y = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].y;
  194. tft.canvas(x - 15, y - 15, 31, 31);
  195. tft.set_background(COLOR_BACKGROUND);
  196. }
  197. touch.clear();
  198. if (calibration_stage < CALIBRATION_SUCCESS) {
  199. switch (calibration_stage) {
  200. case CALIBRATION_TOP_LEFT: tft_string.set(GET_TEXT(MSG_TOP_LEFT)); break;
  201. case CALIBRATION_BOTTOM_LEFT: tft_string.set(GET_TEXT(MSG_BOTTOM_LEFT)); break;
  202. case CALIBRATION_TOP_RIGHT: tft_string.set(GET_TEXT(MSG_TOP_RIGHT)); break;
  203. case CALIBRATION_BOTTOM_RIGHT: tft_string.set(GET_TEXT(MSG_BOTTOM_RIGHT)); break;
  204. default: break;
  205. }
  206. x = touch_calibration.calibration_points[calibration_stage].x;
  207. y = touch_calibration.calibration_points[calibration_stage].y;
  208. tft.canvas(x - 15, y - 15, 31, 31);
  209. tft.set_background(COLOR_BACKGROUND);
  210. tft.add_bar(0, 15, 31, 1, COLOR_TOUCH_CALIBRATION);
  211. tft.add_bar(15, 0, 1, 31, COLOR_TOUCH_CALIBRATION);
  212. touch.add_control(CALIBRATE, 0, 0, TFT_WIDTH, TFT_HEIGHT, uint32_t(x) << 16 | uint32_t(y));
  213. }
  214. else {
  215. tft_string.set(calibration_stage == CALIBRATION_SUCCESS ? GET_TEXT(MSG_CALIBRATION_COMPLETED) : GET_TEXT(MSG_CALIBRATION_FAILED));
  216. defer_status_screen(false);
  217. touch_calibration.calibration_end();
  218. touch.add_control(BACK, 0, 0, TFT_WIDTH, TFT_HEIGHT);
  219. }
  220. tft.canvas(0, (TFT_HEIGHT - tft_string.font_height()) >> 1, TFT_WIDTH, tft_string.font_height());
  221. tft.set_background(COLOR_BACKGROUND);
  222. tft.add_text(tft_string.center(TFT_WIDTH), 0, COLOR_MENU_TEXT, tft_string);
  223. }
  224. #endif // TOUCH_SCREEN_CALIBRATION
  225. #endif // HAS_GRAPHICAL_TFT