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

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