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.h 28KB

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