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.

marlinui_HD44780.cpp 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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_MARLINUI_HD44780
  24. /**
  25. * marlinui_HD44780.cpp
  26. *
  27. * LCD display implementations for Hitachi HD44780.
  28. * These are the most common LCD character displays.
  29. */
  30. #include "marlinui_HD44780.h"
  31. #include "../marlinui.h"
  32. #include "../../libs/numtostr.h"
  33. #include "../../sd/cardreader.h"
  34. #include "../../module/temperature.h"
  35. #include "../../module/printcounter.h"
  36. #include "../../module/planner.h"
  37. #include "../../module/motion.h"
  38. #if DISABLED(LCD_PROGRESS_BAR) && BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  39. #include "../../feature/filwidth.h"
  40. #include "../../gcode/parser.h"
  41. #endif
  42. #if ENABLED(AUTO_BED_LEVELING_UBL)
  43. #include "../../feature/bedlevel/bedlevel.h"
  44. #endif
  45. //
  46. // Create LCD instance and chipset-specific information
  47. //
  48. #if ENABLED(LCD_I2C_TYPE_PCF8575)
  49. LCD_CLASS lcd(LCD_I2C_ADDRESS, LCD_I2C_PIN_EN, LCD_I2C_PIN_RW, LCD_I2C_PIN_RS, LCD_I2C_PIN_D4, LCD_I2C_PIN_D5, LCD_I2C_PIN_D6, LCD_I2C_PIN_D7);
  50. #elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)
  51. LCD_CLASS lcd(LCD_I2C_ADDRESS
  52. #ifdef DETECT_DEVICE
  53. , 1
  54. #endif
  55. );
  56. #elif ENABLED(LCD_I2C_TYPE_PCA8574)
  57. LCD_CLASS lcd(LCD_I2C_ADDRESS, LCD_WIDTH, LCD_HEIGHT);
  58. #elif ENABLED(SR_LCD_2W_NL)
  59. // 2 wire Non-latching LCD SR from:
  60. // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/schematics#!shiftregister-connection
  61. LCD_CLASS lcd(SR_DATA_PIN, SR_CLK_PIN
  62. #if PIN_EXISTS(SR_STROBE)
  63. , SR_STROBE_PIN
  64. #endif
  65. );
  66. #elif ENABLED(SR_LCD_3W_NL)
  67. // NewLiquidCrystal was not working
  68. // https://github.com/mikeshub/SailfishLCD
  69. // uses the code directly from Sailfish
  70. LCD_CLASS lcd(SR_STROBE_PIN, SR_DATA_PIN, SR_CLK_PIN);
  71. #elif ENABLED(LCM1602)
  72. LCD_CLASS lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  73. #elif ENABLED(YHCB2004)
  74. LCD_CLASS lcd(YHCB2004_CLK, 20, 4, YHCB2004_MOSI, YHCB2004_MISO); // CLK, cols, rows, MOSI, MISO
  75. #else
  76. // Standard direct-connected LCD implementations
  77. LCD_CLASS lcd(LCD_PINS_RS, LCD_PINS_ENABLE, LCD_PINS_D4, LCD_PINS_D5, LCD_PINS_D6, LCD_PINS_D7);
  78. #endif
  79. static void createChar_P(const char c, const byte * const ptr) {
  80. byte temp[8];
  81. LOOP_L_N(i, 8)
  82. temp[i] = pgm_read_byte(&ptr[i]);
  83. lcd.createChar(c, temp);
  84. }
  85. #if ENABLED(LCD_PROGRESS_BAR)
  86. #define LCD_STR_PROGRESS "\x03\x04\x05"
  87. #endif
  88. #if ENABLED(LCD_USE_I2C_BUZZER)
  89. void MarlinUI::buzz(const long duration, const uint16_t freq) {
  90. if (!buzzer_enabled) return;
  91. lcd.buzz(duration, freq);
  92. }
  93. #endif
  94. void MarlinUI::set_custom_characters(const HD44780CharSet screen_charset/*=CHARSET_INFO*/) {
  95. #if NONE(LCD_PROGRESS_BAR, SHOW_BOOTSCREEN)
  96. UNUSED(screen_charset);
  97. #endif
  98. // CHARSET_BOOT
  99. #if ENABLED(SHOW_BOOTSCREEN)
  100. const static PROGMEM byte corner[4][8] = { {
  101. B00000,
  102. B00000,
  103. B00000,
  104. B00000,
  105. B00001,
  106. B00010,
  107. B00100,
  108. B00100
  109. }, {
  110. B00000,
  111. B00000,
  112. B00000,
  113. B11100,
  114. B11100,
  115. B01100,
  116. B00100,
  117. B00100
  118. }, {
  119. B00100,
  120. B00010,
  121. B00001,
  122. B00000,
  123. B00000,
  124. B00000,
  125. B00000,
  126. B00000
  127. }, {
  128. B00100,
  129. B01000,
  130. B10000,
  131. B00000,
  132. B00000,
  133. B00000,
  134. B00000,
  135. B00000
  136. } };
  137. #endif // SHOW_BOOTSCREEN
  138. // CHARSET_INFO
  139. const static PROGMEM byte bedTemp[8] = {
  140. B00000,
  141. B11111,
  142. B10101,
  143. B10001,
  144. B10101,
  145. B11111,
  146. B00000,
  147. B00000
  148. };
  149. const static PROGMEM byte degree[8] = {
  150. B01100,
  151. B10010,
  152. B10010,
  153. B01100,
  154. B00000,
  155. B00000,
  156. B00000,
  157. B00000
  158. };
  159. const static PROGMEM byte thermometer[8] = {
  160. B00100,
  161. B01010,
  162. B01010,
  163. B01010,
  164. B01010,
  165. B10001,
  166. B10001,
  167. B01110
  168. };
  169. const static PROGMEM byte uplevel[8] = {
  170. B00100,
  171. B01110,
  172. B11111,
  173. B00100,
  174. B11100,
  175. B00000,
  176. B00000,
  177. B00000
  178. };
  179. const static PROGMEM byte feedrate[8] = {
  180. #if LCD_INFO_SCREEN_STYLE == 1
  181. B00000,
  182. B00100,
  183. B10010,
  184. B01001,
  185. B10010,
  186. B00100,
  187. B00000,
  188. B00000
  189. #else
  190. B11100,
  191. B10000,
  192. B11000,
  193. B10111,
  194. B00101,
  195. B00110,
  196. B00101,
  197. B00000
  198. #endif
  199. };
  200. const static PROGMEM byte clock[8] = {
  201. B00000,
  202. B01110,
  203. B10011,
  204. B10101,
  205. B10001,
  206. B01110,
  207. B00000,
  208. B00000
  209. };
  210. #if ENABLED(LCD_PROGRESS_BAR)
  211. // CHARSET_INFO
  212. const static PROGMEM byte progress[3][8] = { {
  213. B00000,
  214. B10000,
  215. B10000,
  216. B10000,
  217. B10000,
  218. B10000,
  219. B10000,
  220. B00000
  221. }, {
  222. B00000,
  223. B10100,
  224. B10100,
  225. B10100,
  226. B10100,
  227. B10100,
  228. B10100,
  229. B00000
  230. }, {
  231. B00000,
  232. B10101,
  233. B10101,
  234. B10101,
  235. B10101,
  236. B10101,
  237. B10101,
  238. B00000
  239. } };
  240. #endif // LCD_PROGRESS_BAR
  241. #if BOTH(SDSUPPORT, HAS_LCD_MENU)
  242. // CHARSET_MENU
  243. const static PROGMEM byte refresh[8] = {
  244. B00000,
  245. B00110,
  246. B11001,
  247. B11000,
  248. B00011,
  249. B10011,
  250. B01100,
  251. B00000,
  252. };
  253. const static PROGMEM byte folder[8] = {
  254. B00000,
  255. B11100,
  256. B11111,
  257. B10001,
  258. B10001,
  259. B11111,
  260. B00000,
  261. B00000
  262. };
  263. #endif // SDSUPPORT
  264. #if ENABLED(SHOW_BOOTSCREEN)
  265. // Set boot screen corner characters
  266. if (screen_charset == CHARSET_BOOT) {
  267. for (uint8_t i = 4; i--;)
  268. createChar_P(i, corner[i]);
  269. }
  270. else
  271. #endif
  272. { // Info Screen uses 5 special characters
  273. createChar_P(LCD_STR_BEDTEMP[0], bedTemp);
  274. createChar_P(LCD_STR_DEGREE[0], degree);
  275. createChar_P(LCD_STR_THERMOMETER[0], thermometer);
  276. createChar_P(LCD_STR_FEEDRATE[0], feedrate);
  277. createChar_P(LCD_STR_CLOCK[0], clock);
  278. #if ENABLED(LCD_PROGRESS_BAR)
  279. if (screen_charset == CHARSET_INFO) { // 3 Progress bar characters for info screen
  280. for (int16_t i = 3; i--;)
  281. createChar_P(LCD_STR_PROGRESS[i], progress[i]);
  282. }
  283. else
  284. #endif
  285. {
  286. createChar_P(LCD_STR_UPLEVEL[0], uplevel);
  287. #if BOTH(SDSUPPORT, HAS_LCD_MENU)
  288. // SD Card sub-menu special characters
  289. createChar_P(LCD_STR_REFRESH[0], refresh);
  290. createChar_P(LCD_STR_FOLDER[0], folder);
  291. #endif
  292. }
  293. }
  294. }
  295. void MarlinUI::init_lcd() {
  296. #if ENABLED(LCD_I2C_TYPE_PCF8575)
  297. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  298. #ifdef LCD_I2C_PIN_BL
  299. lcd.setBacklightPin(LCD_I2C_PIN_BL, POSITIVE);
  300. lcd.setBacklight(HIGH);
  301. #endif
  302. #elif ENABLED(LCD_I2C_TYPE_MCP23017)
  303. lcd.setMCPType(LTI_TYPE_MCP23017);
  304. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  305. update_indicators();
  306. #elif ENABLED(LCD_I2C_TYPE_MCP23008)
  307. lcd.setMCPType(LTI_TYPE_MCP23008);
  308. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  309. #elif ENABLED(LCD_I2C_TYPE_PCA8574)
  310. lcd.init();
  311. lcd.backlight();
  312. #else
  313. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  314. #endif
  315. set_custom_characters(on_status_screen() ? CHARSET_INFO : CHARSET_MENU);
  316. lcd.clear();
  317. }
  318. bool MarlinUI::detected() {
  319. return (true
  320. #if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
  321. && lcd.LcdDetected() == 1
  322. #endif
  323. );
  324. }
  325. #if HAS_SLOW_BUTTONS
  326. uint8_t MarlinUI::read_slow_buttons() {
  327. #if ENABLED(LCD_I2C_TYPE_MCP23017)
  328. // Reading these buttons is too slow for interrupt context
  329. // so they are read during LCD update in the main loop.
  330. uint8_t slow_bits = lcd.readButtons()
  331. #if !BUTTON_EXISTS(ENC)
  332. << B_I2C_BTN_OFFSET
  333. #endif
  334. ;
  335. #if ENABLED(LCD_I2C_VIKI)
  336. if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked
  337. slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated
  338. #endif
  339. return slow_bits;
  340. #endif // LCD_I2C_TYPE_MCP23017
  341. }
  342. #endif
  343. void MarlinUI::clear_lcd() { lcd.clear(); }
  344. #if ENABLED(SHOW_BOOTSCREEN)
  345. void lcd_erase_line(const lcd_uint_t line) {
  346. lcd_moveto(0, line);
  347. for (uint8_t i = LCD_WIDTH + 1; --i;)
  348. lcd_put_wchar(' ');
  349. }
  350. // Scroll the PSTR 'text' in a 'len' wide field for 'time' milliseconds at position col,line
  351. void lcd_scroll(const lcd_uint_t col, const lcd_uint_t line, PGM_P const text, const uint8_t len, const int16_t time) {
  352. uint8_t slen = utf8_strlen_P(text);
  353. if (slen < len) {
  354. lcd_put_u8str_max_P(col, line, text, len);
  355. for (; slen < len; ++slen) lcd_put_wchar(' ');
  356. safe_delay(time);
  357. }
  358. else {
  359. PGM_P p = text;
  360. int dly = time / _MAX(slen, 1);
  361. LOOP_LE_N(i, slen) {
  362. // Print the text at the correct place
  363. lcd_put_u8str_max_P(col, line, p, len);
  364. // Fill with spaces
  365. for (uint8_t ix = slen - i; ix < len; ++ix) lcd_put_wchar(' ');
  366. // Delay
  367. safe_delay(dly);
  368. // Advance to the next UTF8 valid position
  369. p++;
  370. while (!START_OF_UTF8_CHAR(pgm_read_byte(p))) p++;
  371. }
  372. }
  373. }
  374. static void logo_lines(PGM_P const extra) {
  375. int16_t indent = (LCD_WIDTH - 8 - utf8_strlen_P(extra)) / 2;
  376. lcd_put_wchar(indent, 0, '\x00'); lcd_put_u8str_P(PSTR( "------" )); lcd_put_wchar('\x01');
  377. lcd_put_u8str_P(indent, 1, PSTR("|Marlin|")); lcd_put_u8str_P(extra);
  378. lcd_put_wchar(indent, 2, '\x02'); lcd_put_u8str_P(PSTR( "------" )); lcd_put_wchar('\x03');
  379. }
  380. void MarlinUI::show_bootscreen() {
  381. set_custom_characters(CHARSET_BOOT);
  382. lcd.clear();
  383. #define LCD_EXTRA_SPACE (LCD_WIDTH-8)
  384. #define CENTER_OR_SCROLL(STRING,DELAY) \
  385. lcd_erase_line(3); \
  386. if (utf8_strlen(STRING) <= LCD_WIDTH) { \
  387. lcd_put_u8str_P((LCD_WIDTH - utf8_strlen_P(PSTR(STRING))) / 2, 3, PSTR(STRING)); \
  388. safe_delay(DELAY); \
  389. } \
  390. else { \
  391. lcd_scroll(0, 3, PSTR(STRING), LCD_WIDTH, DELAY); \
  392. }
  393. //
  394. // Show the Marlin logo with splash line 1
  395. //
  396. if (LCD_EXTRA_SPACE >= utf8_strlen(SHORT_BUILD_VERSION) + 1) {
  397. //
  398. // Show the Marlin logo, splash line1, and splash line 2
  399. //
  400. logo_lines(PSTR(" " SHORT_BUILD_VERSION));
  401. CENTER_OR_SCROLL(MARLIN_WEBSITE_URL, 2000);
  402. }
  403. else {
  404. //
  405. // Show the Marlin logo and short build version
  406. // After a delay show the website URL
  407. //
  408. logo_lines(NUL_STR);
  409. CENTER_OR_SCROLL(SHORT_BUILD_VERSION, 1500);
  410. CENTER_OR_SCROLL(MARLIN_WEBSITE_URL, 1500);
  411. #ifdef STRING_SPLASH_LINE3
  412. CENTER_OR_SCROLL(STRING_SPLASH_LINE3, 1500);
  413. #endif
  414. }
  415. lcd.clear();
  416. safe_delay(100);
  417. set_custom_characters(CHARSET_INFO);
  418. lcd.clear();
  419. }
  420. #endif // SHOW_BOOTSCREEN
  421. void MarlinUI::draw_kill_screen() {
  422. lcd_put_u8str(0, 0, status_message);
  423. lcd_uint_t y = 2;
  424. #if LCD_HEIGHT >= 4
  425. lcd_put_u8str_P(0, y++, GET_TEXT(MSG_HALTED));
  426. #endif
  427. lcd_put_u8str_P(0, y, GET_TEXT(MSG_PLEASE_RESET));
  428. }
  429. //
  430. // Before homing, blink '123' <-> '???'.
  431. // Homed but unknown... '123' <-> ' '.
  432. // Homed and known, display constantly.
  433. //
  434. FORCE_INLINE void _draw_axis_value(const AxisEnum axis, const char *value, const bool blink) {
  435. lcd_put_wchar('X' + uint8_t(axis));
  436. if (blink)
  437. lcd_put_u8str(value);
  438. else if (axis_should_home(axis))
  439. while (const char c = *value++) lcd_put_wchar(c <= '.' ? c : '?');
  440. else if (NONE(HOME_AFTER_DEACTIVATE, DISABLE_REDUCED_ACCURACY_WARNING) && !axis_is_trusted(axis))
  441. lcd_put_u8str_P(axis == Z_AXIS ? PSTR(" ") : PSTR(" "));
  442. else
  443. lcd_put_u8str(value);
  444. }
  445. FORCE_INLINE void _draw_heater_status(const heater_id_t heater_id, const char prefix, const bool blink) {
  446. #if HAS_HEATED_BED
  447. const bool isBed = TERN(HAS_HEATED_CHAMBER, heater_id == H_BED, heater_id < 0);
  448. const float t1 = (isBed ? thermalManager.degBed() : thermalManager.degHotend(heater_id)),
  449. t2 = (isBed ? thermalManager.degTargetBed() : thermalManager.degTargetHotend(heater_id));
  450. #else
  451. const float t1 = thermalManager.degHotend(heater_id), t2 = thermalManager.degTargetHotend(heater_id);
  452. #endif
  453. if (prefix >= 0) lcd_put_wchar(prefix);
  454. lcd_put_u8str(i16tostr3rj(t1 + 0.5));
  455. lcd_put_wchar('/');
  456. #if !HEATER_IDLE_HANDLER
  457. UNUSED(blink);
  458. #else
  459. if (!blink && thermalManager.heater_idle[thermalManager.idle_index_for_id(heater_id)].timed_out) {
  460. lcd_put_wchar(' ');
  461. if (t2 >= 10) lcd_put_wchar(' ');
  462. if (t2 >= 100) lcd_put_wchar(' ');
  463. }
  464. else
  465. #endif
  466. lcd_put_u8str(i16tostr3left(t2 + 0.5));
  467. if (prefix >= 0) {
  468. lcd_put_wchar(LCD_STR_DEGREE[0]);
  469. lcd_put_wchar(' ');
  470. if (t2 < 10) lcd_put_wchar(' ');
  471. }
  472. }
  473. FORCE_INLINE void _draw_bed_status(const bool blink) {
  474. _draw_heater_status(H_BED, TERN0(HAS_LEVELING, blink && planner.leveling_active) ? '_' : LCD_STR_BEDTEMP[0], blink);
  475. }
  476. #if HAS_PRINT_PROGRESS
  477. FORCE_INLINE void _draw_print_progress() {
  478. const uint8_t progress = ui.get_progress_percent();
  479. lcd_put_u8str_P(PSTR(TERN(SDSUPPORT, "SD", "P:")));
  480. if (progress)
  481. lcd_put_u8str(ui8tostr3rj(progress));
  482. else
  483. lcd_put_u8str_P(PSTR("---"));
  484. lcd_put_wchar('%');
  485. }
  486. #endif
  487. #if ENABLED(LCD_PROGRESS_BAR)
  488. void MarlinUI::draw_progress_bar(const uint8_t percent) {
  489. const int16_t tix = int16_t(percent * (LCD_WIDTH) * 3) / 100,
  490. cel = tix / 3,
  491. rem = tix % 3;
  492. uint8_t i = LCD_WIDTH;
  493. char msg[LCD_WIDTH + 1], b = ' ';
  494. msg[LCD_WIDTH] = '\0';
  495. while (i--) {
  496. if (i == cel - 1)
  497. b = LCD_STR_PROGRESS[2];
  498. else if (i == cel && rem != 0)
  499. b = LCD_STR_PROGRESS[rem - 1];
  500. msg[i] = b;
  501. }
  502. lcd_put_u8str(msg);
  503. }
  504. #endif // LCD_PROGRESS_BAR
  505. void MarlinUI::draw_status_message(const bool blink) {
  506. lcd_moveto(0, LCD_HEIGHT - 1);
  507. #if ENABLED(LCD_PROGRESS_BAR)
  508. // Draw the progress bar if the message has shown long enough
  509. // or if there is no message set.
  510. if (ELAPSED(millis(), progress_bar_ms + PROGRESS_BAR_MSG_TIME) || !has_status()) {
  511. const uint8_t progress = get_progress_percent();
  512. if (progress > 2) return draw_progress_bar(progress);
  513. }
  514. #elif BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  515. // Alternate Status message and Filament display
  516. if (ELAPSED(millis(), next_filament_display)) {
  517. lcd_put_u8str_P(PSTR("Dia "));
  518. lcd_put_u8str(ftostr12ns(filwidth.measured_mm));
  519. lcd_put_u8str_P(PSTR(" V"));
  520. lcd_put_u8str(i16tostr3rj(planner.volumetric_percent(parser.volumetric_enabled)));
  521. lcd_put_wchar('%');
  522. return;
  523. }
  524. #endif // FILAMENT_LCD_DISPLAY && SDSUPPORT
  525. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  526. static bool last_blink = false;
  527. // Get the UTF8 character count of the string
  528. uint8_t slen = utf8_strlen(status_message);
  529. // If the string fits into the LCD, just print it and do not scroll it
  530. if (slen <= LCD_WIDTH) {
  531. // The string isn't scrolling and may not fill the screen
  532. lcd_put_u8str(status_message);
  533. // Fill the rest with spaces
  534. while (slen < LCD_WIDTH) { lcd_put_wchar(' '); ++slen; }
  535. }
  536. else {
  537. // String is larger than the available space in screen.
  538. // Get a pointer to the next valid UTF8 character
  539. // and the string remaining length
  540. uint8_t rlen;
  541. const char *stat = status_and_len(rlen);
  542. lcd_put_u8str_max(stat, LCD_WIDTH); // The string leaves space
  543. // If the remaining string doesn't completely fill the screen
  544. if (rlen < LCD_WIDTH) {
  545. lcd_put_wchar('.'); // Always at 1+ spaces left, draw a dot
  546. uint8_t chars = LCD_WIDTH - rlen; // Amount of space left in characters
  547. if (--chars) { // Draw a second dot if there's space
  548. lcd_put_wchar('.');
  549. if (--chars)
  550. lcd_put_u8str_max(status_message, chars); // Print a second copy of the message
  551. }
  552. }
  553. if (last_blink != blink) {
  554. last_blink = blink;
  555. advance_status_scroll();
  556. }
  557. }
  558. #else
  559. UNUSED(blink);
  560. // Get the UTF8 character count of the string
  561. uint8_t slen = utf8_strlen(status_message);
  562. // Just print the string to the LCD
  563. lcd_put_u8str_max(status_message, LCD_WIDTH);
  564. // Fill the rest with spaces if there are missing spaces
  565. while (slen < LCD_WIDTH) {
  566. lcd_put_wchar(' ');
  567. ++slen;
  568. }
  569. #endif
  570. }
  571. /**
  572. * LCD_INFO_SCREEN_STYLE 0 : Classic Status Screen
  573. *
  574. * 16x2 |000/000 B000/000|
  575. * |0123456789012345|
  576. *
  577. * 16x4 |000/000 B000/000|
  578. * |SD---% Z 000.00|
  579. * |F---% T--:--|
  580. * |0123456789012345|
  581. *
  582. * 20x2 |T000/000° B000/000° |
  583. * |01234567890123456789|
  584. *
  585. * 20x4 |T000/000° B000/000° |
  586. * |X 000 Y 000 Z000.000|
  587. * |F---% SD---% T--:--|
  588. * |01234567890123456789|
  589. *
  590. * LCD_INFO_SCREEN_STYLE 1 : Průša-style Status Screen
  591. *
  592. * |T000/000° Z 000.00 |
  593. * |B000/000° F---% |
  594. * |SD---% T--:-- |
  595. * |01234567890123456789|
  596. *
  597. * |T000/000° Z 000.00 |
  598. * |T000/000° F---% |
  599. * |B000/000° SD---% |
  600. * |01234567890123456789|
  601. */
  602. inline uint8_t draw_elapsed_or_remaining_time(uint8_t timepos, const bool blink) {
  603. char buffer[14];
  604. #if ENABLED(SHOW_REMAINING_TIME)
  605. const bool show_remain = TERN1(ROTATE_PROGRESS_DISPLAY, blink) && (printingIsActive() || marlin_state == MF_SD_COMPLETE);
  606. if (show_remain) {
  607. #if ENABLED(USE_M73_REMAINING_TIME)
  608. duration_t remaining = ui.get_remaining_time();
  609. #else
  610. uint8_t progress = ui.get_progress_percent();
  611. uint32_t elapsed = print_job_timer.duration();
  612. duration_t remaining = (progress > 0) ? ((elapsed * 25600 / progress) >> 8) - elapsed : 0;
  613. #endif
  614. timepos -= remaining.toDigital(buffer);
  615. lcd_put_wchar(timepos, 2, 'R');
  616. }
  617. #else
  618. constexpr bool show_remain = false;
  619. #endif
  620. if (!show_remain) {
  621. duration_t elapsed = print_job_timer.duration();
  622. timepos -= elapsed.toDigital(buffer);
  623. lcd_put_wchar(timepos, 2, LCD_STR_CLOCK[0]);
  624. }
  625. lcd_put_u8str(buffer);
  626. return timepos;
  627. }
  628. void MarlinUI::draw_status_screen() {
  629. const bool blink = get_blink();
  630. lcd_moveto(0, 0);
  631. #if LCD_INFO_SCREEN_STYLE == 0
  632. // ========== Line 1 ==========
  633. #if LCD_WIDTH < 20
  634. //
  635. // Hotend 0 Temperature
  636. //
  637. _draw_heater_status(H_E0, -1, blink);
  638. //
  639. // Hotend 1 or Bed Temperature
  640. //
  641. #if HAS_MULTI_HOTEND
  642. lcd_moveto(8, 0);
  643. _draw_heater_status(H_E1, LCD_STR_THERMOMETER[0], blink);
  644. #elif HAS_HEATED_BED
  645. lcd_moveto(8, 0);
  646. _draw_bed_status(blink);
  647. #endif
  648. #else // LCD_WIDTH >= 20
  649. //
  650. // Hotend 0 Temperature
  651. //
  652. _draw_heater_status(H_E0, LCD_STR_THERMOMETER[0], blink);
  653. //
  654. // Hotend 1 or Bed Temperature
  655. //
  656. #if HAS_MULTI_HOTEND
  657. lcd_moveto(10, 0);
  658. _draw_heater_status(H_E1, LCD_STR_THERMOMETER[0], blink);
  659. #elif HAS_HEATED_BED
  660. lcd_moveto(10, 0);
  661. _draw_bed_status(blink);
  662. #endif
  663. #endif // LCD_WIDTH >= 20
  664. // ========== Line 2 ==========
  665. #if LCD_HEIGHT > 2
  666. #if LCD_WIDTH < 20
  667. #if HAS_PRINT_PROGRESS
  668. lcd_moveto(0, 2);
  669. _draw_print_progress();
  670. #endif
  671. #else // LCD_WIDTH >= 20
  672. lcd_moveto(0, 1);
  673. // If the first line has two extruder temps,
  674. // show more temperatures on the next line
  675. #if HOTENDS > 2 || (HAS_MULTI_HOTEND && HAS_HEATED_BED)
  676. #if HOTENDS > 2
  677. _draw_heater_status(H_E2, LCD_STR_THERMOMETER[0], blink);
  678. lcd_moveto(10, 1);
  679. #endif
  680. _draw_bed_status(blink);
  681. #else // HOTENDS <= 2 && (HOTENDS <= 1 || !HAS_HEATED_BED)
  682. #if HAS_DUAL_MIXING
  683. // Two-component mix / gradient instead of XY
  684. char mixer_messages[12];
  685. const char *mix_label;
  686. #if ENABLED(GRADIENT_MIX)
  687. if (mixer.gradient.enabled) {
  688. mixer.update_mix_from_gradient();
  689. mix_label = "Gr";
  690. }
  691. else
  692. #endif
  693. {
  694. mixer.update_mix_from_vtool();
  695. mix_label = "Mx";
  696. }
  697. sprintf_P(mixer_messages, PSTR("%s %d;%d%% "), mix_label, int(mixer.mix[0]), int(mixer.mix[1]));
  698. lcd_put_u8str(mixer_messages);
  699. #else // !HAS_DUAL_MIXING
  700. const bool show_e_total = TERN0(LCD_SHOW_E_TOTAL, printingIsActive() || marlin_state == MF_SD_COMPLETE);
  701. if (show_e_total) {
  702. #if ENABLED(LCD_SHOW_E_TOTAL)
  703. char tmp[20];
  704. const uint8_t escale = e_move_accumulator >= 100000.0f ? 10 : 1; // After 100m switch to cm
  705. sprintf_P(tmp, PSTR("E %ld%cm "), uint32_t(_MAX(e_move_accumulator, 0.0f)) / escale, escale == 10 ? 'c' : 'm'); // 1234567mm
  706. lcd_put_u8str(tmp);
  707. #endif
  708. }
  709. else {
  710. const xy_pos_t lpos = current_position.asLogical();
  711. _draw_axis_value(X_AXIS, ftostr4sign(lpos.x), blink);
  712. lcd_put_wchar(' ');
  713. _draw_axis_value(Y_AXIS, ftostr4sign(lpos.y), blink);
  714. }
  715. #endif // !HAS_DUAL_MIXING
  716. #endif // HOTENDS <= 2 && (HOTENDS <= 1 || !HAS_HEATED_BED)
  717. #endif // LCD_WIDTH >= 20
  718. lcd_moveto(LCD_WIDTH - 8, 1);
  719. _draw_axis_value(Z_AXIS, ftostr52sp(LOGICAL_Z_POSITION(current_position.z)), blink);
  720. #if HAS_LEVELING && !HAS_HEATED_BED
  721. lcd_put_wchar(planner.leveling_active || blink ? '_' : ' ');
  722. #endif
  723. #endif // LCD_HEIGHT > 2
  724. // ========== Line 3 ==========
  725. #if LCD_HEIGHT > 3
  726. lcd_put_wchar(0, 2, LCD_STR_FEEDRATE[0]);
  727. lcd_put_u8str(i16tostr3rj(feedrate_percentage));
  728. lcd_put_wchar('%');
  729. const uint8_t timepos = draw_elapsed_or_remaining_time(LCD_WIDTH - 1, blink);
  730. #if LCD_WIDTH >= 20
  731. lcd_moveto(timepos - 7, 2);
  732. #if HAS_PRINT_PROGRESS
  733. _draw_print_progress();
  734. #else
  735. char c;
  736. uint16_t per;
  737. #if HAS_FAN0
  738. if (true
  739. #if EXTRUDERS && ENABLED(ADAPTIVE_FAN_SLOWING)
  740. && (blink || thermalManager.fan_speed_scaler[0] < 128)
  741. #endif
  742. ) {
  743. uint16_t spd = thermalManager.fan_speed[0];
  744. if (blink) c = 'F';
  745. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  746. else { c = '*'; spd = thermalManager.scaledFanSpeed(0, spd); }
  747. #endif
  748. per = thermalManager.fanPercent(spd);
  749. }
  750. else
  751. #endif
  752. {
  753. #if EXTRUDERS
  754. c = 'E';
  755. per = planner.flow_percentage[0];
  756. #endif
  757. }
  758. lcd_put_wchar(c);
  759. lcd_put_u8str(i16tostr3rj(per));
  760. lcd_put_wchar('%');
  761. #endif
  762. #endif
  763. #endif // LCD_HEIGHT > 3
  764. #elif LCD_INFO_SCREEN_STYLE == 1
  765. // ========== Line 1 ==========
  766. //
  767. // Hotend 0 Temperature
  768. //
  769. _draw_heater_status(H_E0, LCD_STR_THERMOMETER[0], blink);
  770. //
  771. // Z Coordinate
  772. //
  773. lcd_moveto(LCD_WIDTH - 9, 0);
  774. _draw_axis_value(Z_AXIS, ftostr52sp(LOGICAL_Z_POSITION(current_position.z)), blink);
  775. #if HAS_LEVELING && (HAS_MULTI_HOTEND || !HAS_HEATED_BED)
  776. lcd_put_wchar(LCD_WIDTH - 1, 0, planner.leveling_active || blink ? '_' : ' ');
  777. #endif
  778. // ========== Line 2 ==========
  779. //
  780. // Hotend 1 or Bed Temperature
  781. //
  782. lcd_moveto(0, 1);
  783. #if HAS_MULTI_HOTEND
  784. _draw_heater_status(H_E1, LCD_STR_THERMOMETER[0], blink);
  785. #elif HAS_HEATED_BED
  786. _draw_bed_status(blink);
  787. #endif
  788. lcd_put_wchar(LCD_WIDTH - 9, 1, LCD_STR_FEEDRATE[0]);
  789. lcd_put_u8str(i16tostr3rj(feedrate_percentage));
  790. lcd_put_wchar('%');
  791. // ========== Line 3 ==========
  792. //
  793. // SD Percent, Hotend 2, or Bed
  794. //
  795. lcd_moveto(0, 2);
  796. #if HOTENDS > 2
  797. _draw_heater_status(H_E2, LCD_STR_THERMOMETER[0], blink);
  798. #elif HAS_MULTI_HOTEND && HAS_HEATED_BED
  799. _draw_bed_status(blink);
  800. #elif HAS_PRINT_PROGRESS
  801. #define DREW_PRINT_PROGRESS 1
  802. _draw_print_progress();
  803. #endif
  804. //
  805. // Elapsed Time or SD Percent
  806. //
  807. lcd_moveto(LCD_WIDTH - 9, 2);
  808. #if HAS_PRINT_PROGRESS && !DREW_PRINT_PROGRESS
  809. _draw_print_progress();
  810. #else
  811. (void)draw_elapsed_or_remaining_time(LCD_WIDTH - 4, blink);
  812. #endif
  813. #endif // LCD_INFO_SCREEN_STYLE 1
  814. // ========= Last Line ========
  815. //
  816. // Status Message (which may be a Progress Bar or Filament display)
  817. //
  818. draw_status_message(blink);
  819. }
  820. #if HAS_LCD_MENU
  821. #include "../menu/menu.h"
  822. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  823. void MarlinUI::draw_hotend_status(const uint8_t row, const uint8_t extruder) {
  824. if (row < LCD_HEIGHT) {
  825. lcd_moveto(LCD_WIDTH - 9, row);
  826. _draw_heater_status((heater_id_t)extruder, LCD_STR_THERMOMETER[0], get_blink());
  827. }
  828. }
  829. #endif // ADVANCED_PAUSE_FEATURE
  830. // Draw a static item with no left-right margin required. Centered by default.
  831. void MenuItem_static::draw(const uint8_t row, PGM_P const pstr, const uint8_t style/*=SS_DEFAULT*/, const char * const vstr/*=nullptr*/) {
  832. int8_t n = LCD_WIDTH;
  833. lcd_moveto(0, row);
  834. const int8_t plen = pstr ? utf8_strlen_P(pstr) : 0,
  835. vlen = vstr ? utf8_strlen(vstr) : 0;
  836. if (style & SS_CENTER) {
  837. int8_t pad = (LCD_WIDTH - plen - vlen) / 2;
  838. while (--pad >= 0) { lcd_put_wchar(' '); n--; }
  839. }
  840. if (plen) n = lcd_put_u8str_ind_P(pstr, itemIndex, itemString, n);
  841. if (vlen) n -= lcd_put_u8str_max(vstr, n);
  842. for (; n > 0; --n) lcd_put_wchar(' ');
  843. }
  844. // Draw a generic menu item with pre_char (if selected) and post_char
  845. void MenuItemBase::_draw(const bool sel, const uint8_t row, PGM_P const pstr, const char pre_char, const char post_char) {
  846. lcd_put_wchar(0, row, sel ? pre_char : ' ');
  847. uint8_t n = lcd_put_u8str_ind_P(pstr, itemIndex, itemString, LCD_WIDTH - 2);
  848. for (; n; --n) lcd_put_wchar(' ');
  849. lcd_put_wchar(post_char);
  850. }
  851. // Draw a menu item with a (potentially) editable value
  852. void MenuEditItemBase::draw(const bool sel, const uint8_t row, PGM_P const pstr, const char* const inStr, const bool pgm) {
  853. const uint8_t vlen = inStr ? (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)) : 0;
  854. lcd_put_wchar(0, row, sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
  855. uint8_t n = lcd_put_u8str_ind_P(pstr, itemIndex, itemString, LCD_WIDTH - 2 - vlen);
  856. if (vlen) {
  857. lcd_put_wchar(':');
  858. for (; n; --n) lcd_put_wchar(' ');
  859. if (pgm) lcd_put_u8str_P(inStr); else lcd_put_u8str(inStr);
  860. }
  861. }
  862. // Low-level draw_edit_screen can be used to draw an edit screen from anyplace
  863. void MenuEditItemBase::draw_edit_screen(PGM_P const pstr, const char* const value/*=nullptr*/) {
  864. ui.encoder_direction_normal();
  865. uint8_t n = lcd_put_u8str_ind_P(0, 1, pstr, itemIndex, itemString, LCD_WIDTH - 1);
  866. if (value) {
  867. lcd_put_wchar(':'); n--;
  868. const uint8_t len = utf8_strlen(value) + 1; // Plus one for a leading space
  869. const lcd_uint_t valrow = n < len ? 2 : 1; // Value on the next row if it won't fit
  870. lcd_put_wchar(LCD_WIDTH - len, valrow, ' '); // Right-justified, padded, leading space
  871. lcd_put_u8str(value);
  872. }
  873. }
  874. // The Select Screen presents a prompt and two "buttons"
  875. void MenuItem_confirm::draw_select_screen(PGM_P const yes, PGM_P const no, const bool yesno, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  876. ui.draw_select_screen_prompt(pref, string, suff);
  877. SETCURSOR(0, LCD_HEIGHT - 1);
  878. lcd_put_wchar(yesno ? ' ' : '['); lcd_put_u8str_P(no); lcd_put_wchar(yesno ? ' ' : ']');
  879. SETCURSOR_RJ(utf8_strlen_P(yes) + 2, LCD_HEIGHT - 1);
  880. lcd_put_wchar(yesno ? '[' : ' '); lcd_put_u8str_P(yes); lcd_put_wchar(yesno ? ']' : ' ');
  881. }
  882. #if ENABLED(SDSUPPORT)
  883. void MenuItem_sdbase::draw(const bool sel, const uint8_t row, PGM_P const, CardReader &theCard, const bool isDir) {
  884. lcd_put_wchar(0, row, sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
  885. constexpr uint8_t maxlen = LCD_WIDTH - 2;
  886. uint8_t n = maxlen - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), maxlen);
  887. for (; n; --n) lcd_put_wchar(' ');
  888. lcd_put_wchar(isDir ? LCD_STR_FOLDER[0] : ' ');
  889. }
  890. #endif
  891. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  892. void MarlinUI::update_indicators() {
  893. // Set the LEDS - referred to as backlights by the LiquidTWI2 library
  894. static uint8_t ledsprev = 0;
  895. uint8_t leds = 0;
  896. if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0)) leds |= LED_A;
  897. if (TERN0(HAS_HOTEND, thermalManager.degTargetHotend(0) > 0)) leds |= LED_B;
  898. #if HAS_FAN
  899. if ( TERN0(HAS_FAN0, thermalManager.fan_speed[0])
  900. || TERN0(HAS_FAN1, thermalManager.fan_speed[1])
  901. || TERN0(HAS_FAN2, thermalManager.fan_speed[2])
  902. || TERN0(HAS_FAN3, thermalManager.fan_speed[3])
  903. || TERN0(HAS_FAN4, thermalManager.fan_speed[4])
  904. || TERN0(HAS_FAN5, thermalManager.fan_speed[5])
  905. || TERN0(HAS_FAN6, thermalManager.fan_speed[6])
  906. || TERN0(HAS_FAN7, thermalManager.fan_speed[7])
  907. ) leds |= LED_C;
  908. #endif // HAS_FAN
  909. if (TERN0(HAS_MULTI_HOTEND, thermalManager.degTargetHotend(1) > 0)) leds |= LED_C;
  910. if (leds != ledsprev) {
  911. lcd.setBacklight(leds);
  912. ledsprev = leds;
  913. }
  914. }
  915. #endif // LCD_HAS_STATUS_INDICATORS
  916. #if ENABLED(AUTO_BED_LEVELING_UBL)
  917. #define HD44780_CHAR_WIDTH 5
  918. #define HD44780_CHAR_HEIGHT 8
  919. #define MESH_MAP_COLS 7
  920. #define MESH_MAP_ROWS 4
  921. #define CHAR_LINE_TOP 0
  922. #define CHAR_LINE_BOT 1
  923. #define CHAR_EDGE_L 2
  924. #define CHAR_EDGE_R 3
  925. #define CHAR_UL_UL 4
  926. #define CHAR_LR_UL 5
  927. #define CHAR_UL_LR 6
  928. #define CHAR_LR_LR 7
  929. #define TOP_LEFT _BV(0)
  930. #define TOP_RIGHT _BV(1)
  931. #define LOWER_LEFT _BV(2)
  932. #define LOWER_RIGHT _BV(3)
  933. /**
  934. * Possible map screens:
  935. *
  936. * 16x2 |X000.00 Y000.00|
  937. * |(00,00) Z00.000|
  938. *
  939. * 20x2 | X:000.00 Y:000.00 |
  940. * | (00,00) Z:00.000 |
  941. *
  942. * 16x4 |+-------+(00,00)|
  943. * || |X000.00|
  944. * || |Y000.00|
  945. * |+-------+Z00.000|
  946. *
  947. * 20x4 | +-------+ (00,00) |
  948. * | | | X:000.00|
  949. * | | | Y:000.00|
  950. * | +-------+ Z:00.000|
  951. */
  952. typedef struct {
  953. uint8_t custom_char_bits[HD44780_CHAR_HEIGHT];
  954. } custom_char;
  955. typedef struct {
  956. lcd_uint_t column, row,
  957. x_pixel_offset, y_pixel_offset;
  958. uint8_t x_pixel_mask;
  959. } coordinate;
  960. void add_edges_to_custom_char(custom_char &custom, const coordinate &ul, const coordinate &lr, const coordinate &brc, const uint8_t cell_location);
  961. FORCE_INLINE static void clear_custom_char(custom_char * const cc) { ZERO(cc->custom_char_bits); }
  962. coordinate pixel_location(int16_t x, int16_t y) {
  963. coordinate ret_val;
  964. int16_t xp, yp, r, c;
  965. x++; y++; // +1 because lines on the left and top
  966. c = x / (HD44780_CHAR_WIDTH);
  967. r = y / (HD44780_CHAR_HEIGHT);
  968. ret_val.column = c;
  969. ret_val.row = r;
  970. xp = x - c * (HD44780_CHAR_WIDTH); // Get the pixel offsets into the character cell
  971. xp = HD44780_CHAR_WIDTH - 1 - xp; // Column within relevant character cell (0 on the right)
  972. yp = y - r * (HD44780_CHAR_HEIGHT);
  973. ret_val.x_pixel_mask = _BV(xp);
  974. ret_val.x_pixel_offset = xp;
  975. ret_val.y_pixel_offset = yp;
  976. return ret_val;
  977. }
  978. inline coordinate pixel_location(const lcd_uint_t x, const lcd_uint_t y) { return pixel_location((int16_t)x, (int16_t)y); }
  979. void prep_and_put_map_char(custom_char &chrdata, const coordinate &ul, const coordinate &lr, const coordinate &brc, const uint8_t cl, const char c, const lcd_uint_t x, const lcd_uint_t y) {
  980. add_edges_to_custom_char(chrdata, ul, lr, brc, cl);
  981. lcd.createChar(c, (uint8_t*)&chrdata);
  982. lcd_put_wchar(x, y, c);
  983. }
  984. void MarlinUI::ubl_plot(const uint8_t x_plot, const uint8_t y_plot) {
  985. #if LCD_WIDTH >= 20
  986. #define _LCD_W_POS 12
  987. #define _PLOT_X 1
  988. #define _MAP_X 3
  989. #define _LABEL(C,X,Y) lcd_put_u8str_P(X, Y, C)
  990. #define _XLABEL(X,Y) _LABEL(X_LBL,X,Y)
  991. #define _YLABEL(X,Y) _LABEL(Y_LBL,X,Y)
  992. #define _ZLABEL(X,Y) _LABEL(Z_LBL,X,Y)
  993. #else
  994. #define _LCD_W_POS 8
  995. #define _PLOT_X 0
  996. #define _MAP_X 1
  997. #define _LABEL(X,Y,C) lcd_put_wchar(X, Y, C)
  998. #define _XLABEL(X,Y) _LABEL('X',X,Y)
  999. #define _YLABEL(X,Y) _LABEL('Y',X,Y)
  1000. #define _ZLABEL(X,Y) _LABEL('Z',X,Y)
  1001. #endif
  1002. #if LCD_HEIGHT <= 3 // 16x2 or 20x2 display
  1003. /**
  1004. * Show X and Y positions
  1005. */
  1006. _XLABEL(_PLOT_X, 0);
  1007. lcd_put_u8str(ftostr52(LOGICAL_X_POSITION(ubl.mesh_index_to_xpos(x_plot))));
  1008. _YLABEL(_LCD_W_POS, 0);
  1009. lcd_put_u8str(ftostr52(LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos(y_plot))));
  1010. lcd_moveto(_PLOT_X, 0);
  1011. #else // 16x4 or 20x4 display
  1012. coordinate upper_left, lower_right, bottom_right_corner;
  1013. custom_char new_char;
  1014. uint8_t i, n, n_rows, n_cols;
  1015. lcd_uint_t j, k, l, m, bottom_line, right_edge,
  1016. x_map_pixels, y_map_pixels,
  1017. pixels_per_x_mesh_pnt, pixels_per_y_mesh_pnt,
  1018. suppress_x_offset = 0, suppress_y_offset = 0;
  1019. const uint8_t y_plot_inv = (GRID_MAX_POINTS_Y - 1) - y_plot;
  1020. upper_left.column = 0;
  1021. upper_left.row = 0;
  1022. lower_right.column = 0;
  1023. lower_right.row = 0;
  1024. clear_lcd();
  1025. x_map_pixels = (HD44780_CHAR_WIDTH) * (MESH_MAP_COLS) - 2; // Minus 2 because we are drawing a box around the map
  1026. y_map_pixels = (HD44780_CHAR_HEIGHT) * (MESH_MAP_ROWS) - 2;
  1027. pixels_per_x_mesh_pnt = x_map_pixels / (GRID_MAX_POINTS_X);
  1028. pixels_per_y_mesh_pnt = y_map_pixels / (GRID_MAX_POINTS_Y);
  1029. if (pixels_per_x_mesh_pnt >= HD44780_CHAR_WIDTH) { // There are only 2 custom characters available, so the X
  1030. pixels_per_x_mesh_pnt = HD44780_CHAR_WIDTH; // Size of the mesh point needs to fit within them independent
  1031. suppress_x_offset = 1; // Of where the starting pixel is located.
  1032. }
  1033. if (pixels_per_y_mesh_pnt >= HD44780_CHAR_HEIGHT) { // There are only 2 custom characters available, so the Y
  1034. pixels_per_y_mesh_pnt = HD44780_CHAR_HEIGHT; // Size of the mesh point needs to fit within them independent
  1035. suppress_y_offset = 1; // Of where the starting pixel is located.
  1036. }
  1037. x_map_pixels = pixels_per_x_mesh_pnt * (GRID_MAX_POINTS_X); // Now we have the right number of pixels to make both
  1038. y_map_pixels = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y); // Directions fit nicely
  1039. right_edge = pixels_per_x_mesh_pnt * (GRID_MAX_POINTS_X) + 1; // Find location of right edge within the character cell
  1040. bottom_line = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y) + 1; // Find location of bottome line within the character cell
  1041. n_rows = bottom_line / (HD44780_CHAR_HEIGHT) + 1;
  1042. n_cols = right_edge / (HD44780_CHAR_WIDTH) + 1;
  1043. for (i = 0; i < n_cols; i++) {
  1044. lcd_put_wchar(i, 0, CHAR_LINE_TOP); // Box Top line
  1045. lcd_put_wchar(i, n_rows - 1, CHAR_LINE_BOT); // Box Bottom line
  1046. }
  1047. for (j = 0; j < n_rows; j++) {
  1048. lcd_put_wchar(0, j, CHAR_EDGE_L); // Box Left edge
  1049. lcd_put_wchar(n_cols - 1, j, CHAR_EDGE_R); // Box Right edge
  1050. }
  1051. /**
  1052. * If the entire 4th row is not in use, do not put vertical bars all the way down to the bottom of the display
  1053. */
  1054. k = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y) + 2;
  1055. l = (HD44780_CHAR_HEIGHT) * n_rows;
  1056. if (l > k && l - k >= (HD44780_CHAR_HEIGHT) / 2) {
  1057. lcd_put_wchar(0, n_rows - 1, ' '); // Box Left edge
  1058. lcd_put_wchar(n_cols - 1, n_rows - 1, ' '); // Box Right edge
  1059. }
  1060. clear_custom_char(&new_char);
  1061. new_char.custom_char_bits[0] = 0b11111U; // Char #0 is used for the box top line
  1062. lcd.createChar(CHAR_LINE_TOP, (uint8_t*)&new_char);
  1063. clear_custom_char(&new_char);
  1064. k = (GRID_MAX_POINTS_Y) * pixels_per_y_mesh_pnt + 1; // Row of pixels for the bottom box line
  1065. l = k % (HD44780_CHAR_HEIGHT); // Row within relevant character cell
  1066. new_char.custom_char_bits[l] = 0b11111U; // Char #1 is used for the box bottom line
  1067. lcd.createChar(CHAR_LINE_BOT, (uint8_t*)&new_char);
  1068. clear_custom_char(&new_char);
  1069. for (j = 0; j < HD44780_CHAR_HEIGHT; j++)
  1070. new_char.custom_char_bits[j] = 0b10000U; // Char #2 is used for the box left edge
  1071. lcd.createChar(CHAR_EDGE_L, (uint8_t*)&new_char);
  1072. clear_custom_char(&new_char);
  1073. m = (GRID_MAX_POINTS_X) * pixels_per_x_mesh_pnt + 1; // Column of pixels for the right box line
  1074. n = m % (HD44780_CHAR_WIDTH); // Column within relevant character cell
  1075. i = HD44780_CHAR_WIDTH - 1 - n; // Column within relevant character cell (0 on the right)
  1076. for (j = 0; j < HD44780_CHAR_HEIGHT; j++)
  1077. new_char.custom_char_bits[j] = (uint8_t)_BV(i); // Char #3 is used for the box right edge
  1078. lcd.createChar(CHAR_EDGE_R, (uint8_t*)&new_char);
  1079. i = x_plot * pixels_per_x_mesh_pnt - suppress_x_offset;
  1080. j = y_plot_inv * pixels_per_y_mesh_pnt - suppress_y_offset;
  1081. upper_left = pixel_location(i, j);
  1082. k = (x_plot + 1) * pixels_per_x_mesh_pnt - 1 - suppress_x_offset;
  1083. l = (y_plot_inv + 1) * pixels_per_y_mesh_pnt - 1 - suppress_y_offset;
  1084. lower_right = pixel_location(k, l);
  1085. bottom_right_corner = pixel_location(x_map_pixels, y_map_pixels);
  1086. /**
  1087. * First, handle the simple case where everything is within a single character cell.
  1088. * If part of the Mesh Plot is outside of this character cell, we will follow up
  1089. * and deal with that next.
  1090. */
  1091. clear_custom_char(&new_char);
  1092. const lcd_uint_t ypix = _MIN(upper_left.y_pixel_offset + pixels_per_y_mesh_pnt, HD44780_CHAR_HEIGHT);
  1093. for (j = upper_left.y_pixel_offset; j < ypix; j++) {
  1094. i = upper_left.x_pixel_mask;
  1095. for (k = 0; k < pixels_per_x_mesh_pnt; k++) {
  1096. new_char.custom_char_bits[j] |= i;
  1097. i >>= 1;
  1098. }
  1099. }
  1100. prep_and_put_map_char(new_char, upper_left, lower_right, bottom_right_corner, TOP_LEFT, CHAR_UL_UL, upper_left.column, upper_left.row);
  1101. /**
  1102. * Next, check for two side by side character cells being used to display the Mesh Point
  1103. * If found... do the right hand character cell next.
  1104. */
  1105. if (upper_left.column == lower_right.column - 1) {
  1106. l = upper_left.x_pixel_offset;
  1107. clear_custom_char(&new_char);
  1108. for (j = upper_left.y_pixel_offset; j < ypix; j++) {
  1109. i = _BV(HD44780_CHAR_WIDTH - 1); // Fill in the left side of the right character cell
  1110. for (k = 0; k < pixels_per_x_mesh_pnt - 1 - l; k++) {
  1111. new_char.custom_char_bits[j] |= i;
  1112. i >>= 1;
  1113. }
  1114. }
  1115. prep_and_put_map_char(new_char, upper_left, lower_right, bottom_right_corner, TOP_RIGHT, CHAR_LR_UL, lower_right.column, upper_left.row);
  1116. }
  1117. /**
  1118. * Next, check for two character cells stacked on top of each other being used to display the Mesh Point
  1119. */
  1120. if (upper_left.row == lower_right.row - 1) {
  1121. l = HD44780_CHAR_HEIGHT - upper_left.y_pixel_offset; // Number of pixel rows in top character cell
  1122. k = pixels_per_y_mesh_pnt - l; // Number of pixel rows in bottom character cell
  1123. clear_custom_char(&new_char);
  1124. for (j = 0; j < k; j++) {
  1125. i = upper_left.x_pixel_mask;
  1126. for (m = 0; m < pixels_per_x_mesh_pnt; m++) { // Fill in the top side of the bottom character cell
  1127. new_char.custom_char_bits[j] |= i;
  1128. if (!(i >>= 1)) break;
  1129. }
  1130. }
  1131. prep_and_put_map_char(new_char, upper_left, lower_right, bottom_right_corner, LOWER_LEFT, CHAR_UL_LR, upper_left.column, lower_right.row);
  1132. }
  1133. /**
  1134. * Next, check for four character cells being used to display the Mesh Point. If that is
  1135. * what is here, we work to fill in the character cell that is down one and to the right one
  1136. * from the upper_left character cell.
  1137. */
  1138. if (upper_left.column == lower_right.column - 1 && upper_left.row == lower_right.row - 1) {
  1139. l = HD44780_CHAR_HEIGHT - upper_left.y_pixel_offset; // Number of pixel rows in top character cell
  1140. k = pixels_per_y_mesh_pnt - l; // Number of pixel rows in bottom character cell
  1141. clear_custom_char(&new_char);
  1142. for (j = 0; j < k; j++) {
  1143. l = upper_left.x_pixel_offset;
  1144. i = _BV(HD44780_CHAR_WIDTH - 1); // Fill in the left side of the right character cell
  1145. for (m = 0; m < pixels_per_x_mesh_pnt - 1 - l; m++) { // Fill in the top side of the bottom character cell
  1146. new_char.custom_char_bits[j] |= i;
  1147. i >>= 1;
  1148. }
  1149. }
  1150. prep_and_put_map_char(new_char, upper_left, lower_right, bottom_right_corner, LOWER_RIGHT, CHAR_LR_LR, lower_right.column, lower_right.row);
  1151. }
  1152. #endif
  1153. /**
  1154. * Print plot position
  1155. */
  1156. lcd_put_wchar(_LCD_W_POS, 0, '(');
  1157. lcd_put_u8str(ui8tostr3rj(x_plot));
  1158. lcd_put_wchar(',');
  1159. lcd_put_u8str(ui8tostr3rj(y_plot));
  1160. lcd_put_wchar(')');
  1161. #if LCD_HEIGHT <= 3 // 16x2 or 20x2 display
  1162. /**
  1163. * Print Z values
  1164. */
  1165. _ZLABEL(_LCD_W_POS, 1);
  1166. if (!isnan(ubl.z_values[x_plot][y_plot]))
  1167. lcd_put_u8str(ftostr43sign(ubl.z_values[x_plot][y_plot]));
  1168. else
  1169. lcd_put_u8str_P(PSTR(" -----"));
  1170. #else // 16x4 or 20x4 display
  1171. /**
  1172. * Show all values at right of screen
  1173. */
  1174. _XLABEL(_LCD_W_POS, 1);
  1175. lcd_put_u8str(ftostr52(LOGICAL_X_POSITION(ubl.mesh_index_to_xpos(x_plot))));
  1176. _YLABEL(_LCD_W_POS, 2);
  1177. lcd_put_u8str(ftostr52(LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos(y_plot))));
  1178. /**
  1179. * Show the location value
  1180. */
  1181. _ZLABEL(_LCD_W_POS, 3);
  1182. if (!isnan(ubl.z_values[x_plot][y_plot]))
  1183. lcd_put_u8str(ftostr43sign(ubl.z_values[x_plot][y_plot]));
  1184. else
  1185. lcd_put_u8str_P(PSTR(" -----"));
  1186. #endif // LCD_HEIGHT > 3
  1187. }
  1188. void add_edges_to_custom_char(custom_char &custom, const coordinate &ul, const coordinate &lr, const coordinate &brc, const uint8_t cell_location) {
  1189. uint8_t i, k;
  1190. int16_t n_rows = lr.row - ul.row + 1,
  1191. n_cols = lr.column - ul.column + 1;
  1192. /**
  1193. * Check if Top line of box needs to be filled in
  1194. */
  1195. if (ul.row == 0 && (cell_location & (TOP_LEFT|TOP_RIGHT))) { // Only fill in the top line for the top character cells
  1196. if (n_cols == 1) {
  1197. if (ul.column != brc.column)
  1198. custom.custom_char_bits[0] = 0xFF; // Single column in middle
  1199. else
  1200. for (i = brc.x_pixel_offset; i < HD44780_CHAR_WIDTH; i++) // Single column on right side
  1201. SBI(custom.custom_char_bits[0], i);
  1202. }
  1203. else if ((cell_location & TOP_LEFT) || lr.column != brc.column) // Multiple column in the middle or with right cell in middle
  1204. custom.custom_char_bits[0] = 0xFF;
  1205. else
  1206. for (i = brc.x_pixel_offset; i < HD44780_CHAR_WIDTH; i++)
  1207. SBI(custom.custom_char_bits[0], i);
  1208. }
  1209. /**
  1210. * Check if left line of box needs to be filled in
  1211. */
  1212. if (cell_location & (TOP_LEFT|LOWER_LEFT)) {
  1213. if (ul.column == 0) { // Left column of characters on LCD Display
  1214. k = ul.row == brc.row ? brc.y_pixel_offset : HD44780_CHAR_HEIGHT; // If it isn't the last row... do the full character cell
  1215. for (i = 0; i < k; i++)
  1216. SBI(custom.custom_char_bits[i], HD44780_CHAR_WIDTH - 1);
  1217. }
  1218. }
  1219. /**
  1220. * Check if bottom line of box needs to be filled in
  1221. */
  1222. // Single row of mesh plot cells
  1223. if (n_rows == 1 /* && (cell_location & (TOP_LEFT|TOP_RIGHT)) */ && ul.row == brc.row) {
  1224. if (n_cols == 1) // Single row, single column case
  1225. k = ul.column == brc.column ? brc.x_pixel_mask : 0x01;
  1226. else if (cell_location & TOP_RIGHT) // Single row, multiple column case
  1227. k = lr.column == brc.column ? brc.x_pixel_mask : 0x01;
  1228. else // Single row, left of multiple columns
  1229. k = 0x01;
  1230. while (k < _BV(HD44780_CHAR_WIDTH)) {
  1231. custom.custom_char_bits[brc.y_pixel_offset] |= k;
  1232. k <<= 1;
  1233. }
  1234. }
  1235. // Double row of characters on LCD Display
  1236. // And this is a bottom custom character
  1237. if (n_rows == 2 && (cell_location & (LOWER_LEFT|LOWER_RIGHT)) && lr.row == brc.row) {
  1238. if (n_cols == 1) // Double row, single column case
  1239. k = ul.column == brc.column ? brc.x_pixel_mask : 0x01;
  1240. else if (cell_location & LOWER_RIGHT) // Double row, multiple column case
  1241. k = lr.column == brc.column ? brc.x_pixel_mask : 0x01;
  1242. else // Double row, left of multiple columns
  1243. k = 0x01;
  1244. while (k < _BV(HD44780_CHAR_WIDTH)) {
  1245. custom.custom_char_bits[brc.y_pixel_offset] |= k;
  1246. k <<= 1;
  1247. }
  1248. }
  1249. /**
  1250. * Check if right line of box needs to be filled in
  1251. */
  1252. // Nothing to do if the lower right part of the mesh pnt isn't in the same column as the box line
  1253. if (lr.column == brc.column) {
  1254. // This mesh point is in the same character cell as the right box line
  1255. if (ul.column == brc.column || (cell_location & (TOP_RIGHT|LOWER_RIGHT))) {
  1256. // If not the last row... do the full character cell
  1257. k = ul.row == brc.row ? brc.y_pixel_offset : HD44780_CHAR_HEIGHT;
  1258. for (i = 0; i < k; i++) custom.custom_char_bits[i] |= brc.x_pixel_mask;
  1259. }
  1260. }
  1261. }
  1262. #endif // AUTO_BED_LEVELING_UBL
  1263. #endif // HAS_LCD_MENU
  1264. #endif // HAS_MARLINUI_HD44780