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 30KB

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