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

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