My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

status_screen_lite_ST7920.cpp 27KB

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