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.

ultralcd_HD44780.cpp 47KB

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