My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

status_screen_lite_ST7920.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /**
  2. * Lightweight Status Screen for the RepRapDiscount Full
  3. * Graphics Smart Controller (ST7920-based 128x64 LCD)
  4. *
  5. * (c) 2017 Aleph Objects, Inc.
  6. *
  7. * The code in this page is free software: you can
  8. * redistribute it and/or modify it under the terms of the GNU
  9. * General Public License (GNU GPL) as published by the Free Software
  10. * Foundation, either version 3 of the License, or (at your option)
  11. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  14. */
  15. /**
  16. * Implementation of a Status Screen for the RepRapDiscount
  17. * Full Graphics Smart Controller using native ST7920 commands
  18. * instead of U8Glib.
  19. *
  20. * This alternative Status Screen makes use of the built-in character
  21. * generation capabilities of the ST7920 to update the Status Screen
  22. * with less SPI traffic and CPU use. In particular:
  23. *
  24. * - The fan and bed animations are handled using custom characters
  25. * that are stored in CGRAM. This allows for the animation to be
  26. * updated by writing a single character to the text-buffer (DDRAM).
  27. *
  28. * - All the information in the Status Screen is text that is written
  29. * to DDRAM, so the work of generating the bitmaps is offloaded to
  30. * the ST7920 rather than being render by U8Glib on the MCU.
  31. *
  32. * - The graphics buffer (GDRAM) is only used for static graphics
  33. * elements (nozzle and feedrate bitmaps) and for the progress
  34. * bar, so updates are sporadic.
  35. */
  36. //
  37. // status_screen_lite_ST7920.cpp
  38. // Lightweight Status Screen for Graphical Display
  39. //
  40. #include "../../inc/MarlinConfigPre.h"
  41. #if ENABLED(LIGHTWEIGHT_UI)
  42. #include "status_screen_lite_ST7920.h"
  43. #include "../marlinui.h"
  44. #include "../fontutils.h"
  45. #include "../lcdprint.h"
  46. #include "../../libs/duration_t.h"
  47. #include "../../module/motion.h"
  48. #include "../../module/printcounter.h"
  49. #include "../../module/temperature.h"
  50. #if ENABLED(SDSUPPORT)
  51. #include "../../sd/cardreader.h"
  52. #endif
  53. #if ENABLED(LCD_SHOW_E_TOTAL)
  54. #include "../../MarlinCore.h" // for printingIsActive
  55. #endif
  56. #define TEXT_MODE_LCD_WIDTH 16
  57. #define BUFFER_WIDTH 256
  58. #define BUFFER_HEIGHT 32
  59. #define DDRAM_LINE_1 0x00
  60. #define DDRAM_LINE_2 0x10
  61. #define DDRAM_LINE_3 0x08
  62. #define DDRAM_LINE_4 0x18
  63. ST7920_Lite_Status_Screen::st7920_state_t ST7920_Lite_Status_Screen::current_bits;
  64. void ST7920_Lite_Status_Screen::cmd(const uint8_t cmd) {
  65. if (!current_bits.synced || !current_bits.cmd) {
  66. current_bits.synced = true;
  67. current_bits.cmd = true;
  68. sync_cmd();
  69. }
  70. write_byte(cmd);
  71. }
  72. void ST7920_Lite_Status_Screen::begin_data() {
  73. if (!current_bits.synced || current_bits.cmd) {
  74. current_bits.synced = true;
  75. current_bits.cmd = false;
  76. sync_dat();
  77. }
  78. }
  79. void ST7920_Lite_Status_Screen::write_str(const char *str) {
  80. while (*str) write_byte(*str++);
  81. }
  82. void ST7920_Lite_Status_Screen::write_str(const char *str, uint8_t len) {
  83. while (*str && len--) write_byte(*str++);
  84. }
  85. void ST7920_Lite_Status_Screen::write_str_P(PGM_P const str) {
  86. PGM_P p_str = (PGM_P)str;
  87. while (char c = pgm_read_byte(p_str++)) write_byte(c);
  88. }
  89. void ST7920_Lite_Status_Screen::write_number(const int16_t value, const uint8_t digits/*=3*/) {
  90. char str[7];
  91. PGM_P fmt;
  92. switch (digits) {
  93. case 6: fmt = PSTR("%6d"); break;
  94. case 5: fmt = PSTR("%5d"); break;
  95. case 4: fmt = PSTR("%4d"); break;
  96. case 3: fmt = PSTR("%3d"); break;
  97. case 2: fmt = PSTR("%2d"); break;
  98. case 1: fmt = PSTR("%1d"); break;
  99. default: return;
  100. }
  101. sprintf_P(str, fmt, value);
  102. write_str(str);
  103. }
  104. void ST7920_Lite_Status_Screen::display_status(const bool display_on, const bool cursor_on, const bool blink_on) {
  105. extended_function_set(false);
  106. cmd(0b00001000 |
  107. (display_on ? 0b0100 : 0) |
  108. (cursor_on ? 0b0010 : 0) |
  109. (blink_on ? 0b0001 : 0)
  110. );
  111. }
  112. // Sets the extended and graphics bits simultaneously, regardless of
  113. // the current state. This is a helper function for extended_function_set()
  114. // and graphics()
  115. void ST7920_Lite_Status_Screen::_extended_function_set(const bool extended, const bool graphics) {
  116. cmd( 0b00100000 |
  117. (extended ? 0b00000100 : 0) |
  118. (graphics ? 0b00000010 : 0)
  119. );
  120. current_bits.extended = extended;
  121. current_bits.graphics = graphics;
  122. }
  123. void ST7920_Lite_Status_Screen::extended_function_set(const bool extended) {
  124. if (extended != current_bits.extended)
  125. _extended_function_set(extended, current_bits.graphics);
  126. }
  127. void ST7920_Lite_Status_Screen::graphics(const bool graphics) {
  128. if (graphics != current_bits.graphics)
  129. _extended_function_set(current_bits.extended, graphics);
  130. }
  131. void ST7920_Lite_Status_Screen::entry_mode_select(const bool ac_increase, const bool shift) {
  132. extended_function_set(false);
  133. cmd(0b00000100 |
  134. (ac_increase ? 0b00000010 : 0) |
  135. (shift ? 0b00000001 : 0)
  136. );
  137. }
  138. // Sets the sa bit regardless of the current state. This is a helper
  139. // function for scroll_or_addr_select()
  140. void ST7920_Lite_Status_Screen::_scroll_or_addr_select(const bool sa) {
  141. extended_function_set(true);
  142. cmd(0b00000010 | (sa ? 0b00000001 : 0));
  143. current_bits.sa = sa;
  144. }
  145. void ST7920_Lite_Status_Screen::scroll_or_addr_select(const bool sa) {
  146. if (sa != current_bits.sa)
  147. _scroll_or_addr_select(sa);
  148. }
  149. void ST7920_Lite_Status_Screen::set_ddram_address(const uint8_t addr) {
  150. extended_function_set(false);
  151. cmd(0b10000000 | (addr & 0b00111111));
  152. }
  153. void ST7920_Lite_Status_Screen::set_cgram_address(const uint8_t addr) {
  154. extended_function_set(false);
  155. cmd(0b01000000 | (addr & 0b00111111));
  156. }
  157. void ST7920_Lite_Status_Screen::set_gdram_address(const uint8_t x, const uint8_t y) {
  158. extended_function_set(true);
  159. cmd(0b10000000 | (y & 0b01111111));
  160. cmd(0b10000000 | (x & 0b00001111));
  161. }
  162. void ST7920_Lite_Status_Screen::clear() {
  163. extended_function_set(false);
  164. cmd(0x00000001);
  165. delay(15); //delay for CGRAM clear
  166. }
  167. void ST7920_Lite_Status_Screen::home() {
  168. extended_function_set(false);
  169. cmd(0x00000010);
  170. }
  171. /* This fills the entire text buffer with spaces */
  172. void ST7920_Lite_Status_Screen::clear_ddram() {
  173. set_ddram_address(DDRAM_LINE_1);
  174. begin_data();
  175. for (uint8_t i = 64; i--;) write_byte(' ');
  176. }
  177. /* This fills the entire graphics buffer with zeros */
  178. void ST7920_Lite_Status_Screen::clear_gdram() {
  179. LOOP_L_N(y, BUFFER_HEIGHT) {
  180. set_gdram_address(0, y);
  181. begin_data();
  182. for (uint8_t i = (BUFFER_WIDTH) / 16; i--;) write_word(0);
  183. }
  184. }
  185. void ST7920_Lite_Status_Screen::load_cgram_icon(const uint16_t addr, const void *data) {
  186. const uint16_t *p_word = (const uint16_t *)data;
  187. set_cgram_address(addr);
  188. begin_data();
  189. for (uint8_t i = 16; i--;)
  190. write_word(pgm_read_word(p_word++));
  191. }
  192. /**
  193. * Draw an icon in GDRAM. Position specified in DDRAM
  194. * coordinates. i.e., X from 1 to 8, Y from 1 to 4.
  195. */
  196. void ST7920_Lite_Status_Screen::draw_gdram_icon(uint8_t x, uint8_t y, const void *data) {
  197. const uint16_t *p_word = (const uint16_t *)data;
  198. // Handle display folding
  199. if (y > 1) y -= 2, x += 8;
  200. for (int i = 0; i < 16; i++) {
  201. set_gdram_address(x, i + y * 16);
  202. begin_data();
  203. write_word(pgm_read_word(p_word++));
  204. }
  205. }
  206. /************************** ICON DEFINITIONS *************************************/
  207. #define CGRAM_ICON_1_ADDR 0x00
  208. #define CGRAM_ICON_2_ADDR 0x10
  209. #define CGRAM_ICON_3_ADDR 0x20
  210. #define CGRAM_ICON_4_ADDR 0x30
  211. #define CGRAM_ICON_1_WORD 0x00
  212. #define CGRAM_ICON_2_WORD 0x02
  213. #define CGRAM_ICON_3_WORD 0x04
  214. #define CGRAM_ICON_4_WORD 0x06
  215. const uint8_t degree_symbol_y_top = 1;
  216. PROGMEM const uint8_t degree_symbol[] = {
  217. 0b00110000,
  218. 0b01001000,
  219. 0b01001000,
  220. 0b00110000,
  221. };
  222. const uint16_t nozzle_icon[] PROGMEM = {
  223. 0b0000000000000000,
  224. 0b0000000000000000,
  225. 0b0000111111110000,
  226. 0b0001111111111000,
  227. 0b0001111111111000,
  228. 0b0001111111111000,
  229. 0b0000111111110000,
  230. 0b0000111111110000,
  231. 0b0001111111111000,
  232. 0b0001111111111000,
  233. 0b0001111111111000,
  234. 0b0000011111100000,
  235. 0b0000001111000000,
  236. 0b0000000110000000,
  237. 0b0000000000000000,
  238. 0b0000000000000000
  239. };
  240. const uint16_t bed_icon[] PROGMEM = {
  241. 0b0000000000000000,
  242. 0b0000000000000000,
  243. 0b0000000000000000,
  244. 0b0000000000000000,
  245. 0b0000000000000000,
  246. 0b0000000000000000,
  247. 0b0000000000000000,
  248. 0b0000000000000000,
  249. 0b0000000000000000,
  250. 0b0000000000000000,
  251. 0b0000000000000000,
  252. 0b0111111111111110,
  253. 0b0111111111111110,
  254. 0b0110000000000110,
  255. 0b0000000000000000,
  256. 0b0000000000000000
  257. };
  258. const uint16_t heat1_icon[] PROGMEM = {
  259. 0b0000000000000000,
  260. 0b0010001000100000,
  261. 0b0001000100010000,
  262. 0b0000100010001000,
  263. 0b0000100010001000,
  264. 0b0001000100010000,
  265. 0b0010001000100000,
  266. 0b0010001000100000,
  267. 0b0001000100010000,
  268. 0b0000100010001000,
  269. 0b0000000000000000,
  270. 0b0000000000000000,
  271. 0b0000000000000000,
  272. 0b0000000000000000,
  273. 0b0000000000000000,
  274. 0b0000000000000000
  275. };
  276. const uint16_t heat2_icon[] PROGMEM = {
  277. 0b0000000000000000,
  278. 0b0000100010001000,
  279. 0b0000100010001000,
  280. 0b0001000100010000,
  281. 0b0010001000100000,
  282. 0b0010001000100000,
  283. 0b0001000100010000,
  284. 0b0000100010001000,
  285. 0b0000100010001000,
  286. 0b0001000100010000,
  287. 0b0000000000000000,
  288. 0b0000000000000000,
  289. 0b0000000000000000,
  290. 0b0000000000000000,
  291. 0b0000000000000000,
  292. 0b0000000000000000
  293. };
  294. const uint16_t fan1_icon[] PROGMEM = {
  295. 0b0000000000000000,
  296. 0b0111111111111110,
  297. 0b0111000000001110,
  298. 0b0110001111000110,
  299. 0b0100001111000010,
  300. 0b0100000110000010,
  301. 0b0101100000011010,
  302. 0b0101110110111010,
  303. 0b0101100000011010,
  304. 0b0100000110000010,
  305. 0b0100001111000010,
  306. 0b0110001111000110,
  307. 0b0111000000001110,
  308. 0b0111111111111110,
  309. 0b0000000000000000,
  310. 0b0000000000000000
  311. };
  312. const uint16_t fan2_icon[] PROGMEM = {
  313. 0b0000000000000000,
  314. 0b0111111111111110,
  315. 0b0111000000001110,
  316. 0b0110010000100110,
  317. 0b0100111001110010,
  318. 0b0101111001111010,
  319. 0b0100110000110010,
  320. 0b0100000110000010,
  321. 0b0100110000110010,
  322. 0b0101111001111010,
  323. 0b0100111001110010,
  324. 0b0110010000100110,
  325. 0b0111000000001110,
  326. 0b0111111111111110,
  327. 0b0000000000000000,
  328. 0b0000000000000000
  329. };
  330. const uint16_t feedrate_icon[] PROGMEM = {
  331. 0b0000000000000000,
  332. 0b0111111000000000,
  333. 0b0110000000000000,
  334. 0b0110000000000000,
  335. 0b0110000000000000,
  336. 0b0111111011111000,
  337. 0b0110000011001100,
  338. 0b0110000011001100,
  339. 0b0110000011001100,
  340. 0b0110000011111000,
  341. 0b0000000011001100,
  342. 0b0000000011001100,
  343. 0b0000000011001100,
  344. 0b0000000011001100,
  345. 0b0000000000000000,
  346. 0b0000000000000000
  347. };
  348. /************************** MAIN SCREEN *************************************/
  349. /**
  350. * The ST7920 has no degree character, so draw it to GDRAM.
  351. * This function takes character position xy
  352. * i.e., x is [0-15], while the y position is [0-3]
  353. */
  354. void ST7920_Lite_Status_Screen::draw_degree_symbol(uint8_t x, uint8_t y, const bool draw) {
  355. const uint8_t *p_bytes = degree_symbol;
  356. // Handle display folding
  357. if (y > 1) y -= 2, x += 16;
  358. const bool oddChar = x & 1;
  359. const uint8_t x_word = x >> 1,
  360. y_top = degree_symbol_y_top,
  361. y_bot = y_top + COUNT(degree_symbol);
  362. LOOP_S_L_N(i, y_top, y_bot) {
  363. uint8_t byte = pgm_read_byte(p_bytes++);
  364. set_gdram_address(x_word, i + y * 16);
  365. begin_data();
  366. if (draw) {
  367. write_byte(oddChar ? 0x00 : byte);
  368. write_byte(oddChar ? byte : 0x00);
  369. }
  370. else
  371. write_word(0x0000);
  372. }
  373. }
  374. void ST7920_Lite_Status_Screen::draw_static_elements() {
  375. scroll_or_addr_select(0);
  376. // Load the animated bed and fan icons
  377. load_cgram_icon(CGRAM_ICON_1_ADDR, heat1_icon);
  378. load_cgram_icon(CGRAM_ICON_2_ADDR, heat2_icon);
  379. load_cgram_icon(CGRAM_ICON_3_ADDR, fan1_icon);
  380. load_cgram_icon(CGRAM_ICON_4_ADDR, fan2_icon);
  381. // Draw the static icons in GDRAM
  382. draw_gdram_icon(0, 0, nozzle_icon);
  383. #if HAS_MULTI_HOTEND
  384. draw_gdram_icon(0, 1, nozzle_icon);
  385. draw_gdram_icon(0, 2, bed_icon);
  386. #else
  387. draw_gdram_icon(0, 1, bed_icon);
  388. #endif
  389. draw_gdram_icon(5, 1, feedrate_icon);
  390. // Draw the initial fan icon
  391. draw_fan_icon(false);
  392. }
  393. /**
  394. * Although this is undocumented, the ST7920 allows the character
  395. * data buffer (DDRAM) to be used in conjunction with the graphics
  396. * bitmap buffer (CGRAM). The contents of the graphics buffer is
  397. * XORed with the data from the character generator. This allows
  398. * us to make the progess bar out of graphical data (the bar) and
  399. * text data (the percentage).
  400. */
  401. void ST7920_Lite_Status_Screen::draw_progress_bar(const uint8_t value) {
  402. #if HOTENDS == 1
  403. // If we have only one extruder, draw a long progress bar on the third line
  404. constexpr uint8_t top = 1, // Top in pixels
  405. bottom = 13, // Bottom in pixels
  406. left = 12, // Left edge, in 16-bit words
  407. width = 4; // Width of progress bar, in 16-bit words
  408. #else
  409. constexpr uint8_t top = 16 + 1,
  410. bottom = 16 + 13,
  411. left = 5,
  412. width = 3;
  413. #endif
  414. const uint8_t char_pcnt = 100 / width; // How many percent does each 16-bit word represent?
  415. // Draw the progress bar as a bitmap in CGRAM
  416. LOOP_S_LE_N(y, top, bottom) {
  417. set_gdram_address(left, y);
  418. begin_data();
  419. LOOP_L_N(x, width) {
  420. uint16_t gfx_word = 0x0000;
  421. if ((x + 1) * char_pcnt <= value)
  422. gfx_word = 0xFFFF; // Draw completely filled bytes
  423. else if ((x * char_pcnt) < value)
  424. gfx_word = int(0x8000) >> (value % char_pcnt) * 16 / char_pcnt; // Draw partially filled bytes
  425. // Draw the frame around the progress bar
  426. if (y == top || y == bottom)
  427. gfx_word = 0xFFFF; // Draw top/bottom border
  428. else if (x == width - 1)
  429. gfx_word |= 0x0001; // Draw right border
  430. else if (x == 0)
  431. gfx_word |= 0x8000; // Draw left border
  432. write_word(gfx_word);
  433. }
  434. }
  435. // Draw the percentage as text in DDRAM
  436. #if HOTENDS == 1
  437. set_ddram_address(DDRAM_LINE_3 + 4);
  438. begin_data();
  439. write_byte(' ');
  440. #else
  441. set_ddram_address(DDRAM_LINE_2 + left);
  442. begin_data();
  443. #endif
  444. // Draw centered
  445. if (value > 9) {
  446. write_number(value, 4);
  447. write_str_P(PSTR("% "));
  448. }
  449. else {
  450. write_number(value, 3);
  451. write_str_P(PSTR("% "));
  452. }
  453. }
  454. void ST7920_Lite_Status_Screen::draw_fan_icon(const bool whichIcon) {
  455. set_ddram_address(DDRAM_LINE_1 + 5);
  456. begin_data();
  457. write_word(whichIcon ? CGRAM_ICON_3_WORD : CGRAM_ICON_4_WORD);
  458. }
  459. void ST7920_Lite_Status_Screen::draw_heat_icon(const bool whichIcon, const bool heating) {
  460. set_ddram_address(
  461. #if HOTENDS == 1
  462. DDRAM_LINE_2
  463. #else
  464. DDRAM_LINE_3
  465. #endif
  466. );
  467. begin_data();
  468. if (heating)
  469. write_word(whichIcon ? CGRAM_ICON_1_WORD : CGRAM_ICON_2_WORD);
  470. else {
  471. write_byte(' ');
  472. write_byte(' ');
  473. }
  474. }
  475. #define FAR(a,b) (((a > b) ? (a-b) : (b-a)) > 2)
  476. static struct {
  477. bool E1_show_target : 1;
  478. bool E2_show_target : 1;
  479. TERN_(HAS_HEATED_BED, bool bed_show_target : 1);
  480. } display_state = {
  481. true, true, TERN_(HAS_HEATED_BED, true)
  482. };
  483. void ST7920_Lite_Status_Screen::draw_temps(uint8_t line, const int16_t temp, const int16_t target, bool showTarget, bool targetStateChange) {
  484. switch (line) {
  485. case 0: set_ddram_address(DDRAM_LINE_1 + 1); break;
  486. case 1: set_ddram_address(DDRAM_LINE_2 + 1); break;
  487. case 2: set_ddram_address(DDRAM_LINE_3 + 1); break;
  488. case 3: set_ddram_address(DDRAM_LINE_3 + 1); break;
  489. }
  490. begin_data();
  491. write_number(temp);
  492. if (showTarget) {
  493. write_byte('\x1A');
  494. write_number(target);
  495. };
  496. if (targetStateChange) {
  497. if (!showTarget) write_str_P(PSTR(" "));
  498. draw_degree_symbol(5, line, !showTarget);
  499. draw_degree_symbol(9, line, showTarget);
  500. }
  501. }
  502. void ST7920_Lite_Status_Screen::draw_extruder_1_temp(const int16_t temp, const int16_t target, bool forceUpdate) {
  503. const bool show_target = target && FAR(temp, target);
  504. draw_temps(0, temp, target, show_target, display_state.E1_show_target != show_target || forceUpdate);
  505. display_state.E1_show_target = show_target;
  506. }
  507. void ST7920_Lite_Status_Screen::draw_extruder_2_temp(const int16_t temp, const int16_t target, bool forceUpdate) {
  508. const bool show_target = target && FAR(temp, target);
  509. draw_temps(1, temp, target, show_target, display_state.E2_show_target != show_target || forceUpdate);
  510. display_state.E2_show_target = show_target;
  511. }
  512. #if HAS_HEATED_BED
  513. void ST7920_Lite_Status_Screen::draw_bed_temp(const int16_t temp, const int16_t target, bool forceUpdate) {
  514. const bool show_target = target && FAR(temp, target);
  515. draw_temps(TERN(HAS_MULTI_HOTEND, 2, 1), temp, target, show_target, display_state.bed_show_target != show_target || forceUpdate);
  516. display_state.bed_show_target = show_target;
  517. }
  518. #endif
  519. void ST7920_Lite_Status_Screen::draw_fan_speed(const uint8_t value) {
  520. set_ddram_address(DDRAM_LINE_1 + 6);
  521. begin_data();
  522. write_number(value, 3);
  523. write_byte('%');
  524. }
  525. void ST7920_Lite_Status_Screen::draw_print_time(const duration_t &elapsed, char suffix) {
  526. #if HOTENDS == 1
  527. set_ddram_address(DDRAM_LINE_3);
  528. #else
  529. set_ddram_address(DDRAM_LINE_3 + 5);
  530. #endif
  531. char str[7];
  532. int str_length = elapsed.toDigital(str);
  533. str[str_length++] = suffix;
  534. begin_data();
  535. write_str(str, str_length);
  536. }
  537. void ST7920_Lite_Status_Screen::draw_feedrate_percentage(const uint16_t percentage) {
  538. // We only have enough room for the feedrate when
  539. // we have one extruder
  540. #if HOTENDS == 1
  541. set_ddram_address(DDRAM_LINE_2 + 6);
  542. begin_data();
  543. write_number(percentage, 3);
  544. write_byte('%');
  545. #else
  546. UNUSED(percentage);
  547. #endif
  548. }
  549. void ST7920_Lite_Status_Screen::draw_status_message() {
  550. const char *str = ui.status_message;
  551. set_ddram_address(DDRAM_LINE_4);
  552. begin_data();
  553. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  554. uint8_t slen = utf8_strlen(str);
  555. if (slen <= TEXT_MODE_LCD_WIDTH) {
  556. // String fits the LCD, so just print it
  557. write_str(str);
  558. while (slen < TEXT_MODE_LCD_WIDTH) { write_byte(' '); ++slen; }
  559. }
  560. else {
  561. // String is larger than the available space in screen.
  562. // Get a pointer to the next valid UTF8 character
  563. // and the string remaining length
  564. uint8_t rlen;
  565. const char *stat = ui.status_and_len(rlen);
  566. write_str(stat, TEXT_MODE_LCD_WIDTH);
  567. // If the remaining string doesn't completely fill the screen
  568. if (rlen < TEXT_MODE_LCD_WIDTH) {
  569. write_byte('.'); // Always at 1+ spaces left, draw a dot
  570. uint8_t chars = TEXT_MODE_LCD_WIDTH - rlen; // Amount of space left in characters
  571. if (--chars) { // Draw a second dot if there's space
  572. write_byte('.');
  573. if (--chars) write_str(str, chars); // Print a second copy of the message
  574. }
  575. }
  576. ui.advance_status_scroll();
  577. }
  578. #else
  579. uint8_t slen = utf8_strlen(str);
  580. write_str(str, TEXT_MODE_LCD_WIDTH);
  581. for (; slen < TEXT_MODE_LCD_WIDTH; ++slen) write_byte(' ');
  582. #endif
  583. }
  584. void ST7920_Lite_Status_Screen::draw_position(const xyze_pos_t &pos, const bool position_trusted) {
  585. char str[7];
  586. set_ddram_address(DDRAM_LINE_4);
  587. begin_data();
  588. // If position is unknown, flash the labels.
  589. const unsigned char alt_label = position_trusted ? 0 : (ui.get_blink() ? ' ' : 0);
  590. if (TERN1(LCD_SHOW_E_TOTAL, !printingIsActive())) {
  591. write_byte(alt_label ? alt_label : 'X');
  592. write_str(dtostrf(pos.x, -4, 0, str), 4);
  593. write_byte(alt_label ? alt_label : 'Y');
  594. write_str(dtostrf(pos.y, -4, 0, str), 4);
  595. }
  596. else {
  597. #if ENABLED(LCD_SHOW_E_TOTAL)
  598. char tmp[15];
  599. const uint8_t escale = e_move_accumulator >= 100000.0f ? 10 : 1; // After 100m switch to cm
  600. sprintf_P(tmp, PSTR("E%-7ld%cm "), uint32_t(_MAX(e_move_accumulator, 0.0f)) / escale, escale == 10 ? 'c' : 'm'); // 1234567mm
  601. write_str(tmp);
  602. #endif
  603. }
  604. write_byte(alt_label ? alt_label : 'Z');
  605. write_str(dtostrf(pos.z, -5, 1, str), 5);
  606. }
  607. bool ST7920_Lite_Status_Screen::indicators_changed() {
  608. // We only add the target temperatures to the checksum
  609. // because the actual temps fluctuate so by updating
  610. // them only during blinks we gain a bit of stability.
  611. const bool blink = ui.get_blink();
  612. const uint16_t feedrate_perc = feedrate_percentage;
  613. const uint16_t fs = thermalManager.scaledFanSpeed(0);
  614. const int16_t extruder_1_target = thermalManager.degTargetHotend(0);
  615. #if HAS_MULTI_HOTEND
  616. const int16_t extruder_2_target = thermalManager.degTargetHotend(1);
  617. #endif
  618. #if HAS_HEATED_BED
  619. const int16_t bed_target = thermalManager.degTargetBed();
  620. #endif
  621. static uint16_t last_checksum = 0;
  622. const uint16_t checksum = blink ^ feedrate_perc ^ fs ^ extruder_1_target
  623. ^ TERN0(HAS_MULTI_HOTEND, extruder_2_target)
  624. ^ TERN0(HAS_HEATED_BED, bed_target);
  625. if (last_checksum == checksum) return false;
  626. last_checksum = checksum;
  627. return true;
  628. }
  629. void ST7920_Lite_Status_Screen::update_indicators(const bool forceUpdate) {
  630. if (forceUpdate || indicators_changed()) {
  631. const bool blink = ui.get_blink();
  632. const duration_t elapsed = print_job_timer.duration();
  633. duration_t remaining = TERN0(USE_M73_REMAINING_TIME, ui.get_remaining_time());
  634. const uint16_t feedrate_perc = feedrate_percentage;
  635. const int16_t extruder_1_temp = thermalManager.degHotend(0),
  636. extruder_1_target = thermalManager.degTargetHotend(0);
  637. #if HAS_MULTI_HOTEND
  638. const int16_t extruder_2_temp = thermalManager.degHotend(1),
  639. extruder_2_target = thermalManager.degTargetHotend(1);
  640. #endif
  641. #if HAS_HEATED_BED
  642. const int16_t bed_temp = thermalManager.degBed(),
  643. bed_target = thermalManager.degTargetBed();
  644. #endif
  645. draw_extruder_1_temp(extruder_1_temp, extruder_1_target, forceUpdate);
  646. TERN_(HAS_MULTI_HOTEND, draw_extruder_2_temp(extruder_2_temp, extruder_2_target, forceUpdate));
  647. TERN_(HAS_HEATED_BED, draw_bed_temp(bed_temp, bed_target, forceUpdate));
  648. uint16_t spd = thermalManager.fan_speed[0];
  649. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  650. if (!blink && thermalManager.fan_speed_scaler[0] < 128)
  651. spd = thermalManager.scaledFanSpeed(0, spd);
  652. #endif
  653. draw_fan_speed(thermalManager.fanPercent(spd));
  654. // Draw elapsed/remaining time
  655. const bool show_remaining = ENABLED(SHOW_REMAINING_TIME) && (DISABLED(ROTATE_PROGRESS_DISPLAY) || blink);
  656. if (show_remaining && !remaining.second()) {
  657. const auto progress = ui.get_progress_percent();
  658. if (progress)
  659. remaining = elapsed.second() * (100 - progress) / progress;
  660. }
  661. if (show_remaining && remaining.second())
  662. draw_print_time(remaining, 'R');
  663. else
  664. draw_print_time(elapsed);
  665. draw_feedrate_percentage(feedrate_perc);
  666. // Update the fan and bed animations
  667. if (spd) draw_fan_icon(blink);
  668. TERN_(HAS_HEATED_BED, draw_heat_icon(bed_target > 0 && blink, bed_target > 0));
  669. }
  670. }
  671. bool ST7920_Lite_Status_Screen::position_changed() {
  672. const xyz_pos_t pos = current_position;
  673. const uint8_t checksum = uint8_t(pos.x) ^ uint8_t(pos.y) ^ uint8_t(pos.z);
  674. static uint8_t last_checksum = 0, changed = last_checksum != checksum;
  675. if (changed) last_checksum = checksum;
  676. return changed;
  677. }
  678. bool ST7920_Lite_Status_Screen::status_changed() {
  679. uint8_t checksum = 0;
  680. for (const char *p = ui.status_message; *p; p++) checksum ^= *p;
  681. static uint8_t last_checksum = 0;
  682. bool changed = last_checksum != checksum;
  683. if (changed) last_checksum = checksum;
  684. return changed;
  685. }
  686. bool ST7920_Lite_Status_Screen::blink_changed() {
  687. static uint8_t last_blink = 0;
  688. const bool blink = ui.get_blink(), changed = last_blink != blink;
  689. if (changed) last_blink = blink;
  690. return changed;
  691. }
  692. #ifndef STATUS_EXPIRE_SECONDS
  693. #define STATUS_EXPIRE_SECONDS 20
  694. #endif
  695. void ST7920_Lite_Status_Screen::update_status_or_position(bool forceUpdate) {
  696. #if STATUS_EXPIRE_SECONDS
  697. static uint8_t countdown = 0;
  698. #endif
  699. /**
  700. * There's only enough room for either the status message or the position,
  701. * so draw one or the other. When the status message changes, show it for
  702. * a few seconds, then return to the position display once the head moves.
  703. *
  704. * countdown > 1 -- Show status
  705. * countdown = 1 -- Show status, until movement
  706. * countdown = 0 -- Show position
  707. *
  708. * If STATUS_EXPIRE_SECONDS is zero, only the status is shown.
  709. */
  710. if (forceUpdate || status_changed()) {
  711. TERN_(STATUS_MESSAGE_SCROLLING, ui.status_scroll_offset = 0);
  712. #if STATUS_EXPIRE_SECONDS
  713. countdown = ui.status_message[0] ? STATUS_EXPIRE_SECONDS : 0;
  714. #endif
  715. draw_status_message();
  716. blink_changed(); // Clear changed flag
  717. }
  718. #if !STATUS_EXPIRE_SECONDS
  719. else if (TERN0(STATUS_MESSAGE_SCROLLING, blink_changed()))
  720. draw_status_message();
  721. #else
  722. else if (blink_changed()) {
  723. if (countdown > 1) {
  724. countdown--;
  725. TERN_(STATUS_MESSAGE_SCROLLING, draw_status_message());
  726. }
  727. else if (countdown > 0) {
  728. if (position_changed()) {
  729. countdown--;
  730. forceUpdate = true;
  731. }
  732. TERN_(STATUS_MESSAGE_SCROLLING, draw_status_message());
  733. }
  734. }
  735. if (countdown == 0 && (forceUpdate || position_changed() || TERN(DISABLE_REDUCED_ACCURACY_WARNING, 0, blink_changed())))
  736. draw_position(current_position, TERN(DISABLE_REDUCED_ACCURACY_WARNING, 1, all_axes_trusted()));
  737. #endif
  738. }
  739. void ST7920_Lite_Status_Screen::update_progress(const bool forceUpdate) {
  740. #if EITHER(LCD_SET_PROGRESS_MANUALLY, SDSUPPORT)
  741. // Since the progress bar involves writing
  742. // quite a few bytes to GDRAM, only do this
  743. // when an update is actually necessary.
  744. static uint8_t last_progress = 0;
  745. const uint8_t progress = ui.get_progress_percent();
  746. if (forceUpdate || last_progress != progress) {
  747. last_progress = progress;
  748. draw_progress_bar(progress);
  749. }
  750. #else
  751. UNUSED(forceUpdate);
  752. #endif
  753. }
  754. void ST7920_Lite_Status_Screen::update(const bool forceUpdate) {
  755. cs();
  756. update_indicators(forceUpdate);
  757. update_status_or_position(forceUpdate);
  758. update_progress(forceUpdate);
  759. ncs();
  760. }
  761. void ST7920_Lite_Status_Screen::reset_state_from_unknown() {
  762. _extended_function_set(true, true); // Do it twice as only one bit
  763. _extended_function_set(true, true); // get set at a time.
  764. _scroll_or_addr_select(false);
  765. }
  766. void ST7920_Lite_Status_Screen::on_entry() {
  767. cs();
  768. reset_state_from_unknown();
  769. clear();
  770. clear_gdram();
  771. draw_static_elements();
  772. update(true);
  773. ncs();
  774. }
  775. void ST7920_Lite_Status_Screen::on_exit() {
  776. cs();
  777. clear();
  778. _extended_function_set(true, true); // Restore state to what u8g expects.
  779. ncs();
  780. }
  781. // Called prior to the KILL screen to
  782. // clear the screen, preventing a garbled display.
  783. void ST7920_Lite_Status_Screen::clear_text_buffer() {
  784. cs();
  785. reset_state_from_unknown();
  786. clear();
  787. _extended_function_set(true, true); // Restore state to what u8g expects.
  788. ncs();
  789. }
  790. void MarlinUI::draw_status_screen() {
  791. ST7920_Lite_Status_Screen::update(false);
  792. }
  793. // This method is called before each screen update and
  794. // fires on_entry() and on_exit() events upon entering
  795. // or exiting the Status Screen.
  796. void MarlinUI::lcd_in_status(const bool inStatus) {
  797. static bool lastInStatus = false;
  798. if (lastInStatus == inStatus) return;
  799. if ((lastInStatus = inStatus))
  800. ST7920_Lite_Status_Screen::on_entry();
  801. else
  802. ST7920_Lite_Status_Screen::on_exit();
  803. }
  804. #endif // LIGHTWEIGHT_UI