My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

status_screen_lite_ST7920.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 HOTENDS > 1
  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. #if HAS_HEATED_BED
  481. bool bed_show_target : 1;
  482. #endif
  483. } display_state = {
  484. true, true
  485. #if HAS_HEATED_BED
  486. , true
  487. #endif
  488. };
  489. void ST7920_Lite_Status_Screen::draw_temps(uint8_t line, const int16_t temp, const int16_t target, bool showTarget, bool targetStateChange) {
  490. switch (line) {
  491. case 0: set_ddram_address(DDRAM_LINE_1 + 1); break;
  492. case 1: set_ddram_address(DDRAM_LINE_2 + 1); break;
  493. case 2: set_ddram_address(DDRAM_LINE_3 + 1); break;
  494. case 3: set_ddram_address(DDRAM_LINE_3 + 1); break;
  495. }
  496. begin_data();
  497. write_number(temp);
  498. if (showTarget) {
  499. write_byte('\x1A');
  500. write_number(target);
  501. };
  502. if (targetStateChange) {
  503. if (!showTarget) write_str_P(PSTR(" "));
  504. draw_degree_symbol(5, line, !showTarget);
  505. draw_degree_symbol(9, line, showTarget);
  506. }
  507. }
  508. void ST7920_Lite_Status_Screen::draw_extruder_1_temp(const int16_t temp, const int16_t target, bool forceUpdate) {
  509. const bool show_target = target && FAR(temp, target);
  510. draw_temps(0, temp, target, show_target, display_state.E1_show_target != show_target || forceUpdate);
  511. display_state.E1_show_target = show_target;
  512. }
  513. void ST7920_Lite_Status_Screen::draw_extruder_2_temp(const int16_t temp, const int16_t target, bool forceUpdate) {
  514. const bool show_target = target && FAR(temp, target);
  515. draw_temps(1, temp, target, show_target, display_state.E2_show_target != show_target || forceUpdate);
  516. display_state.E2_show_target = show_target;
  517. }
  518. #if HAS_HEATED_BED
  519. void ST7920_Lite_Status_Screen::draw_bed_temp(const int16_t temp, const int16_t target, bool forceUpdate) {
  520. const bool show_target = target && FAR(temp, target);
  521. draw_temps(HOTENDS > 1 ? 2 : 1, temp, target, show_target, display_state.bed_show_target != show_target || forceUpdate);
  522. display_state.bed_show_target = show_target;
  523. }
  524. #endif
  525. void ST7920_Lite_Status_Screen::draw_fan_speed(const uint8_t value) {
  526. set_ddram_address(DDRAM_LINE_1 + 6);
  527. begin_data();
  528. write_number(value, 3);
  529. write_byte('%');
  530. }
  531. void ST7920_Lite_Status_Screen::draw_print_time(const duration_t &elapsed) {
  532. #if HOTENDS == 1
  533. set_ddram_address(DDRAM_LINE_3);
  534. #else
  535. set_ddram_address(DDRAM_LINE_3 + 5);
  536. #endif
  537. char str[7];
  538. str[elapsed.toDigital(str)] = ' ';
  539. begin_data();
  540. write_str(str, 6);
  541. }
  542. void ST7920_Lite_Status_Screen::draw_feedrate_percentage(const uint16_t percentage) {
  543. // We only have enough room for the feedrate when
  544. // we have one extruder
  545. #if HOTENDS == 1
  546. set_ddram_address(DDRAM_LINE_2 + 6);
  547. begin_data();
  548. write_number(percentage, 3);
  549. write_byte('%');
  550. #else
  551. UNUSED(percentage);
  552. #endif
  553. }
  554. void ST7920_Lite_Status_Screen::draw_status_message() {
  555. const char *str = ui.status_message;
  556. set_ddram_address(DDRAM_LINE_4);
  557. begin_data();
  558. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  559. uint8_t slen = utf8_strlen(str);
  560. if (slen <= TEXT_MODE_LCD_WIDTH) {
  561. // String fits the LCD, so just print it
  562. write_str(str);
  563. while (slen < TEXT_MODE_LCD_WIDTH) { write_byte(' '); ++slen; }
  564. }
  565. else {
  566. // String is larger than the available space in screen.
  567. // Get a pointer to the next valid UTF8 character
  568. // and the string remaining length
  569. uint8_t rlen;
  570. const char *stat = ui.status_and_len(rlen);
  571. write_str(stat, TEXT_MODE_LCD_WIDTH);
  572. // If the remaining string doesn't completely fill the screen
  573. if (rlen < TEXT_MODE_LCD_WIDTH) {
  574. write_byte('.'); // Always at 1+ spaces left, draw a dot
  575. uint8_t chars = TEXT_MODE_LCD_WIDTH - rlen; // Amount of space left in characters
  576. if (--chars) { // Draw a second dot if there's space
  577. write_byte('.');
  578. if (--chars) write_str(str, chars); // Print a second copy of the message
  579. }
  580. }
  581. ui.advance_status_scroll();
  582. }
  583. #else
  584. uint8_t slen = utf8_strlen(str);
  585. write_str(str, TEXT_MODE_LCD_WIDTH);
  586. for (; slen < TEXT_MODE_LCD_WIDTH; ++slen) write_byte(' ');
  587. #endif
  588. }
  589. void ST7920_Lite_Status_Screen::draw_position(const xyze_pos_t &pos, const bool position_known) {
  590. char str[7];
  591. set_ddram_address(DDRAM_LINE_4);
  592. begin_data();
  593. // If position is unknown, flash the labels.
  594. const unsigned char alt_label = position_known ? 0 : (ui.get_blink() ? ' ' : 0);
  595. if (true
  596. #if ENABLED(LCD_SHOW_E_TOTAL)
  597. && !printingIsActive()
  598. #endif
  599. ) {
  600. write_byte(alt_label ? alt_label : 'X');
  601. write_str(dtostrf(pos.x, -4, 0, str), 4);
  602. write_byte(alt_label ? alt_label : 'Y');
  603. write_str(dtostrf(pos.y, -4, 0, str), 4);
  604. }
  605. else {
  606. #if ENABLED(LCD_SHOW_E_TOTAL)
  607. char tmp[15];
  608. const uint8_t escale = e_move_accumulator >= 100000.0f ? 10 : 1; // After 100m switch to cm
  609. sprintf_P(tmp, PSTR("E%-7ld%cm "), uint32_t(_MAX(e_move_accumulator, 0.0f)) / escale, escale == 10 ? 'c' : 'm'); // 1234567mm
  610. write_str(tmp);
  611. #endif
  612. }
  613. write_byte(alt_label ? alt_label : 'Z');
  614. write_str(dtostrf(pos.z, -5, 1, str), 5);
  615. }
  616. bool ST7920_Lite_Status_Screen::indicators_changed() {
  617. // We only add the target temperatures to the checksum
  618. // because the actual temps fluctuate so by updating
  619. // them only during blinks we gain a bit of stability.
  620. const bool blink = ui.get_blink();
  621. const uint16_t feedrate_perc = feedrate_percentage;
  622. const uint16_t fs = thermalManager.scaledFanSpeed(0);
  623. const int16_t extruder_1_target = thermalManager.degTargetHotend(0);
  624. #if HOTENDS > 1
  625. const int16_t extruder_2_target = thermalManager.degTargetHotend(1);
  626. #endif
  627. #if HAS_HEATED_BED
  628. const int16_t bed_target = thermalManager.degTargetBed();
  629. #endif
  630. static uint16_t last_checksum = 0;
  631. const uint16_t checksum = blink ^ feedrate_perc ^ fs ^ extruder_1_target
  632. #if HOTENDS > 1
  633. ^ extruder_2_target
  634. #endif
  635. #if HAS_HEATED_BED
  636. ^ bed_target
  637. #endif
  638. ;
  639. if (last_checksum == checksum) return false;
  640. last_checksum = checksum;
  641. return true;
  642. }
  643. void ST7920_Lite_Status_Screen::update_indicators(const bool forceUpdate) {
  644. if (forceUpdate || indicators_changed()) {
  645. const bool blink = ui.get_blink();
  646. const duration_t elapsed = print_job_timer.duration();
  647. const uint16_t feedrate_perc = feedrate_percentage;
  648. const int16_t extruder_1_temp = thermalManager.degHotend(0),
  649. extruder_1_target = thermalManager.degTargetHotend(0);
  650. #if HOTENDS > 1
  651. const int16_t extruder_2_temp = thermalManager.degHotend(1),
  652. extruder_2_target = thermalManager.degTargetHotend(1);
  653. #endif
  654. #if HAS_HEATED_BED
  655. const int16_t bed_temp = thermalManager.degBed(),
  656. bed_target = thermalManager.degTargetBed();
  657. #endif
  658. draw_extruder_1_temp(extruder_1_temp, extruder_1_target, forceUpdate);
  659. #if HOTENDS > 1
  660. draw_extruder_2_temp(extruder_2_temp, extruder_2_target, forceUpdate);
  661. #endif
  662. #if HAS_HEATED_BED
  663. draw_bed_temp(bed_temp, bed_target, forceUpdate);
  664. #endif
  665. uint16_t spd = thermalManager.fan_speed[0];
  666. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  667. if (!blink && thermalManager.fan_speed_scaler[0] < 128)
  668. spd = thermalManager.scaledFanSpeed(0, spd);
  669. #endif
  670. draw_fan_speed(thermalManager.fanPercent(spd));
  671. draw_print_time(elapsed);
  672. draw_feedrate_percentage(feedrate_perc);
  673. // Update the fan and bed animations
  674. if (spd) draw_fan_icon(blink);
  675. #if HAS_HEATED_BED
  676. draw_heat_icon(bed_target > 0 && blink, bed_target > 0);
  677. #endif
  678. }
  679. }
  680. bool ST7920_Lite_Status_Screen::position_changed() {
  681. const xyz_pos_t pos = current_position;
  682. const uint8_t checksum = uint8_t(pos.x) ^ uint8_t(pos.y) ^ uint8_t(pos.z);
  683. static uint8_t last_checksum = 0, changed = last_checksum != checksum;
  684. if (changed) last_checksum = checksum;
  685. return changed;
  686. }
  687. bool ST7920_Lite_Status_Screen::status_changed() {
  688. uint8_t checksum = 0;
  689. for (const char *p = ui.status_message; *p; p++) checksum ^= *p;
  690. static uint8_t last_checksum = 0;
  691. bool changed = last_checksum != checksum;
  692. if (changed) last_checksum = checksum;
  693. return changed;
  694. }
  695. bool ST7920_Lite_Status_Screen::blink_changed() {
  696. static uint8_t last_blink = 0;
  697. const bool blink = ui.get_blink(), changed = last_blink != blink;
  698. if (changed) last_blink = blink;
  699. return changed;
  700. }
  701. #ifndef STATUS_EXPIRE_SECONDS
  702. #define STATUS_EXPIRE_SECONDS 20
  703. #endif
  704. void ST7920_Lite_Status_Screen::update_status_or_position(bool forceUpdate) {
  705. #if STATUS_EXPIRE_SECONDS
  706. static uint8_t countdown = 0;
  707. #endif
  708. /**
  709. * There's only enough room for either the status message or the position,
  710. * so draw one or the other. When the status message changes, show it for
  711. * a few seconds, then return to the position display once the head moves.
  712. *
  713. * countdown > 1 -- Show status
  714. * countdown = 1 -- Show status, until movement
  715. * countdown = 0 -- Show position
  716. *
  717. * If STATUS_EXPIRE_SECONDS is zero, only the status is shown.
  718. */
  719. if (forceUpdate || status_changed()) {
  720. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  721. ui.status_scroll_offset = 0;
  722. #endif
  723. #if STATUS_EXPIRE_SECONDS
  724. countdown = ui.status_message[0] ? STATUS_EXPIRE_SECONDS : 0;
  725. #endif
  726. draw_status_message();
  727. blink_changed(); // Clear changed flag
  728. }
  729. #if !STATUS_EXPIRE_SECONDS
  730. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  731. else if (blink_changed())
  732. draw_status_message();
  733. #endif
  734. #else
  735. else if (blink_changed()) {
  736. if (countdown > 1) {
  737. countdown--;
  738. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  739. draw_status_message();
  740. #endif
  741. }
  742. else if (countdown > 0) {
  743. if (position_changed()) {
  744. countdown--;
  745. forceUpdate = true;
  746. }
  747. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  748. draw_status_message();
  749. #endif
  750. }
  751. }
  752. if (countdown == 0 && (forceUpdate || position_changed()
  753. #if DISABLED(DISABLE_REDUCED_ACCURACY_WARNING)
  754. || blink_changed()
  755. #endif
  756. )) {
  757. draw_position(current_position, true
  758. #if DISABLED(DISABLE_REDUCED_ACCURACY_WARNING)
  759. && all_axes_known()
  760. #endif
  761. );
  762. }
  763. #endif
  764. }
  765. void ST7920_Lite_Status_Screen::update_progress(const bool forceUpdate) {
  766. #if EITHER(LCD_SET_PROGRESS_MANUALLY, SDSUPPORT)
  767. // Since the progress bar involves writing
  768. // quite a few bytes to GDRAM, only do this
  769. // when an update is actually necessary.
  770. static uint8_t last_progress = 0;
  771. const uint8_t progress = ui.get_progress_percent();
  772. if (forceUpdate || last_progress != progress) {
  773. last_progress = progress;
  774. draw_progress_bar(progress);
  775. }
  776. #else
  777. UNUSED(forceUpdate);
  778. #endif // LCD_SET_PROGRESS_MANUALLY || SDSUPPORT
  779. }
  780. void ST7920_Lite_Status_Screen::update(const bool forceUpdate) {
  781. cs();
  782. update_indicators(forceUpdate);
  783. update_status_or_position(forceUpdate);
  784. update_progress(forceUpdate);
  785. ncs();
  786. }
  787. void ST7920_Lite_Status_Screen::reset_state_from_unknown() {
  788. _extended_function_set(true, true); // Do it twice as only one bit
  789. _extended_function_set(true, true); // get set at a time.
  790. _scroll_or_addr_select(false);
  791. }
  792. void ST7920_Lite_Status_Screen::on_entry() {
  793. cs();
  794. reset_state_from_unknown();
  795. clear();
  796. clear_gdram();
  797. draw_static_elements();
  798. update(true);
  799. ncs();
  800. }
  801. void ST7920_Lite_Status_Screen::on_exit() {
  802. cs();
  803. clear();
  804. _extended_function_set(true, true); // Restore state to what u8g expects.
  805. ncs();
  806. }
  807. // Called prior to the KILL screen to
  808. // clear the screen, preventing a garbled display.
  809. void ST7920_Lite_Status_Screen::clear_text_buffer() {
  810. cs();
  811. reset_state_from_unknown();
  812. clear();
  813. _extended_function_set(true, true); // Restore state to what u8g expects.
  814. ncs();
  815. }
  816. void MarlinUI::draw_status_screen() {
  817. ST7920_Lite_Status_Screen::update(false);
  818. }
  819. // This method is called before each screen update and
  820. // fires on_entry() and on_exit() events upon entering
  821. // or exiting the Status Screen.
  822. void MarlinUI::lcd_in_status(const bool inStatus) {
  823. static bool lastInStatus = false;
  824. if (lastInStatus == inStatus) return;
  825. if ((lastInStatus = inStatus))
  826. ST7920_Lite_Status_Screen::on_entry();
  827. else
  828. ST7920_Lite_Status_Screen::on_exit();
  829. }
  830. #endif // LIGHTWEIGHT_UI