My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ultralcd_impl_HD44780.h 50KB

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