ultralcd_impl_HD44780.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef ULTRALCD_IMPL_HD44780_H
  23. #define ULTRALCD_IMPL_HD44780_H
  24. /**
  25. * Implementation of the LCD display routines for a Hitachi HD44780 display.
  26. * These are the most common LCD character displays.
  27. */
  28. #include "utility.h"
  29. #include "duration_t.h"
  30. #if ENABLED(AUTO_BED_LEVELING_UBL)
  31. #include "ubl.h"
  32. #if ENABLED(ULTIPANEL)
  33. #define ULTRA_X_PIXELS_PER_CHAR 5
  34. #define ULTRA_Y_PIXELS_PER_CHAR 8
  35. #define ULTRA_COLUMNS_FOR_MESH_MAP 7
  36. #define ULTRA_ROWS_FOR_MESH_MAP 4
  37. #define N_USER_CHARS 8
  38. #define TOP_LEFT _BV(0)
  39. #define TOP_RIGHT _BV(1)
  40. #define LOWER_LEFT _BV(2)
  41. #define LOWER_RIGHT _BV(3)
  42. #endif
  43. #endif
  44. extern volatile uint8_t buttons; //an extended version of the last checked buttons in a bit array.
  45. ////////////////////////////////////
  46. // Setup button and encode mappings for each panel (into 'buttons' variable
  47. //
  48. // This is just to map common functions (across different panels) onto the same
  49. // macro name. The mapping is independent of whether the button is directly connected or
  50. // via a shift/i2c register.
  51. #if ENABLED(ULTIPANEL)
  52. //
  53. // Setup other button mappings of each panel
  54. //
  55. #if ENABLED(LCD_I2C_VIKI)
  56. #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
  57. // button and encoder bit positions within 'buttons'
  58. #define B_LE (BUTTON_LEFT << B_I2C_BTN_OFFSET) // The remaining normalized buttons are all read via I2C
  59. #define B_UP (BUTTON_UP << B_I2C_BTN_OFFSET)
  60. #define B_MI (BUTTON_SELECT << B_I2C_BTN_OFFSET)
  61. #define B_DW (BUTTON_DOWN << B_I2C_BTN_OFFSET)
  62. #define B_RI (BUTTON_RIGHT << B_I2C_BTN_OFFSET)
  63. #undef LCD_CLICKED
  64. #if BUTTON_EXISTS(ENC)
  65. // the pause/stop/restart button is connected to BTN_ENC when used
  66. #define B_ST (EN_C) // Map the pause/stop/resume button into its normalized functional name
  67. #define LCD_CLICKED (buttons & (B_MI|B_RI|B_ST)) // pause/stop button also acts as click until we implement proper pause/stop.
  68. #else
  69. #define LCD_CLICKED (buttons & (B_MI|B_RI))
  70. #endif
  71. // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
  72. #define LCD_HAS_SLOW_BUTTONS
  73. #elif ENABLED(LCD_I2C_PANELOLU2)
  74. #if !BUTTON_EXISTS(ENC) // Use I2C if not directly connected to a pin
  75. #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
  76. #define B_MI (PANELOLU2_ENCODER_C << B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later
  77. #undef LCD_CLICKED
  78. #define LCD_CLICKED (buttons & B_MI)
  79. // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
  80. #define LCD_HAS_SLOW_BUTTONS
  81. #endif
  82. #elif DISABLED(NEWPANEL) // old style ULTIPANEL
  83. // Shift register bits correspond to buttons:
  84. #define BL_LE 7 // Left
  85. #define BL_UP 6 // Up
  86. #define BL_MI 5 // Middle
  87. #define BL_DW 4 // Down
  88. #define BL_RI 3 // Right
  89. #define BL_ST 2 // Red Button
  90. #define B_LE (_BV(BL_LE))
  91. #define B_UP (_BV(BL_UP))
  92. #define B_MI (_BV(BL_MI))
  93. #define B_DW (_BV(BL_DW))
  94. #define B_RI (_BV(BL_RI))
  95. #define B_ST (_BV(BL_ST))
  96. #define LCD_CLICKED (buttons & (B_MI|B_ST))
  97. #endif
  98. #endif // ULTIPANEL
  99. ////////////////////////////////////
  100. // Create LCD class instance and chipset-specific information
  101. #if ENABLED(LCD_I2C_TYPE_PCF8575)
  102. // note: these are register mapped pins on the PCF8575 controller not Arduino pins
  103. #define LCD_I2C_PIN_BL 3
  104. #define LCD_I2C_PIN_EN 2
  105. #define LCD_I2C_PIN_RW 1
  106. #define LCD_I2C_PIN_RS 0
  107. #define LCD_I2C_PIN_D4 4
  108. #define LCD_I2C_PIN_D5 5
  109. #define LCD_I2C_PIN_D6 6
  110. #define LCD_I2C_PIN_D7 7
  111. #include <Wire.h>
  112. #include <LCD.h>
  113. #include <LiquidCrystal_I2C.h>
  114. #define LCD_CLASS LiquidCrystal_I2C
  115. 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);
  116. #elif ENABLED(LCD_I2C_TYPE_MCP23017)
  117. //for the LED indicators (which maybe mapped to different things in lcd_implementation_update_indicators())
  118. #define LED_A 0x04 //100
  119. #define LED_B 0x02 //010
  120. #define LED_C 0x01 //001
  121. #define LCD_HAS_STATUS_INDICATORS
  122. #include <Wire.h>
  123. #include <LiquidTWI2.h>
  124. #define LCD_CLASS LiquidTWI2
  125. #if ENABLED(DETECT_DEVICE)
  126. LCD_CLASS lcd(LCD_I2C_ADDRESS, 1);
  127. #else
  128. LCD_CLASS lcd(LCD_I2C_ADDRESS);
  129. #endif
  130. #elif ENABLED(LCD_I2C_TYPE_MCP23008)
  131. #include <Wire.h>
  132. #include <LiquidTWI2.h>
  133. #define LCD_CLASS LiquidTWI2
  134. #if ENABLED(DETECT_DEVICE)
  135. LCD_CLASS lcd(LCD_I2C_ADDRESS, 1);
  136. #else
  137. LCD_CLASS lcd(LCD_I2C_ADDRESS);
  138. #endif
  139. #elif ENABLED(LCD_I2C_TYPE_PCA8574)
  140. #include <LiquidCrystal_I2C.h>
  141. #define LCD_CLASS LiquidCrystal_I2C
  142. LCD_CLASS lcd(LCD_I2C_ADDRESS, LCD_WIDTH, LCD_HEIGHT);
  143. // 2 wire Non-latching LCD SR from:
  144. // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/schematics#!shiftregister-connection
  145. #elif ENABLED(SR_LCD_2W_NL)
  146. extern "C" void __cxa_pure_virtual() { while (1); }
  147. #include <LCD.h>
  148. #include <LiquidCrystal_SR.h>
  149. #define LCD_CLASS LiquidCrystal_SR
  150. #if PIN_EXISTS(SR_STROBE)
  151. LCD_CLASS lcd(SR_DATA_PIN, SR_CLK_PIN, SR_STROBE_PIN);
  152. #else
  153. LCD_CLASS lcd(SR_DATA_PIN, SR_CLK_PIN);
  154. #endif
  155. #elif ENABLED(LCM1602)
  156. #include <Wire.h>
  157. #include <LCD.h>
  158. #include <LiquidCrystal_I2C.h>
  159. #define LCD_CLASS LiquidCrystal_I2C
  160. LCD_CLASS lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  161. #else
  162. // Standard directly connected LCD implementations
  163. #include <LiquidCrystal.h>
  164. #define LCD_CLASS LiquidCrystal
  165. LCD_CLASS lcd(LCD_PINS_RS, LCD_PINS_ENABLE, LCD_PINS_D4, LCD_PINS_D5, LCD_PINS_D6, LCD_PINS_D7); //RS,Enable,D4,D5,D6,D7
  166. #endif
  167. #include "utf_mapper.h"
  168. #if ENABLED(LCD_PROGRESS_BAR)
  169. static millis_t progress_bar_ms = 0; // Start millis of the current progress bar cycle
  170. #if PROGRESS_MSG_EXPIRE > 0
  171. static millis_t expire_status_ms = 0; // millis at which to expire the status message
  172. #endif
  173. #define LCD_STR_PROGRESS "\x03\x04\x05"
  174. #endif
  175. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  176. static void lcd_implementation_update_indicators();
  177. #endif
  178. static void createChar_P(const char c, const byte * const ptr) {
  179. byte temp[8];
  180. for (uint8_t i = 0; i < 8; i++)
  181. temp[i] = pgm_read_byte(&ptr[i]);
  182. lcd.createChar(c, temp);
  183. }
  184. #define CHARSET_MENU 0
  185. #define CHARSET_INFO 1
  186. #define CHARSET_BOOT 2
  187. static void lcd_set_custom_characters(
  188. #if ENABLED(LCD_PROGRESS_BAR) || ENABLED(SHOW_BOOTSCREEN)
  189. const uint8_t screen_charset=CHARSET_INFO
  190. #endif
  191. ) {
  192. // CHARSET_BOOT
  193. #if ENABLED(SHOW_BOOTSCREEN)
  194. const static PROGMEM byte corner[4][8] = { {
  195. B00000,
  196. B00000,
  197. B00000,
  198. B00000,
  199. B00001,
  200. B00010,
  201. B00100,
  202. B00100
  203. }, {
  204. B00000,
  205. B00000,
  206. B00000,
  207. B11100,
  208. B11100,
  209. B01100,
  210. B00100,
  211. B00100
  212. }, {
  213. B00100,
  214. B00010,
  215. B00001,
  216. B00000,
  217. B00000,
  218. B00000,
  219. B00000,
  220. B00000
  221. }, {
  222. B00100,
  223. B01000,
  224. B10000,
  225. B00000,
  226. B00000,
  227. B00000,
  228. B00000,
  229. B00000
  230. } };
  231. #endif // SHOW_BOOTSCREEN
  232. // CHARSET_INFO
  233. const static PROGMEM byte bedTemp[8] = {
  234. B00000,
  235. B11111,
  236. B10101,
  237. B10001,
  238. B10101,
  239. B11111,
  240. B00000,
  241. B00000
  242. };
  243. const static PROGMEM byte degree[8] = {
  244. B01100,
  245. B10010,
  246. B10010,
  247. B01100,
  248. B00000,
  249. B00000,
  250. B00000,
  251. B00000
  252. };
  253. const static PROGMEM byte thermometer[8] = {
  254. B00100,
  255. B01010,
  256. B01010,
  257. B01010,
  258. B01010,
  259. B10001,
  260. B10001,
  261. B01110
  262. };
  263. const static PROGMEM byte uplevel[8] = {
  264. B00100,
  265. B01110,
  266. B11111,
  267. B00100,
  268. B11100,
  269. B00000,
  270. B00000,
  271. B00000
  272. };
  273. const static PROGMEM byte feedrate[8] = {
  274. B11100,
  275. B10000,
  276. B11000,
  277. B10111,
  278. B00101,
  279. B00110,
  280. B00101,
  281. B00000
  282. };
  283. const static PROGMEM byte clock[8] = {
  284. B00000,
  285. B01110,
  286. B10011,
  287. B10101,
  288. B10001,
  289. B01110,
  290. B00000,
  291. B00000
  292. };
  293. #if ENABLED(LCD_PROGRESS_BAR)
  294. // CHARSET_INFO
  295. const static PROGMEM byte progress[3][8] = { {
  296. B00000,
  297. B10000,
  298. B10000,
  299. B10000,
  300. B10000,
  301. B10000,
  302. B10000,
  303. B00000
  304. }, {
  305. B00000,
  306. B10100,
  307. B10100,
  308. B10100,
  309. B10100,
  310. B10100,
  311. B10100,
  312. B00000
  313. }, {
  314. B00000,
  315. B10101,
  316. B10101,
  317. B10101,
  318. B10101,
  319. B10101,
  320. B10101,
  321. B00000
  322. } };
  323. #endif // LCD_PROGRESS_BAR
  324. #if ENABLED(SDSUPPORT)
  325. // CHARSET_MENU
  326. const static PROGMEM byte refresh[8] = {
  327. B00000,
  328. B00110,
  329. B11001,
  330. B11000,
  331. B00011,
  332. B10011,
  333. B01100,
  334. B00000,
  335. };
  336. const static PROGMEM byte folder[8] = {
  337. B00000,
  338. B11100,
  339. B11111,
  340. B10001,
  341. B10001,
  342. B11111,
  343. B00000,
  344. B00000
  345. };
  346. #endif // SDSUPPORT
  347. #if ENABLED(SHOW_BOOTSCREEN)
  348. // Set boot screen corner characters
  349. if (screen_charset == CHARSET_BOOT) {
  350. for (uint8_t i = 4; i--;)
  351. createChar_P(i, corner[i]);
  352. }
  353. else
  354. #endif
  355. { // Info Screen uses 5 special characters
  356. createChar_P(LCD_BEDTEMP_CHAR, bedTemp);
  357. createChar_P(LCD_DEGREE_CHAR, degree);
  358. createChar_P(LCD_STR_THERMOMETER[0], thermometer);
  359. createChar_P(LCD_FEEDRATE_CHAR, feedrate);
  360. createChar_P(LCD_CLOCK_CHAR, clock);
  361. #if ENABLED(LCD_PROGRESS_BAR)
  362. if (screen_charset == CHARSET_INFO) { // 3 Progress bar characters for info screen
  363. for (int16_t i = 3; i--;)
  364. createChar_P(LCD_STR_PROGRESS[i], progress[i]);
  365. }
  366. else
  367. #endif
  368. {
  369. createChar_P(LCD_UPLEVEL_CHAR, uplevel);
  370. #if ENABLED(SDSUPPORT)
  371. // SD Card sub-menu special characters
  372. createChar_P(LCD_STR_REFRESH[0], refresh);
  373. createChar_P(LCD_STR_FOLDER[0], folder);
  374. #endif
  375. }
  376. }
  377. }
  378. static void lcd_implementation_init(
  379. #if ENABLED(LCD_PROGRESS_BAR)
  380. const uint8_t screen_charset=CHARSET_INFO
  381. #endif
  382. ) {
  383. #if ENABLED(LCD_I2C_TYPE_PCF8575)
  384. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  385. #ifdef LCD_I2C_PIN_BL
  386. lcd.setBacklightPin(LCD_I2C_PIN_BL, POSITIVE);
  387. lcd.setBacklight(HIGH);
  388. #endif
  389. #elif ENABLED(LCD_I2C_TYPE_MCP23017)
  390. lcd.setMCPType(LTI_TYPE_MCP23017);
  391. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  392. lcd_implementation_update_indicators();
  393. #elif ENABLED(LCD_I2C_TYPE_MCP23008)
  394. lcd.setMCPType(LTI_TYPE_MCP23008);
  395. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  396. #elif ENABLED(LCD_I2C_TYPE_PCA8574)
  397. lcd.init();
  398. lcd.backlight();
  399. #else
  400. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  401. #endif
  402. lcd_set_custom_characters(
  403. #if ENABLED(LCD_PROGRESS_BAR)
  404. screen_charset
  405. #endif
  406. );
  407. lcd.clear();
  408. }
  409. void lcd_implementation_clear() { lcd.clear(); }
  410. void lcd_print(const char c) { charset_mapper(c); }
  411. void lcd_print(const char *str) { while (*str) lcd.print(*str++); }
  412. void lcd_printPGM(const char *str) { while (const char c = pgm_read_byte(str)) lcd.print(c), ++str; }
  413. void lcd_print_utf(const char *str, uint8_t n=LCD_WIDTH) {
  414. char c;
  415. while (n && (c = *str)) n -= charset_mapper(c), ++str;
  416. }
  417. void lcd_printPGM_utf(const char *str, uint8_t n=LCD_WIDTH) {
  418. char c;
  419. while (n && (c = pgm_read_byte(str))) n -= charset_mapper(c), ++str;
  420. }
  421. #if ENABLED(SHOW_BOOTSCREEN)
  422. void lcd_erase_line(const int16_t line) {
  423. lcd.setCursor(0, line);
  424. for (uint8_t i = LCD_WIDTH + 1; --i;)
  425. lcd.write(' ');
  426. }
  427. // Scroll the PSTR 'text' in a 'len' wide field for 'time' milliseconds at position col,line
  428. void lcd_scroll(const int16_t col, const int16_t line, const char* const text, const int16_t len, const int16_t time) {
  429. uint8_t slen = utf8_strlen_P(text);
  430. if (slen < len) {
  431. // Fits into,
  432. lcd.setCursor(col, line);
  433. lcd_printPGM_utf(text, len);
  434. while (slen < len) {
  435. lcd.write(' ');
  436. ++slen;
  437. }
  438. safe_delay(time);
  439. }
  440. else {
  441. const char* p = text;
  442. int dly = time / MAX(slen, 1);
  443. for (uint8_t i = 0; i <= slen; i++) {
  444. // Go to the correct place
  445. lcd.setCursor(col, line);
  446. // Print the text
  447. lcd_printPGM_utf(p, len);
  448. // Fill with spaces
  449. uint8_t ix = slen - i;
  450. while (ix < len) {
  451. lcd.write(' ');
  452. ++ix;
  453. }
  454. // Delay
  455. safe_delay(dly);
  456. // Advance to the next UTF8 valid position
  457. p++;
  458. while (!START_OF_UTF8_CHAR(pgm_read_byte(p))) p++;
  459. }
  460. }
  461. }
  462. static void logo_lines(const char* const extra) {
  463. int16_t indent = (LCD_WIDTH - 8 - utf8_strlen_P(extra)) / 2;
  464. lcd.setCursor(indent, 0); lcd.print('\x00'); lcd_printPGM(PSTR( "------" )); lcd.write('\x01');
  465. lcd.setCursor(indent, 1); lcd_printPGM(PSTR("|Marlin|")); lcd_printPGM(extra);
  466. lcd.setCursor(indent, 2); lcd.write('\x02'); lcd_printPGM(PSTR( "------" )); lcd.write('\x03');
  467. }
  468. void lcd_bootscreen() {
  469. lcd_set_custom_characters(CHARSET_BOOT);
  470. lcd.clear();
  471. #define LCD_EXTRA_SPACE (LCD_WIDTH-8)
  472. #define CENTER_OR_SCROLL(STRING,DELAY) \
  473. lcd_erase_line(3); \
  474. if (strlen(STRING) <= LCD_WIDTH) { \
  475. lcd.setCursor((LCD_WIDTH - utf8_strlen_P(PSTR(STRING))) / 2, 3); \
  476. lcd_printPGM_utf(PSTR(STRING)); \
  477. safe_delay(DELAY); \
  478. } \
  479. else { \
  480. lcd_scroll(0, 3, PSTR(STRING), LCD_WIDTH, DELAY); \
  481. }
  482. #ifdef STRING_SPLASH_LINE1
  483. //
  484. // Show the Marlin logo with splash line 1
  485. //
  486. if (LCD_EXTRA_SPACE >= strlen(STRING_SPLASH_LINE1) + 1) {
  487. //
  488. // Show the Marlin logo, splash line1, and splash line 2
  489. //
  490. logo_lines(PSTR(" " STRING_SPLASH_LINE1));
  491. #ifdef STRING_SPLASH_LINE2
  492. CENTER_OR_SCROLL(STRING_SPLASH_LINE2, 2000);
  493. #else
  494. safe_delay(2000);
  495. #endif
  496. }
  497. else {
  498. //
  499. // Show the Marlin logo with splash line 1
  500. // After a delay show splash line 2, if it exists
  501. //
  502. #ifdef STRING_SPLASH_LINE2
  503. #define _SPLASH_WAIT_1 1500
  504. #else
  505. #define _SPLASH_WAIT_1 2000
  506. #endif
  507. logo_lines(PSTR(""));
  508. CENTER_OR_SCROLL(STRING_SPLASH_LINE1, _SPLASH_WAIT_1);
  509. #ifdef STRING_SPLASH_LINE2
  510. CENTER_OR_SCROLL(STRING_SPLASH_LINE2, 1500);
  511. #endif
  512. }
  513. #elif defined(STRING_SPLASH_LINE2)
  514. //
  515. // Show splash line 2 only, alongside the logo if possible
  516. //
  517. if (LCD_EXTRA_SPACE >= strlen(STRING_SPLASH_LINE2) + 1) {
  518. logo_lines(PSTR(" " STRING_SPLASH_LINE2));
  519. safe_delay(2000);
  520. }
  521. else {
  522. logo_lines(PSTR(""));
  523. CENTER_OR_SCROLL(STRING_SPLASH_LINE2, 2000);
  524. }
  525. #else
  526. //
  527. // Show only the Marlin logo
  528. //
  529. logo_lines(PSTR(""));
  530. safe_delay(2000);
  531. #endif
  532. lcd.clear();
  533. safe_delay(100);
  534. lcd_set_custom_characters();
  535. lcd.clear();
  536. }
  537. #endif // SHOW_BOOTSCREEN
  538. void lcd_kill_screen() {
  539. lcd.setCursor(0, 0);
  540. lcd_print_utf(lcd_status_message);
  541. #if LCD_HEIGHT < 4
  542. lcd.setCursor(0, 2);
  543. #else
  544. lcd.setCursor(0, 2);
  545. lcd_printPGM_utf(PSTR(MSG_HALTED));
  546. lcd.setCursor(0, 3);
  547. #endif
  548. lcd_printPGM_utf(PSTR(MSG_PLEASE_RESET));
  549. }
  550. //
  551. // Before homing, blink '123' <-> '???'.
  552. // Homed but unknown... '123' <-> ' '.
  553. // Homed and known, display constantly.
  554. //
  555. FORCE_INLINE void _draw_axis_value(const AxisEnum axis, const char *value, const bool blink) {
  556. lcd_print('X' + uint8_t(axis));
  557. if (blink)
  558. lcd.print(value);
  559. else {
  560. if (!TEST(axis_homed, axis))
  561. while (const char c = *value++) lcd_print(c <= '.' ? c : '?');
  562. else {
  563. #if DISABLED(HOME_AFTER_DEACTIVATE) && DISABLED(DISABLE_REDUCED_ACCURACY_WARNING)
  564. if (!TEST(axis_known_position, axis))
  565. lcd_printPGM(axis == Z_AXIS ? PSTR(" ") : PSTR(" "));
  566. else
  567. #endif
  568. lcd.print(value);
  569. }
  570. }
  571. }
  572. FORCE_INLINE void _draw_heater_status(const int8_t heater, const char prefix, const bool blink) {
  573. #if HAS_HEATED_BED
  574. const bool isBed = heater < 0;
  575. const float t1 = (isBed ? thermalManager.degBed() : thermalManager.degHotend(heater)),
  576. t2 = (isBed ? thermalManager.degTargetBed() : thermalManager.degTargetHotend(heater));
  577. #else
  578. const float t1 = thermalManager.degHotend(heater), t2 = thermalManager.degTargetHotend(heater);
  579. #endif
  580. if (prefix >= 0) lcd.print(prefix);
  581. lcd.print(itostr3(t1 + 0.5));
  582. lcd.write('/');
  583. #if !HEATER_IDLE_HANDLER
  584. UNUSED(blink);
  585. #else
  586. const bool is_idle = (
  587. #if HAS_HEATED_BED
  588. isBed ? thermalManager.is_bed_idle() :
  589. #endif
  590. thermalManager.is_heater_idle(heater)
  591. );
  592. if (!blink && is_idle) {
  593. lcd.write(' ');
  594. if (t2 >= 10) lcd.write(' ');
  595. if (t2 >= 100) lcd.write(' ');
  596. }
  597. else
  598. #endif
  599. lcd.print(itostr3left(t2 + 0.5));
  600. if (prefix >= 0) {
  601. lcd.print((char)LCD_DEGREE_CHAR);
  602. lcd.write(' ');
  603. if (t2 < 10) lcd.write(' ');
  604. }
  605. }
  606. #if ENABLED(LCD_PROGRESS_BAR)
  607. inline void lcd_draw_progress_bar(const uint8_t percent) {
  608. const int16_t tix = (int16_t)(percent * (LCD_WIDTH) * 3) / 100,
  609. cel = tix / 3,
  610. rem = tix % 3;
  611. uint8_t i = LCD_WIDTH;
  612. char msg[LCD_WIDTH + 1], b = ' ';
  613. msg[LCD_WIDTH] = '\0';
  614. while (i--) {
  615. if (i == cel - 1)
  616. b = LCD_STR_PROGRESS[2];
  617. else if (i == cel && rem != 0)
  618. b = LCD_STR_PROGRESS[rem - 1];
  619. msg[i] = b;
  620. }
  621. lcd.print(msg);
  622. }
  623. #endif // LCD_PROGRESS_BAR
  624. /**
  625. Possible status screens:
  626. 16x2 |000/000 B000/000|
  627. |0123456789012345|
  628. 16x4 |000/000 B000/000|
  629. |SD100% Z 000.00|
  630. |F100% T--:--|
  631. |0123456789012345|
  632. 20x2 |T000/000D B000/000D |
  633. |01234567890123456789|
  634. 20x4 |T000/000D B000/000D |
  635. |X 000 Y 000 Z 000.00|
  636. |F100% SD100% T--:--|
  637. |01234567890123456789|
  638. 20x4 |T000/000D B000/000D |
  639. |T000/000D Z 000.00|
  640. |F100% SD100% T--:--|
  641. |01234567890123456789|
  642. */
  643. static void lcd_implementation_status_screen() {
  644. const bool blink = lcd_blink();
  645. //
  646. // Line 1
  647. //
  648. lcd.setCursor(0, 0);
  649. #if LCD_WIDTH < 20
  650. //
  651. // Hotend 0 Temperature
  652. //
  653. _draw_heater_status(0, -1, blink);
  654. //
  655. // Hotend 1 or Bed Temperature
  656. //
  657. #if HOTENDS > 1 || HAS_HEATED_BED
  658. lcd.setCursor(8, 0);
  659. #if HOTENDS > 1
  660. lcd.print((char)LCD_STR_THERMOMETER[0]);
  661. _draw_heater_status(1, -1, blink);
  662. #else
  663. lcd.print((char)LCD_BEDTEMP_CHAR);
  664. _draw_heater_status(-1, -1, blink);
  665. #endif
  666. #endif // HOTENDS > 1 || HAS_HEATED_BED
  667. #else // LCD_WIDTH >= 20
  668. //
  669. // Hotend 0 Temperature
  670. //
  671. _draw_heater_status(0, LCD_STR_THERMOMETER[0], blink);
  672. //
  673. // Hotend 1 or Bed Temperature
  674. //
  675. #if HOTENDS > 1 || HAS_HEATED_BED
  676. lcd.setCursor(10, 0);
  677. #if HOTENDS > 1
  678. _draw_heater_status(1, LCD_STR_THERMOMETER[0], blink);
  679. #else
  680. _draw_heater_status(-1, (
  681. #if HAS_LEVELING
  682. planner.leveling_active && blink ? '_' :
  683. #endif
  684. LCD_BEDTEMP_CHAR
  685. ), blink);
  686. #endif
  687. #endif // HOTENDS > 1 || HAS_HEATED_BED
  688. #endif // LCD_WIDTH >= 20
  689. //
  690. // Line 2
  691. //
  692. #if LCD_HEIGHT > 2
  693. #if LCD_WIDTH < 20
  694. #if ENABLED(SDSUPPORT)
  695. lcd.setCursor(0, 2);
  696. lcd_printPGM(PSTR("SD"));
  697. if (IS_SD_PRINTING())
  698. lcd.print(itostr3(card.percentDone()));
  699. else
  700. lcd_printPGM(PSTR("---"));
  701. lcd.write('%');
  702. #endif // SDSUPPORT
  703. #else // LCD_WIDTH >= 20
  704. lcd.setCursor(0, 1);
  705. // If the first line has two extruder temps,
  706. // show more temperatures on the next line
  707. #if HOTENDS > 2 || (HOTENDS > 1 && HAS_HEATED_BED)
  708. #if HOTENDS > 2
  709. _draw_heater_status(2, LCD_STR_THERMOMETER[0], blink);
  710. lcd.setCursor(10, 1);
  711. #endif
  712. _draw_heater_status(-1, (
  713. #if HAS_LEVELING
  714. planner.leveling_active && blink ? '_' :
  715. #endif
  716. LCD_BEDTEMP_CHAR
  717. ), blink);
  718. #else // HOTENDS <= 2 && (HOTENDS <= 1 || !HAS_HEATED_BED)
  719. _draw_axis_value(X_AXIS, ftostr4sign(LOGICAL_X_POSITION(current_position[X_AXIS])), blink);
  720. lcd.write(' ');
  721. _draw_axis_value(Y_AXIS, ftostr4sign(LOGICAL_Y_POSITION(current_position[Y_AXIS])), blink);
  722. #endif // HOTENDS <= 2 && (HOTENDS <= 1 || !HAS_HEATED_BED)
  723. #endif // LCD_WIDTH >= 20
  724. lcd.setCursor(LCD_WIDTH - 8, 1);
  725. _draw_axis_value(Z_AXIS, ftostr52sp(LOGICAL_Z_POSITION(current_position[Z_AXIS])), blink);
  726. #if HAS_LEVELING && !HAS_HEATED_BED
  727. lcd.write(planner.leveling_active || blink ? '_' : ' ');
  728. #endif
  729. #endif // LCD_HEIGHT > 2
  730. //
  731. // Line 3
  732. //
  733. #if LCD_HEIGHT > 3
  734. lcd.setCursor(0, 2);
  735. lcd.print((char)LCD_FEEDRATE_CHAR);
  736. lcd.print(itostr3(feedrate_percentage));
  737. lcd.write('%');
  738. #if LCD_WIDTH >= 20 && ENABLED(SDSUPPORT)
  739. lcd.setCursor(7, 2);
  740. lcd_printPGM(PSTR("SD"));
  741. if (IS_SD_PRINTING())
  742. lcd.print(itostr3(card.percentDone()));
  743. else
  744. lcd_printPGM(PSTR("---"));
  745. lcd.write('%');
  746. #endif // LCD_WIDTH >= 20 && SDSUPPORT
  747. char buffer[10];
  748. duration_t elapsed = print_job_timer.duration();
  749. uint8_t len = elapsed.toDigital(buffer);
  750. lcd.setCursor(LCD_WIDTH - len - 1, 2);
  751. lcd.print((char)LCD_CLOCK_CHAR);
  752. lcd_print(buffer);
  753. #endif // LCD_HEIGHT > 3
  754. //
  755. // Last Line
  756. // Status Message (which may be a Progress Bar or Filament display)
  757. //
  758. lcd.setCursor(0, LCD_HEIGHT - 1);
  759. #if ENABLED(LCD_PROGRESS_BAR)
  760. // Draw the progress bar if the message has shown long enough
  761. // or if there is no message set.
  762. #if DISABLED(LCD_SET_PROGRESS_MANUALLY)
  763. const uint8_t progress_bar_percent = card.percentDone();
  764. #endif
  765. if (progress_bar_percent > 2 && (ELAPSED(millis(), progress_bar_ms + PROGRESS_BAR_MSG_TIME) || !lcd_status_message[0]))
  766. return lcd_draw_progress_bar(progress_bar_percent);
  767. #elif ENABLED(FILAMENT_LCD_DISPLAY) && ENABLED(SDSUPPORT)
  768. // Show Filament Diameter and Volumetric Multiplier %
  769. // After allowing lcd_status_message to show for 5 seconds
  770. if (ELAPSED(millis(), previous_lcd_status_ms + 5000UL)) {
  771. lcd_printPGM(PSTR("Dia "));
  772. lcd.print(ftostr12ns(filament_width_meas));
  773. lcd_printPGM(PSTR(" V"));
  774. lcd.print(itostr3(100.0 * (
  775. parser.volumetric_enabled
  776. ? planner.volumetric_area_nominal / planner.volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]
  777. : planner.volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]
  778. )
  779. ));
  780. lcd.write('%');
  781. return;
  782. }
  783. #endif // FILAMENT_LCD_DISPLAY && SDSUPPORT
  784. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  785. static bool last_blink = false;
  786. // Get the UTF8 character count of the string
  787. uint8_t slen = utf8_strlen(lcd_status_message);
  788. // If the string fits into the LCD, just print it and do not scroll it
  789. if (slen <= LCD_WIDTH) {
  790. // The string isn't scrolling and may not fill the screen
  791. lcd_print_utf(lcd_status_message);
  792. // Fill the rest with spaces
  793. while (slen < LCD_WIDTH) {
  794. lcd.write(' ');
  795. ++slen;
  796. }
  797. }
  798. else {
  799. // String is larger than the available space in screen.
  800. // Get a pointer to the next valid UTF8 character
  801. const char *stat = lcd_status_message + status_scroll_offset;
  802. // Get the string remaining length
  803. const uint8_t rlen = utf8_strlen(stat);
  804. // If we have enough characters to display
  805. if (rlen >= LCD_WIDTH) {
  806. // The remaining string fills the screen - Print it
  807. lcd_print_utf(stat, LCD_WIDTH);
  808. }
  809. else {
  810. // The remaining string does not completely fill the screen
  811. lcd_print_utf(stat, LCD_WIDTH); // The string leaves space
  812. uint8_t chars = LCD_WIDTH - rlen; // Amount of space left in characters
  813. lcd.write('.'); // Always at 1+ spaces left, draw a dot
  814. if (--chars) { // Draw a second dot if there's space
  815. lcd.write('.');
  816. if (--chars)
  817. lcd_print_utf(lcd_status_message, chars); // Print a second copy of the message
  818. }
  819. }
  820. if (last_blink != blink) {
  821. last_blink = blink;
  822. // Adjust by complete UTF8 characters
  823. if (status_scroll_offset < slen) {
  824. status_scroll_offset++;
  825. while (!START_OF_UTF8_CHAR(lcd_status_message[status_scroll_offset]))
  826. status_scroll_offset++;
  827. }
  828. else
  829. status_scroll_offset = 0;
  830. }
  831. }
  832. #else
  833. UNUSED(blink);
  834. // Get the UTF8 character count of the string
  835. uint8_t slen = utf8_strlen(lcd_status_message);
  836. // Just print the string to the LCD
  837. lcd_print_utf(lcd_status_message, LCD_WIDTH);
  838. // Fill the rest with spaces if there are missing spaces
  839. while (slen < LCD_WIDTH) {
  840. lcd.write(' ');
  841. ++slen;
  842. }
  843. #endif
  844. }
  845. #if ENABLED(ULTIPANEL)
  846. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  847. static void lcd_implementation_hotend_status(const uint8_t row, const uint8_t extruder=active_extruder) {
  848. if (row < LCD_HEIGHT) {
  849. lcd.setCursor(LCD_WIDTH - 9, row);
  850. _draw_heater_status(extruder, LCD_STR_THERMOMETER[0], lcd_blink());
  851. }
  852. }
  853. #endif // ADVANCED_PAUSE_FEATURE
  854. static void lcd_implementation_drawmenu_static(const uint8_t row, const char* pstr, const bool center=true, const bool invert=false, const char *valstr=NULL) {
  855. UNUSED(invert);
  856. char c;
  857. int8_t n = LCD_WIDTH;
  858. lcd.setCursor(0, row);
  859. if (center && !valstr) {
  860. int8_t pad = (LCD_WIDTH - utf8_strlen_P(pstr)) / 2;
  861. while (--pad >= 0) { lcd.write(' '); n--; }
  862. }
  863. while (n > 0 && (c = pgm_read_byte(pstr))) {
  864. n -= charset_mapper(c);
  865. pstr++;
  866. }
  867. if (valstr) while (n > 0 && (c = *valstr)) {
  868. n -= charset_mapper(c);
  869. valstr++;
  870. }
  871. while (n-- > 0) lcd.write(' ');
  872. }
  873. static void lcd_implementation_drawmenu_generic(const bool sel, const uint8_t row, const char* pstr, const char pre_char, const char post_char) {
  874. char c;
  875. uint8_t n = LCD_WIDTH - 2;
  876. lcd.setCursor(0, row);
  877. lcd.print(sel ? pre_char : ' ');
  878. while ((c = pgm_read_byte(pstr)) && n > 0) {
  879. n -= charset_mapper(c);
  880. pstr++;
  881. }
  882. while (n--) lcd.write(' ');
  883. lcd.print(post_char);
  884. }
  885. static void lcd_implementation_drawmenu_setting_edit_generic(const bool sel, const uint8_t row, const char* pstr, const char pre_char, const char* const data) {
  886. char c;
  887. uint8_t n = LCD_WIDTH - 2 - utf8_strlen(data);
  888. lcd.setCursor(0, row);
  889. lcd.print(sel ? pre_char : ' ');
  890. while ((c = pgm_read_byte(pstr)) && n > 0) {
  891. n -= charset_mapper(c);
  892. pstr++;
  893. }
  894. lcd.write(':');
  895. while (n--) lcd.write(' ');
  896. lcd_print(data);
  897. }
  898. static void lcd_implementation_drawmenu_setting_edit_generic_P(const bool sel, const uint8_t row, const char* pstr, const char pre_char, const char* const data) {
  899. char c;
  900. uint8_t n = LCD_WIDTH - 2 - utf8_strlen_P(data);
  901. lcd.setCursor(0, row);
  902. lcd.print(sel ? pre_char : ' ');
  903. while ((c = pgm_read_byte(pstr)) && n > 0) {
  904. n -= charset_mapper(c);
  905. pstr++;
  906. }
  907. lcd.write(':');
  908. while (n--) lcd.write(' ');
  909. lcd_printPGM(data);
  910. }
  911. #define DRAWMENU_SETTING_EDIT_GENERIC(_src) lcd_implementation_drawmenu_setting_edit_generic(sel, row, pstr, '>', _src)
  912. #define DRAW_BOOL_SETTING(sel, row, pstr, data) lcd_implementation_drawmenu_setting_edit_generic_P(sel, row, pstr, '>', (*(data))?PSTR(MSG_ON):PSTR(MSG_OFF))
  913. void lcd_implementation_drawedit(const char* pstr, const char* const value=NULL) {
  914. lcd.setCursor(1, 1);
  915. lcd_printPGM_utf(pstr);
  916. if (value != NULL) {
  917. lcd.write(':');
  918. const uint8_t valrow = (utf8_strlen_P(pstr) + 1 + utf8_strlen(value) + 1) > (LCD_WIDTH - 2) ? 2 : 1; // Value on the next row if it won't fit
  919. lcd.setCursor((LCD_WIDTH - 1) - (utf8_strlen(value) + 1), valrow); // Right-justified, padded by spaces
  920. lcd.write(' '); // overwrite char if value gets shorter
  921. lcd_print(value);
  922. }
  923. }
  924. #if ENABLED(SDSUPPORT)
  925. static void lcd_implementation_drawmenu_sd(const bool sel, const uint8_t row, const char* const pstr, CardReader& theCard, const uint8_t concat, const char post_char) {
  926. UNUSED(pstr);
  927. lcd.setCursor(0, row);
  928. lcd.print(sel ? '>' : ' ');
  929. uint8_t n = LCD_WIDTH - concat;
  930. const char *outstr = theCard.longest_filename();
  931. if (theCard.longFilename[0]) {
  932. #if ENABLED(SCROLL_LONG_FILENAMES)
  933. if (sel) {
  934. uint8_t name_hash = row;
  935. for (uint8_t l = FILENAME_LENGTH; l--;)
  936. name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ theCard.filename[l]; // rotate, xor
  937. if (filename_scroll_hash != name_hash) { // If the hash changed...
  938. filename_scroll_hash = name_hash; // Save the new hash
  939. filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - n); // Update the scroll limit
  940. filename_scroll_pos = 0; // Reset scroll to the start
  941. lcd_status_update_delay = 8; // Don't scroll right away
  942. }
  943. outstr += filename_scroll_pos;
  944. }
  945. #else
  946. theCard.longFilename[n] = '\0'; // cutoff at screen edge
  947. #endif
  948. }
  949. char c;
  950. while (n && (c = *outstr)) {
  951. n -= charset_mapper(c);
  952. ++outstr;
  953. }
  954. while (n) { --n; lcd.write(' '); }
  955. lcd.print(post_char);
  956. }
  957. static void lcd_implementation_drawmenu_sdfile(const bool sel, const uint8_t row, const char* pstr, CardReader& theCard) {
  958. lcd_implementation_drawmenu_sd(sel, row, pstr, theCard, 2, ' ');
  959. }
  960. static void lcd_implementation_drawmenu_sddirectory(const bool sel, const uint8_t row, const char* pstr, CardReader& theCard) {
  961. lcd_implementation_drawmenu_sd(sel, row, pstr, theCard, 2, LCD_STR_FOLDER[0]);
  962. }
  963. #endif // SDSUPPORT
  964. #define lcd_implementation_drawmenu_back(sel, row, pstr, dummy) lcd_implementation_drawmenu_generic(sel, row, pstr, LCD_UPLEVEL_CHAR, LCD_UPLEVEL_CHAR)
  965. #define lcd_implementation_drawmenu_submenu(sel, row, pstr, data) lcd_implementation_drawmenu_generic(sel, row, pstr, '>', LCD_STR_ARROW_RIGHT[0])
  966. #define lcd_implementation_drawmenu_gcode(sel, row, pstr, gcode) lcd_implementation_drawmenu_generic(sel, row, pstr, '>', ' ')
  967. #define lcd_implementation_drawmenu_function(sel, row, pstr, data) lcd_implementation_drawmenu_generic(sel, row, pstr, '>', ' ')
  968. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  969. extern millis_t next_button_update_ms;
  970. static uint8_t lcd_implementation_read_slow_buttons() {
  971. #if ENABLED(LCD_I2C_TYPE_MCP23017)
  972. // Reading these buttons this is likely to be too slow to call inside interrupt context
  973. // so they are called during normal lcd_update
  974. uint8_t slow_bits = lcd.readButtons() << B_I2C_BTN_OFFSET;
  975. #if ENABLED(LCD_I2C_VIKI)
  976. if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked
  977. slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated
  978. #endif // LCD_I2C_VIKI
  979. return slow_bits;
  980. #endif // LCD_I2C_TYPE_MCP23017
  981. }
  982. #endif // LCD_HAS_SLOW_BUTTONS
  983. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  984. static void lcd_implementation_update_indicators() {
  985. // Set the LEDS - referred to as backlights by the LiquidTWI2 library
  986. static uint8_t ledsprev = 0;
  987. uint8_t leds = 0;
  988. #if HAS_HEATED_BED
  989. if (thermalManager.degTargetBed() > 0) leds |= LED_A;
  990. #endif
  991. if (thermalManager.degTargetHotend(0) > 0) leds |= LED_B;
  992. #if FAN_COUNT > 0
  993. if (0
  994. #if HAS_FAN0
  995. || fanSpeeds[0]
  996. #endif
  997. #if HAS_FAN1
  998. || fanSpeeds[1]
  999. #endif
  1000. #if HAS_FAN2
  1001. || fanSpeeds[2]
  1002. #endif
  1003. ) leds |= LED_C;
  1004. #endif // FAN_COUNT > 0
  1005. #if HOTENDS > 1
  1006. if (thermalManager.degTargetHotend(1) > 0) leds |= LED_C;
  1007. #endif
  1008. if (leds != ledsprev) {
  1009. lcd.setBacklight(leds);
  1010. ledsprev = leds;
  1011. }
  1012. }
  1013. #endif // LCD_HAS_STATUS_INDICATORS
  1014. #if ENABLED(AUTO_BED_LEVELING_UBL)
  1015. /**
  1016. Possible map screens:
  1017. 16x2 |X000.00 Y000.00|
  1018. |(00,00) Z00.000|
  1019. 20x2 | X:000.00 Y:000.00 |
  1020. | (00,00) Z:00.000 |
  1021. 16x4 |+-------+(00,00)|
  1022. || |X000.00|
  1023. || |Y000.00|
  1024. |+-------+Z00.000|
  1025. 20x4 | +-------+ (00,00) |
  1026. | | | X:000.00|
  1027. | | | Y:000.00|
  1028. | +-------+ Z:00.000|
  1029. */
  1030. typedef struct {
  1031. uint8_t custom_char_bits[ULTRA_Y_PIXELS_PER_CHAR];
  1032. } custom_char;
  1033. typedef struct {
  1034. uint8_t column, row;
  1035. uint8_t y_pixel_offset, x_pixel_offset;
  1036. uint8_t x_pixel_mask;
  1037. } coordinate;
  1038. void add_edges_to_custom_char(custom_char * const custom, coordinate * const ul, coordinate * const lr, coordinate * const brc, const uint8_t cell_location);
  1039. FORCE_INLINE static void clear_custom_char(custom_char * const cc) { ZERO(cc->custom_char_bits); }
  1040. /*
  1041. // This debug routine should be deleted by anybody that sees it. It doesn't belong here
  1042. // But I'm leaving it for now until we know the 20x4 Radar Map is working right.
  1043. // We may need it again if any funny lines show up on the mesh points.
  1044. void dump_custom_char(char *title, custom_char *c) {
  1045. SERIAL_PROTOCOLLN(title);
  1046. for (uint8_t j = 0; j < 8; j++) {
  1047. for (uint8_t i = 7; i >= 0; i--)
  1048. SERIAL_PROTOCOLCHAR(TEST(c->custom_char_bits[j], i) ? '1' : '0');
  1049. SERIAL_EOL();
  1050. }
  1051. SERIAL_EOL();
  1052. }
  1053. //*/
  1054. coordinate pixel_location(int16_t x, int16_t y) {
  1055. coordinate ret_val;
  1056. int16_t xp, yp, r, c;
  1057. x++; y++; // +1 because lines on the left and top
  1058. c = x / (ULTRA_X_PIXELS_PER_CHAR);
  1059. r = y / (ULTRA_Y_PIXELS_PER_CHAR);
  1060. ret_val.column = c;
  1061. ret_val.row = r;
  1062. xp = x - c * (ULTRA_X_PIXELS_PER_CHAR); // get the pixel offsets into the character cell
  1063. xp = ULTRA_X_PIXELS_PER_CHAR - 1 - xp; // column within relevant character cell (0 on the right)
  1064. yp = y - r * (ULTRA_Y_PIXELS_PER_CHAR);
  1065. ret_val.x_pixel_mask = _BV(xp);
  1066. ret_val.x_pixel_offset = xp;
  1067. ret_val.y_pixel_offset = yp;
  1068. return ret_val;
  1069. }
  1070. inline coordinate pixel_location(const uint8_t x, const uint8_t y) { return pixel_location((int16_t)x, (int16_t)y); }
  1071. void lcd_implementation_ubl_plot(const uint8_t x, const uint8_t inverted_y) {
  1072. #if LCD_WIDTH >= 20
  1073. #define _LCD_W_POS 12
  1074. #define _PLOT_X 1
  1075. #define _MAP_X 3
  1076. #define _LABEL(C,X,Y) lcd.setCursor(X, Y); lcd.print(C)
  1077. #define _XLABEL(X,Y) _LABEL("X:",X,Y)
  1078. #define _YLABEL(X,Y) _LABEL("Y:",X,Y)
  1079. #define _ZLABEL(X,Y) _LABEL("Z:",X,Y)
  1080. #else
  1081. #define _LCD_W_POS 8
  1082. #define _PLOT_X 0
  1083. #define _MAP_X 1
  1084. #define _LABEL(X,Y,C) lcd.setCursor(X, Y); lcd.write(C)
  1085. #define _XLABEL(X,Y) _LABEL('X',X,Y)
  1086. #define _YLABEL(X,Y) _LABEL('Y',X,Y)
  1087. #define _ZLABEL(X,Y) _LABEL('Z',X,Y)
  1088. #endif
  1089. #if LCD_HEIGHT <= 3 // 16x2 or 20x2 display
  1090. /**
  1091. * Show X and Y positions
  1092. */
  1093. _XLABEL(_PLOT_X, 0);
  1094. lcd.print(ftostr52(LOGICAL_X_POSITION(pgm_read_float(&ubl._mesh_index_to_xpos[x]))));
  1095. _YLABEL(_LCD_W_POS, 0);
  1096. lcd.print(ftostr52(LOGICAL_Y_POSITION(pgm_read_float(&ubl._mesh_index_to_ypos[inverted_y]))));
  1097. lcd.setCursor(_PLOT_X, 0);
  1098. #else // 16x4 or 20x4 display
  1099. coordinate upper_left, lower_right, bottom_right_corner;
  1100. custom_char new_char;
  1101. uint8_t i, j, k, l, m, n, n_rows, n_cols, y,
  1102. bottom_line, right_edge,
  1103. x_map_pixels, y_map_pixels,
  1104. pixels_per_x_mesh_pnt, pixels_per_y_mesh_pnt,
  1105. suppress_x_offset = 0, suppress_y_offset = 0;
  1106. y = GRID_MAX_POINTS_Y - inverted_y - 1;
  1107. upper_left.column = 0;
  1108. upper_left.row = 0;
  1109. lower_right.column = 0;
  1110. lower_right.row = 0;
  1111. lcd_implementation_clear();
  1112. x_map_pixels = (ULTRA_X_PIXELS_PER_CHAR) * (ULTRA_COLUMNS_FOR_MESH_MAP) - 2; // minus 2 because we are drawing a box around the map
  1113. y_map_pixels = (ULTRA_Y_PIXELS_PER_CHAR) * (ULTRA_ROWS_FOR_MESH_MAP) - 2;
  1114. pixels_per_x_mesh_pnt = x_map_pixels / (GRID_MAX_POINTS_X);
  1115. pixels_per_y_mesh_pnt = y_map_pixels / (GRID_MAX_POINTS_Y);
  1116. if (pixels_per_x_mesh_pnt >= ULTRA_X_PIXELS_PER_CHAR) { // There are only 2 custom characters available, so the X
  1117. pixels_per_x_mesh_pnt = ULTRA_X_PIXELS_PER_CHAR; // size of the mesh point needs to fit within them independent
  1118. suppress_x_offset = 1; // of where the starting pixel is located.
  1119. }
  1120. if (pixels_per_y_mesh_pnt >= ULTRA_Y_PIXELS_PER_CHAR) { // There are only 2 custom characters available, so the Y
  1121. pixels_per_y_mesh_pnt = ULTRA_Y_PIXELS_PER_CHAR; // size of the mesh point needs to fit within them independent
  1122. suppress_y_offset = 1; // of where the starting pixel is located.
  1123. }
  1124. x_map_pixels = pixels_per_x_mesh_pnt * (GRID_MAX_POINTS_X); // now we have the right number of pixels to make both
  1125. y_map_pixels = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y); // directions fit nicely
  1126. right_edge = pixels_per_x_mesh_pnt * (GRID_MAX_POINTS_X) + 1; // find location of right edge within the character cell
  1127. bottom_line= pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y) + 1; // find location of bottome line within the character cell
  1128. n_rows = bottom_line / (ULTRA_Y_PIXELS_PER_CHAR) + 1;
  1129. n_cols = right_edge / (ULTRA_X_PIXELS_PER_CHAR) + 1;
  1130. for (i = 0; i < n_cols; i++) {
  1131. lcd.setCursor(i, 0);
  1132. lcd.print((char)0x00); // top line of the box
  1133. lcd.setCursor(i, n_rows - 1);
  1134. lcd.write(0x01); // bottom line of the box
  1135. }
  1136. for (j = 0; j < n_rows; j++) {
  1137. lcd.setCursor(0, j);
  1138. lcd.write(0x02); // Left edge of the box
  1139. lcd.setCursor(n_cols - 1, j);
  1140. lcd.write(0x03); // right edge of the box
  1141. }
  1142. /**
  1143. * If the entire 4th row is not in use, do not put vertical bars all the way down to the bottom of the display
  1144. */
  1145. k = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y) + 2;
  1146. l = (ULTRA_Y_PIXELS_PER_CHAR) * n_rows;
  1147. if (l > k && l - k >= (ULTRA_Y_PIXELS_PER_CHAR) / 2) {
  1148. lcd.setCursor(0, n_rows - 1); // left edge of the box
  1149. lcd.write(' ');
  1150. lcd.setCursor(n_cols - 1, n_rows - 1); // right edge of the box
  1151. lcd.write(' ');
  1152. }
  1153. clear_custom_char(&new_char);
  1154. new_char.custom_char_bits[0] = 0b11111U; // char #0 is used for the top line of the box
  1155. lcd.createChar(0, (uint8_t*)&new_char);
  1156. clear_custom_char(&new_char);
  1157. k = (GRID_MAX_POINTS_Y) * pixels_per_y_mesh_pnt + 1; // row of pixels for the bottom box line
  1158. l = k % (ULTRA_Y_PIXELS_PER_CHAR); // row within relevant character cell
  1159. new_char.custom_char_bits[l] = 0b11111U; // char #1 is used for the bottom line of the box
  1160. lcd.createChar(1, (uint8_t*)&new_char);
  1161. clear_custom_char(&new_char);
  1162. for (j = 0; j < ULTRA_Y_PIXELS_PER_CHAR; j++)
  1163. new_char.custom_char_bits[j] = 0b10000U; // char #2 is used for the left edge of the box
  1164. lcd.createChar(2, (uint8_t*)&new_char);
  1165. clear_custom_char(&new_char);
  1166. m = (GRID_MAX_POINTS_X) * pixels_per_x_mesh_pnt + 1; // Column of pixels for the right box line
  1167. n = m % (ULTRA_X_PIXELS_PER_CHAR); // Column within relevant character cell
  1168. i = ULTRA_X_PIXELS_PER_CHAR - 1 - n; // Column within relevant character cell (0 on the right)
  1169. for (j = 0; j < ULTRA_Y_PIXELS_PER_CHAR; j++)
  1170. new_char.custom_char_bits[j] = (uint8_t)_BV(i); // Char #3 is used for the right edge of the box
  1171. lcd.createChar(3, (uint8_t*)&new_char);
  1172. i = x * pixels_per_x_mesh_pnt - suppress_x_offset;
  1173. j = y * pixels_per_y_mesh_pnt - suppress_y_offset;
  1174. upper_left = pixel_location(i, j);
  1175. k = (x + 1) * pixels_per_x_mesh_pnt - 1 - suppress_x_offset;
  1176. l = (y + 1) * pixels_per_y_mesh_pnt - 1 - suppress_y_offset;
  1177. lower_right = pixel_location(k, l);
  1178. bottom_right_corner = pixel_location(x_map_pixels, y_map_pixels);
  1179. /**
  1180. * First, handle the simple case where everything is within a single character cell.
  1181. * If part of the Mesh Plot is outside of this character cell, we will follow up
  1182. * and deal with that next.
  1183. */
  1184. //dump_custom_char("at entry:", &new_char);
  1185. clear_custom_char(&new_char);
  1186. const uint8_t ypix = MIN(upper_left.y_pixel_offset + pixels_per_y_mesh_pnt, ULTRA_Y_PIXELS_PER_CHAR);
  1187. for (j = upper_left.y_pixel_offset; j < ypix; j++) {
  1188. i = upper_left.x_pixel_mask;
  1189. for (k = 0; k < pixels_per_x_mesh_pnt; k++) {
  1190. new_char.custom_char_bits[j] |= i;
  1191. i >>= 1;
  1192. }
  1193. }
  1194. //dump_custom_char("after loops:", &new_char);
  1195. add_edges_to_custom_char(&new_char, &upper_left, &lower_right, &bottom_right_corner, TOP_LEFT);
  1196. //dump_custom_char("after add edges", &new_char);
  1197. lcd.createChar(4, (uint8_t*)&new_char);
  1198. lcd.setCursor(upper_left.column, upper_left.row);
  1199. lcd.write(0x04);
  1200. //dump_custom_char("after lcd update:", &new_char);
  1201. /**
  1202. * Next, check for two side by side character cells being used to display the Mesh Point
  1203. * If found... do the right hand character cell next.
  1204. */
  1205. if (upper_left.column == lower_right.column - 1) {
  1206. l = upper_left.x_pixel_offset;
  1207. clear_custom_char(&new_char);
  1208. for (j = upper_left.y_pixel_offset; j < ypix; j++) {
  1209. i = _BV(ULTRA_X_PIXELS_PER_CHAR - 1); // Fill in the left side of the right character cell
  1210. for (k = 0; k < pixels_per_x_mesh_pnt - 1 - l; k++) {
  1211. new_char.custom_char_bits[j] |= i;
  1212. i >>= 1;
  1213. }
  1214. }
  1215. add_edges_to_custom_char(&new_char, &upper_left, &lower_right, &bottom_right_corner, TOP_RIGHT);
  1216. lcd.createChar(5, (uint8_t *) &new_char);
  1217. lcd.setCursor(lower_right.column, upper_left.row);
  1218. lcd.write(0x05);
  1219. }
  1220. /**
  1221. * Next, check for two character cells stacked on top of each other being used to display the Mesh Point
  1222. */
  1223. if (upper_left.row == lower_right.row - 1) {
  1224. l = ULTRA_Y_PIXELS_PER_CHAR - upper_left.y_pixel_offset; // Number of pixel rows in top character cell
  1225. k = pixels_per_y_mesh_pnt - l; // Number of pixel rows in bottom character cell
  1226. clear_custom_char(&new_char);
  1227. for (j = 0; j < k; j++) {
  1228. i = upper_left.x_pixel_mask;
  1229. for (m = 0; m < pixels_per_x_mesh_pnt; m++) { // Fill in the top side of the bottom character cell
  1230. new_char.custom_char_bits[j] |= i;
  1231. if (!(i >>= 1)) break;
  1232. }
  1233. }
  1234. add_edges_to_custom_char(&new_char, &upper_left, &lower_right, &bottom_right_corner, LOWER_LEFT);
  1235. lcd.createChar(6, (uint8_t *) &new_char);
  1236. lcd.setCursor(upper_left.column, lower_right.row);
  1237. lcd.write(0x06);
  1238. }
  1239. /**
  1240. * Next, check for four character cells being used to display the Mesh Point. If that is
  1241. * what is here, we work to fill in the character cell that is down one and to the right one
  1242. * from the upper_left character cell.
  1243. */
  1244. if (upper_left.column == lower_right.column - 1 && upper_left.row == lower_right.row - 1) {
  1245. l = ULTRA_Y_PIXELS_PER_CHAR - upper_left.y_pixel_offset; // Number of pixel rows in top character cell
  1246. k = pixels_per_y_mesh_pnt - l; // Number of pixel rows in bottom character cell
  1247. clear_custom_char(&new_char);
  1248. for (j = 0; j < k; j++) {
  1249. l = upper_left.x_pixel_offset;
  1250. i = _BV(ULTRA_X_PIXELS_PER_CHAR - 1); // Fill in the left side of the right character cell
  1251. for (m = 0; m < pixels_per_x_mesh_pnt - 1 - l; m++) { // Fill in the top side of the bottom character cell
  1252. new_char.custom_char_bits[j] |= i;
  1253. i >>= 1;
  1254. }
  1255. }
  1256. add_edges_to_custom_char(&new_char, &upper_left, &lower_right, &bottom_right_corner, LOWER_RIGHT);
  1257. lcd.createChar(7, (uint8_t*)&new_char);
  1258. lcd.setCursor(lower_right.column, lower_right.row);
  1259. lcd.write(0x07);
  1260. }
  1261. #endif
  1262. /**
  1263. * Print plot position
  1264. */
  1265. lcd.setCursor(_LCD_W_POS, 0);
  1266. lcd.write('(');
  1267. lcd.print(x);
  1268. lcd.write(',');
  1269. lcd.print(inverted_y);
  1270. lcd.write(')');
  1271. #if LCD_HEIGHT <= 3 // 16x2 or 20x2 display
  1272. /**
  1273. * Print Z values
  1274. */
  1275. _ZLABEL(_LCD_W_POS, 1);
  1276. if (!isnan(ubl.z_values[x][inverted_y]))
  1277. lcd.print(ftostr43sign(ubl.z_values[x][inverted_y]));
  1278. else
  1279. lcd_printPGM(PSTR(" -----"));
  1280. #else // 16x4 or 20x4 display
  1281. /**
  1282. * Show all values at right of screen
  1283. */
  1284. _XLABEL(_LCD_W_POS, 1);
  1285. lcd.print(ftostr52(LOGICAL_X_POSITION(pgm_read_float(&ubl._mesh_index_to_xpos[x]))));
  1286. _YLABEL(_LCD_W_POS, 2);
  1287. lcd.print(ftostr52(LOGICAL_Y_POSITION(pgm_read_float(&ubl._mesh_index_to_ypos[inverted_y]))));
  1288. /**
  1289. * Show the location value
  1290. */
  1291. _ZLABEL(_LCD_W_POS, 3);
  1292. if (!isnan(ubl.z_values[x][inverted_y]))
  1293. lcd.print(ftostr43sign(ubl.z_values[x][inverted_y]));
  1294. else
  1295. lcd_printPGM(PSTR(" -----"));
  1296. #endif // LCD_HEIGHT > 3
  1297. }
  1298. void add_edges_to_custom_char(custom_char * const custom, coordinate * const ul, coordinate * const lr, coordinate * const brc, uint8_t cell_location) {
  1299. uint8_t i, k;
  1300. int16_t n_rows = lr->row - ul->row + 1,
  1301. n_cols = lr->column - ul->column + 1;
  1302. /**
  1303. * Check if Top line of box needs to be filled in
  1304. */
  1305. if (ul->row == 0 && ((cell_location & TOP_LEFT) || (cell_location & TOP_RIGHT))) { // Only fill in the top line for the top character cells
  1306. if (n_cols == 1) {
  1307. if (ul->column != brc->column)
  1308. custom->custom_char_bits[0] = 0xFF; // Single column in middle
  1309. else
  1310. for (i = brc->x_pixel_offset; i < ULTRA_X_PIXELS_PER_CHAR; i++) // Single column on right side
  1311. SBI(custom->custom_char_bits[0], i);
  1312. }
  1313. else if ((cell_location & TOP_LEFT) || lr->column != brc->column) // Multiple column in the middle or with right cell in middle
  1314. custom->custom_char_bits[0] = 0xFF;
  1315. else
  1316. for (i = brc->x_pixel_offset; i < ULTRA_X_PIXELS_PER_CHAR; i++)
  1317. SBI(custom->custom_char_bits[0], i);
  1318. }
  1319. /**
  1320. * Check if left line of box needs to be filled in
  1321. */
  1322. if ((cell_location & TOP_LEFT) || (cell_location & LOWER_LEFT)) {
  1323. if (ul->column == 0) { // Left column of characters on LCD Display
  1324. k = ul->row == brc->row ? brc->y_pixel_offset : ULTRA_Y_PIXELS_PER_CHAR; // If it isn't the last row... do the full character cell
  1325. for (i = 0; i < k; i++)
  1326. SBI(custom->custom_char_bits[i], ULTRA_X_PIXELS_PER_CHAR - 1);
  1327. }
  1328. }
  1329. /**
  1330. * Check if bottom line of box needs to be filled in
  1331. */
  1332. // Single row of mesh plot cells
  1333. if (n_rows == 1 /* && (cell_location == TOP_LEFT || cell_location == TOP_RIGHT) */ && ul->row == brc->row) {
  1334. if (n_cols == 1) // Single row, single column case
  1335. k = ul->column == brc->column ? brc->x_pixel_mask : 0x01;
  1336. else if (cell_location & TOP_RIGHT) // Single row, multiple column case
  1337. k = lr->column == brc->column ? brc->x_pixel_mask : 0x01;
  1338. else // Single row, left of multiple columns
  1339. k = 0x01;
  1340. while (k < _BV(ULTRA_X_PIXELS_PER_CHAR)) {
  1341. custom->custom_char_bits[brc->y_pixel_offset] |= k;
  1342. k <<= 1;
  1343. }
  1344. }
  1345. // Double row of characters on LCD Display
  1346. // And this is a bottom custom character
  1347. if (n_rows == 2 && (cell_location == LOWER_LEFT || cell_location == LOWER_RIGHT) && lr->row == brc->row) {
  1348. if (n_cols == 1) // Double row, single column case
  1349. k = ul->column == brc->column ? brc->x_pixel_mask : 0x01;
  1350. else if (cell_location & LOWER_RIGHT) // Double row, multiple column case
  1351. k = lr->column == brc->column ? brc->x_pixel_mask : 0x01;
  1352. else // Double row, left of multiple columns
  1353. k = 0x01;
  1354. while (k < _BV(ULTRA_X_PIXELS_PER_CHAR)) {
  1355. custom->custom_char_bits[brc->y_pixel_offset] |= k;
  1356. k <<= 1;
  1357. }
  1358. }
  1359. /**
  1360. * Check if right line of box needs to be filled in
  1361. */
  1362. // Nothing to do if the lower right part of the mesh pnt isn't in the same column as the box line
  1363. if (lr->column == brc->column) {
  1364. // This mesh point is in the same character cell as the right box line
  1365. if (ul->column == brc->column || (cell_location & TOP_RIGHT) || (cell_location & LOWER_RIGHT)) {
  1366. // If not the last row... do the full character cell
  1367. k = ul->row == brc->row ? brc->y_pixel_offset : ULTRA_Y_PIXELS_PER_CHAR;
  1368. for (i = 0; i < k; i++) custom->custom_char_bits[i] |= brc->x_pixel_mask;
  1369. }
  1370. }
  1371. }
  1372. #endif // AUTO_BED_LEVELING_UBL
  1373. #endif // ULTIPANEL
  1374. #endif // ULTRALCD_IMPL_HD44780_H