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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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 + sizeof(degree_symbol)/sizeof(degree_symbol[0]);
  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(1
  520. #if HOTENDS > 1
  521. + 1
  522. #endif
  523. , temp, target, show_target, display_state.bed_show_target != show_target || forceUpdate
  524. );
  525. display_state.bed_show_target = show_target;
  526. }
  527. #endif
  528. void ST7920_Lite_Status_Screen::draw_fan_speed(const uint8_t value) {
  529. set_ddram_address(DDRAM_LINE_1 + 6);
  530. begin_data();
  531. write_number(value, 3);
  532. write_byte('%');
  533. }
  534. void ST7920_Lite_Status_Screen::draw_print_time(const duration_t &elapsed) {
  535. #if HOTENDS == 1
  536. set_ddram_address(DDRAM_LINE_3);
  537. #else
  538. set_ddram_address(DDRAM_LINE_3 + 5);
  539. #endif
  540. char str[7];
  541. str[elapsed.toDigital(str)] = ' ';
  542. begin_data();
  543. write_str(str, 6);
  544. }
  545. void ST7920_Lite_Status_Screen::draw_feedrate_percentage(const uint16_t percentage) {
  546. // We only have enough room for the feedrate when
  547. // we have one extruder
  548. #if HOTENDS == 1
  549. set_ddram_address(DDRAM_LINE_2 + 6);
  550. begin_data();
  551. write_number(percentage, 3);
  552. write_byte('%');
  553. #endif
  554. }
  555. void ST7920_Lite_Status_Screen::draw_status_message() {
  556. const char *str = ui.status_message;
  557. set_ddram_address(DDRAM_LINE_4);
  558. begin_data();
  559. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  560. uint8_t slen = utf8_strlen(str);
  561. if (slen <= LCD_WIDTH) {
  562. // String fits the LCD, so just print it
  563. write_str(str);
  564. for (; slen < LCD_WIDTH; ++slen) write_byte(' ');
  565. }
  566. else {
  567. // String is larger than the available space in screen.
  568. // Get a pointer to the next valid UTF8 character
  569. const char *stat = str + ui.status_scroll_offset;
  570. // Get the string remaining length
  571. const uint8_t rlen = utf8_strlen(stat);
  572. // If we have enough characters to display
  573. if (rlen >= LCD_WIDTH) {
  574. // The remaining string fills the screen - Print it
  575. write_str(stat, LCD_WIDTH);
  576. }
  577. else {
  578. // The remaining string does not completely fill the screen
  579. write_str(stat); // The string leaves space
  580. uint8_t chars = LCD_WIDTH - rlen; // Amount of space left in characters
  581. write_byte('.'); // Always at 1+ spaces left, draw a dot
  582. if (--chars) { // Draw a second dot if there's space
  583. write_byte('.');
  584. if (--chars)
  585. write_str(str, chars); // Print a second copy of the message
  586. }
  587. }
  588. // Adjust by complete UTF8 characters
  589. if (ui.status_scroll_offset < slen) {
  590. ui.status_scroll_offset++;
  591. while (!START_OF_UTF8_CHAR(str[ui.status_scroll_offset]))
  592. ui.status_scroll_offset++;
  593. }
  594. else
  595. ui.status_scroll_offset = 0;
  596. }
  597. #else
  598. uint8_t slen = utf8_strlen(str);
  599. write_str(str, LCD_WIDTH);
  600. for (; slen < LCD_WIDTH; ++slen) write_byte(' ');
  601. #endif
  602. }
  603. void ST7920_Lite_Status_Screen::draw_position(const float x, const float y, const float z, bool position_known) {
  604. char str[7];
  605. set_ddram_address(DDRAM_LINE_4);
  606. begin_data();
  607. // If position is unknown, flash the labels.
  608. const unsigned char alt_label = position_known ? 0 : (ui.get_blink() ? ' ' : 0);
  609. dtostrf(x, -4, 0, str);
  610. write_byte(alt_label ? alt_label : 'X');
  611. write_str(str, 4);
  612. dtostrf(y, -4, 0, str);
  613. write_byte(alt_label ? alt_label : 'Y');
  614. write_str(str, 4);
  615. dtostrf(z, -5, 1, str);
  616. write_byte(alt_label ? alt_label : 'Z');
  617. write_str(str, 5);
  618. }
  619. bool ST7920_Lite_Status_Screen::indicators_changed() {
  620. // We only add the target temperatures to the checksum
  621. // because the actual temps fluctuate so by updating
  622. // them only during blinks we gain a bit of stability.
  623. const bool blink = ui.get_blink();
  624. const uint16_t feedrate_perc = feedrate_percentage;
  625. const uint8_t fs = (((uint16_t)fan_speed[0] + 1) * 100) / 256;
  626. const int16_t extruder_1_target = thermalManager.degTargetHotend(0);
  627. #if HOTENDS > 1
  628. const int16_t extruder_2_target = thermalManager.degTargetHotend(1);
  629. #endif
  630. #if HAS_HEATED_BED
  631. const int16_t bed_target = thermalManager.degTargetBed();
  632. #endif
  633. static uint16_t last_checksum = 0;
  634. const uint16_t checksum = blink ^ feedrate_perc ^ fs ^ extruder_1_target
  635. #if HOTENDS > 1
  636. ^ extruder_2_target
  637. #endif
  638. #if HAS_HEATED_BED
  639. ^ bed_target
  640. #endif
  641. ;
  642. if (last_checksum == checksum) return false;
  643. last_checksum = checksum;
  644. return true;
  645. }
  646. void ST7920_Lite_Status_Screen::update_indicators(const bool forceUpdate) {
  647. if (forceUpdate || indicators_changed()) {
  648. const bool blink = ui.get_blink();
  649. const duration_t elapsed = print_job_timer.duration();
  650. const uint16_t feedrate_perc = feedrate_percentage;
  651. const uint8_t fs = (((uint16_t)fan_speed[0] + 1) * 100) / 256;
  652. const int16_t extruder_1_temp = thermalManager.degHotend(0),
  653. extruder_1_target = thermalManager.degTargetHotend(0);
  654. #if HOTENDS > 1
  655. const int16_t extruder_2_temp = thermalManager.degHotend(1),
  656. extruder_2_target = thermalManager.degTargetHotend(1);
  657. #endif
  658. #if HAS_HEATED_BED
  659. const int16_t bed_temp = thermalManager.degBed(),
  660. bed_target = thermalManager.degTargetBed();
  661. #endif
  662. draw_extruder_1_temp(extruder_1_temp, extruder_1_target, forceUpdate);
  663. #if HOTENDS > 1
  664. draw_extruder_2_temp(extruder_2_temp, extruder_2_target, forceUpdate);
  665. #endif
  666. #if HAS_HEATED_BED
  667. draw_bed_temp(bed_temp, bed_target, forceUpdate);
  668. #endif
  669. draw_fan_speed(fs);
  670. draw_print_time(elapsed);
  671. draw_feedrate_percentage(feedrate_perc);
  672. // Update the fan and bed animations
  673. if (fs) draw_fan_icon(blink);
  674. #if HAS_HEATED_BED
  675. draw_heat_icon(bed_target > 0 && blink, bed_target > 0);
  676. #endif
  677. }
  678. }
  679. bool ST7920_Lite_Status_Screen::position_changed() {
  680. const float x_pos = current_position[X_AXIS], y_pos = current_position[Y_AXIS], z_pos = current_position[Z_AXIS];
  681. const uint8_t checksum = uint8_t(x_pos) ^ uint8_t(y_pos) ^ uint8_t(z_pos);
  682. static uint8_t last_checksum = 0, changed = last_checksum != checksum;
  683. if (changed) last_checksum = checksum;
  684. return changed;
  685. }
  686. bool ST7920_Lite_Status_Screen::status_changed() {
  687. uint8_t checksum = 0;
  688. for (const char *p = ui.status_message; *p; p++) checksum ^= *p;
  689. static uint8_t last_checksum = 0, changed = last_checksum != checksum;
  690. if (changed) last_checksum = checksum;
  691. return changed;
  692. }
  693. bool ST7920_Lite_Status_Screen::blink_changed() {
  694. static uint8_t last_blink = 0;
  695. const bool blink = ui.get_blink(), changed = last_blink != blink;
  696. if (changed) last_blink = blink;
  697. return changed;
  698. }
  699. #ifndef STATUS_EXPIRE_SECONDS
  700. #define STATUS_EXPIRE_SECONDS 20
  701. #endif
  702. void ST7920_Lite_Status_Screen::update_status_or_position(bool forceUpdate) {
  703. #if STATUS_EXPIRE_SECONDS
  704. static uint8_t countdown = 0;
  705. #endif
  706. /**
  707. * There's only enough room for either the status message or the position,
  708. * so draw one or the other. When the status message changes, show it for
  709. * a few seconds, then return to the position display once the head moves.
  710. *
  711. * countdown > 1 -- Show status
  712. * countdown = 1 -- Show status, until movement
  713. * countdown = 0 -- Show position
  714. *
  715. * If STATUS_EXPIRE_SECONDS is zero, only the status is shown.
  716. */
  717. if (forceUpdate || status_changed()) {
  718. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  719. ui.status_scroll_offset = 0;
  720. #endif
  721. #if STATUS_EXPIRE_SECONDS
  722. countdown = ui.status_message[0] ? STATUS_EXPIRE_SECONDS : 0;
  723. #endif
  724. draw_status_message();
  725. blink_changed(); // Clear changed flag
  726. }
  727. #if !STATUS_EXPIRE_SECONDS
  728. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  729. else
  730. draw_status_message();
  731. #endif
  732. #else
  733. else if (blink_changed()) {
  734. if (countdown > 1) {
  735. countdown--;
  736. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  737. draw_status_message();
  738. #endif
  739. }
  740. else if (countdown > 0) {
  741. if (position_changed()) {
  742. countdown--;
  743. forceUpdate = true;
  744. }
  745. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  746. draw_status_message();
  747. #endif
  748. }
  749. }
  750. if (countdown == 0 && (forceUpdate || position_changed() ||
  751. #if DISABLED(DISABLE_REDUCED_ACCURACY_WARNING)
  752. blink_changed()
  753. #endif
  754. )) {
  755. draw_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  756. #if ENABLED(DISABLE_REDUCED_ACCURACY_WARNING)
  757. true
  758. #else
  759. all_axes_known()
  760. #endif
  761. );
  762. }
  763. #endif
  764. }
  765. void ST7920_Lite_Status_Screen::update_progress(const bool forceUpdate) {
  766. #if ENABLED(LCD_SET_PROGRESS_MANUALLY) || ENABLED(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();
  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. // This is 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. #if ENABLED(U8GLIB_ST7920) && !defined(U8G_HAL_LINKS) && !defined(__SAM3X8E__)
  817. #include "ultralcd_st7920_u8glib_rrd_AVR.h"
  818. void ST7920_Lite_Status_Screen::cs() {
  819. ST7920_CS();
  820. current_bits.synced = false;
  821. }
  822. void ST7920_Lite_Status_Screen::ncs() {
  823. ST7920_NCS();
  824. current_bits.synced = false;
  825. }
  826. void ST7920_Lite_Status_Screen::sync_cmd() {
  827. ST7920_SET_CMD();
  828. }
  829. void ST7920_Lite_Status_Screen::sync_dat() {
  830. ST7920_SET_DAT();
  831. }
  832. void ST7920_Lite_Status_Screen::write_byte(const uint8_t data) {
  833. ST7920_WRITE_BYTE(data);
  834. }
  835. #endif
  836. void MarlinUI::draw_status_screen() {
  837. ST7920_Lite_Status_Screen::update(false);
  838. }
  839. // This method is called before each screen update and
  840. // fires on_entry() and on_exit() events upon entering
  841. // or exiting the Status Screen.
  842. void MarlinUI::lcd_in_status(const bool inStatus) {
  843. static bool lastInStatus = false;
  844. if (lastInStatus == inStatus) return;
  845. if ((lastInStatus = inStatus))
  846. ST7920_Lite_Status_Screen::on_entry();
  847. else
  848. ST7920_Lite_Status_Screen::on_exit();
  849. }
  850. #endif // LIGHTWEIGHT_UI