My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

status_screen_lite_ST7920.cpp 28KB

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