lcd.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //menu.cpp
  2. #include "lcd.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <avr/pgmspace.h>
  6. #include "Timer.h"
  7. LiquidCrystal_Prusa lcd(LCD_PINS_RS, LCD_PINS_ENABLE, LCD_PINS_D4, LCD_PINS_D5,LCD_PINS_D6,LCD_PINS_D7); //RS,Enable,D4,D5,D6,D7
  8. FILE _lcdout = {0};
  9. int lcd_putchar(char c, FILE *stream)
  10. {
  11. lcd_write(c);
  12. return 0;
  13. }
  14. void lcd_command(uint8_t value)
  15. {
  16. lcd.command(value);
  17. }
  18. uint8_t lcd_write(uint8_t value)
  19. {
  20. return lcd.write(value);
  21. }
  22. void lcd_clear(void)
  23. {
  24. lcd.clear();
  25. }
  26. void lcd_set_cursor(uint8_t c, uint8_t r)
  27. {
  28. // lcd_printf_P(PSTR("\x1b[%hhu;%hhuH"), r, c);
  29. lcd.setCursor(c, r);
  30. }
  31. int lcd_putc(int c)
  32. {
  33. return fputc(c, lcdout);
  34. }
  35. int lcd_puts_P(const char* str)
  36. {
  37. return fputs_P(str, lcdout);
  38. }
  39. int lcd_puts_at_P(uint8_t c, uint8_t r, const char* str)
  40. {
  41. lcd_set_cursor(c, r);
  42. return fputs_P(str, lcdout);
  43. }
  44. int lcd_printf_P(const char* format, ...)
  45. {
  46. va_list args;
  47. va_start(args, format);
  48. int ret = vfprintf_P(lcdout, format, args);
  49. va_end(args);
  50. return ret;
  51. }
  52. void lcd_print(const char* s)
  53. {
  54. while (*s) lcd_write(*(s++));
  55. }
  56. void lcd_print(char c, int base)
  57. {
  58. lcd_print((long) c, base);
  59. }
  60. void lcd_print(unsigned char b, int base)
  61. {
  62. lcd_print((unsigned long) b, base);
  63. }
  64. void lcd_print(int n, int base)
  65. {
  66. lcd_print((long) n, base);
  67. }
  68. void lcd_print(unsigned int n, int base)
  69. {
  70. lcd_print((unsigned long) n, base);
  71. }
  72. void lcd_print(long n, int base)
  73. {
  74. if (base == 0)
  75. lcd_write(n);
  76. else if (base == 10)
  77. {
  78. if (n < 0)
  79. {
  80. lcd_print('-');
  81. n = -n;
  82. }
  83. lcd_printNumber(n, 10);
  84. }
  85. else
  86. lcd_printNumber(n, base);
  87. }
  88. void lcd_print(unsigned long n, int base)
  89. {
  90. if (base == 0)
  91. lcd_write(n);
  92. else
  93. lcd_printNumber(n, base);
  94. }
  95. void lcd_print(double n, int digits)
  96. {
  97. lcd_printFloat(n, digits);
  98. }
  99. void lcd_printNumber(unsigned long n, uint8_t base)
  100. {
  101. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  102. unsigned long i = 0;
  103. if (n == 0)
  104. {
  105. lcd_print('0');
  106. return;
  107. }
  108. while (n > 0)
  109. {
  110. buf[i++] = n % base;
  111. n /= base;
  112. }
  113. for (; i > 0; i--)
  114. lcd_print((char) (buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
  115. }
  116. void lcd_printFloat(double number, uint8_t digits)
  117. {
  118. // Handle negative numbers
  119. if (number < 0.0)
  120. {
  121. lcd_print('-');
  122. number = -number;
  123. }
  124. // Round correctly so that print(1.999, 2) prints as "2.00"
  125. double rounding = 0.5;
  126. for (uint8_t i=0; i<digits; ++i)
  127. rounding /= 10.0;
  128. number += rounding;
  129. // Extract the integer part of the number and print it
  130. unsigned long int_part = (unsigned long)number;
  131. double remainder = number - (double)int_part;
  132. lcd_print(int_part);
  133. // Print the decimal point, but only if there are digits beyond
  134. if (digits > 0)
  135. lcd_print('.');
  136. // Extract digits from the remainder one at a time
  137. while (digits-- > 0)
  138. {
  139. remainder *= 10.0;
  140. int toPrint = int(remainder);
  141. lcd_print(toPrint);
  142. remainder -= toPrint;
  143. }
  144. }
  145. uint8_t lcd_draw_update = 2;
  146. int32_t lcd_encoder = 0;
  147. uint8_t lcd_encoder_bits = 0;
  148. int8_t lcd_encoder_diff = 0;
  149. uint8_t lcd_buttons = 0;
  150. uint8_t lcd_button_pressed = 0;
  151. uint8_t lcd_update_enabled = 1;
  152. uint32_t lcd_timeoutToStatus = 0;
  153. uint32_t lcd_next_update_millis = 0;
  154. uint8_t lcd_status_update_delay = 0;
  155. uint8_t lcd_long_press_active = 0;
  156. lcd_longpress_func_t lcd_longpress_func = 0;
  157. lcd_charsetup_func_t lcd_charsetup_func = 0;
  158. lcd_lcdupdate_func_t lcd_lcdupdate_func = 0;
  159. uint32_t lcd_button_blanking_time = millis();
  160. ShortTimer longPressTimer;
  161. uint8_t lcd_clicked(void)
  162. {
  163. bool clicked = LCD_CLICKED;
  164. if(clicked) lcd_button_pressed = 1;
  165. return clicked;
  166. }
  167. void lcd_beeper_quick_feedback(void)
  168. {
  169. SET_OUTPUT(BEEPER);
  170. for(int8_t i = 0; i < 10; i++)
  171. {
  172. WRITE(BEEPER,HIGH);
  173. delayMicroseconds(100);
  174. WRITE(BEEPER,LOW);
  175. delayMicroseconds(100);
  176. }
  177. }
  178. void lcd_quick_feedback(void)
  179. {
  180. lcd_draw_update = 2;
  181. lcd_button_pressed = false;
  182. lcd_beeper_quick_feedback();
  183. }
  184. void lcd_update(uint8_t lcdDrawUpdateOverride)
  185. {
  186. if (lcd_draw_update < lcdDrawUpdateOverride)
  187. lcd_draw_update = lcdDrawUpdateOverride;
  188. if (!lcd_update_enabled)
  189. return;
  190. lcd_buttons_update();
  191. if (lcd_lcdupdate_func)
  192. lcd_lcdupdate_func();
  193. }
  194. void lcd_update_enable(uint8_t enabled)
  195. {
  196. if (lcd_update_enabled != enabled)
  197. {
  198. lcd_update_enabled = enabled;
  199. if (enabled)
  200. { // Reset encoder position. This is equivalent to re-entering a menu.
  201. lcd_encoder = 0;
  202. lcd_encoder_diff = 0;
  203. // Enabling the normal LCD update procedure.
  204. // Reset the timeout interval.
  205. lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  206. // Force the keypad update now.
  207. lcd_next_update_millis = millis() - 1;
  208. // Full update.
  209. lcd_clear();
  210. if (lcd_charsetup_func)
  211. lcd_charsetup_func();
  212. lcd_update(2);
  213. } else
  214. {
  215. // Clear the LCD always, or let it to the caller?
  216. }
  217. }
  218. }
  219. void lcd_buttons_update(void)
  220. {
  221. static bool _lock = false;
  222. if (_lock) return;
  223. _lock = true;
  224. uint8_t newbutton = 0;
  225. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  226. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  227. if (lcd_update_enabled)
  228. { //if we are in non-modal mode, long press can be used and short press triggers with button release
  229. if (READ(BTN_ENC) == 0)
  230. { //button is pressed
  231. lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  232. if (millis() > lcd_button_blanking_time)
  233. {
  234. lcd_button_blanking_time = millis() + BUTTON_BLANKING_TIME;
  235. if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
  236. {
  237. longPressTimer.start();
  238. lcd_button_pressed = 1;
  239. }
  240. else
  241. {
  242. if (longPressTimer.expired(LONG_PRESS_TIME))
  243. {
  244. lcd_long_press_active = 1;
  245. if (lcd_longpress_func)
  246. lcd_longpress_func();
  247. }
  248. }
  249. }
  250. }
  251. else
  252. { //button not pressed
  253. if (lcd_button_pressed)
  254. { //button was released
  255. lcd_button_blanking_time = millis() + BUTTON_BLANKING_TIME;
  256. if (lcd_long_press_active == 0)
  257. { //button released before long press gets activated
  258. newbutton |= EN_C;
  259. }
  260. //else if (menu_menu == lcd_move_z) lcd_quick_feedback();
  261. //lcd_button_pressed is set back to false via lcd_quick_feedback function
  262. }
  263. else
  264. lcd_long_press_active = 0;
  265. }
  266. }
  267. else
  268. { //we are in modal mode
  269. if (READ(BTN_ENC) == 0)
  270. newbutton |= EN_C;
  271. }
  272. lcd_buttons = newbutton;
  273. //manage encoder rotation
  274. uint8_t enc = 0;
  275. if (lcd_buttons & EN_A) enc |= B01;
  276. if (lcd_buttons & EN_B) enc |= B10;
  277. if (enc != lcd_encoder_bits)
  278. {
  279. switch (enc)
  280. {
  281. case encrot0:
  282. if (lcd_encoder_bits == encrot3)
  283. lcd_encoder_diff++;
  284. else if (lcd_encoder_bits == encrot1)
  285. lcd_encoder_diff--;
  286. break;
  287. case encrot1:
  288. if (lcd_encoder_bits == encrot0)
  289. lcd_encoder_diff++;
  290. else if (lcd_encoder_bits == encrot2)
  291. lcd_encoder_diff--;
  292. break;
  293. case encrot2:
  294. if (lcd_encoder_bits == encrot1)
  295. lcd_encoder_diff++;
  296. else if (lcd_encoder_bits == encrot3)
  297. lcd_encoder_diff--;
  298. break;
  299. case encrot3:
  300. if (lcd_encoder_bits == encrot2)
  301. lcd_encoder_diff++;
  302. else if (lcd_encoder_bits == encrot0)
  303. lcd_encoder_diff--;
  304. break;
  305. }
  306. }
  307. lcd_encoder_bits = enc;
  308. _lock = false;
  309. }
  310. void lcd_implementation_init(void)
  311. {
  312. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  313. lcd_set_custom_characters();
  314. lcd.clear();
  315. }
  316. void lcd_implementation_init_noclear(void)
  317. {
  318. lcd.begin_noclear(LCD_WIDTH, LCD_HEIGHT);
  319. lcd_set_custom_characters();
  320. }
  321. void lcd_drawedit(const char* pstr, char* value)
  322. {
  323. lcd_set_cursor(1, 1);
  324. lcd_puts_P(pstr);
  325. lcd_print(':');
  326. #if LCD_WIDTH < 20
  327. lcd_set_cursor(LCD_WIDTH - strlen(value), 1);
  328. #else
  329. lcd_set_cursor(LCD_WIDTH -1 - strlen(value), 1);
  330. #endif
  331. lcd_print(value);
  332. }
  333. void lcd_drawedit_2(const char* pstr, char* value)
  334. {
  335. lcd_set_cursor(0, 1);
  336. lcd_puts_P(pstr);
  337. lcd_print(':');
  338. lcd_set_cursor((LCD_WIDTH - strlen(value))/2, 3);
  339. lcd_print(value);
  340. lcd_print(" mm");
  341. }
  342. ////////////////////////////////////////////////////////////////////////////////
  343. // Custom character data
  344. const uint8_t lcd_chardata_bedTemp[8] PROGMEM = {
  345. B00000,
  346. B11111,
  347. B10101,
  348. B10001,
  349. B10101,
  350. B11111,
  351. B00000,
  352. B00000}; //thanks Sonny Mounicou
  353. const uint8_t lcd_chardata_degree[8] PROGMEM = {
  354. B01100,
  355. B10010,
  356. B10010,
  357. B01100,
  358. B00000,
  359. B00000,
  360. B00000,
  361. B00000};
  362. const uint8_t lcd_chardata_thermometer[8] PROGMEM = {
  363. B00100,
  364. B01010,
  365. B01010,
  366. B01010,
  367. B01010,
  368. B10001,
  369. B10001,
  370. B01110};
  371. const uint8_t lcd_chardata_uplevel[8] PROGMEM = {
  372. B00100,
  373. B01110,
  374. B11111,
  375. B00100,
  376. B11100,
  377. B00000,
  378. B00000,
  379. B00000}; //thanks joris
  380. const uint8_t lcd_chardata_refresh[8] PROGMEM = {
  381. B00000,
  382. B00110,
  383. B11001,
  384. B11000,
  385. B00011,
  386. B10011,
  387. B01100,
  388. B00000}; //thanks joris
  389. const uint8_t lcd_chardata_folder[8] PROGMEM = {
  390. B00000,
  391. B11100,
  392. B11111,
  393. B10001,
  394. B10001,
  395. B11111,
  396. B00000,
  397. B00000}; //thanks joris
  398. const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  399. B11100,
  400. B10000,
  401. B11000,
  402. B10111,
  403. B00101,
  404. B00110,
  405. B00101,
  406. B00000}; //thanks Sonny Mounicou
  407. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  408. B11100,
  409. B10100,
  410. B11000,
  411. B10100,
  412. B00000,
  413. B00111,
  414. B00010,
  415. B00010};*/
  416. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  417. B01100,
  418. B10011,
  419. B00000,
  420. B01100,
  421. B10011,
  422. B00000,
  423. B01100,
  424. B10011};*/
  425. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  426. B00000,
  427. B00100,
  428. B10010,
  429. B01001,
  430. B10010,
  431. B00100,
  432. B00000,
  433. B00000};*/
  434. const uint8_t lcd_chardata_clock[8] PROGMEM = {
  435. B00000,
  436. B01110,
  437. B10011,
  438. B10101,
  439. B10001,
  440. B01110,
  441. B00000,
  442. B00000}; //thanks Sonny Mounicou
  443. const uint8_t lcd_chardata_arrup[8] PROGMEM = {
  444. B00100,
  445. B01110,
  446. B11111,
  447. B00000,
  448. B00000,
  449. B00000,
  450. B00000,
  451. B00000};
  452. const uint8_t lcd_chardata_arrdown[8] PROGMEM = {
  453. B00000,
  454. B00000,
  455. B00000,
  456. B00000,
  457. B00000,
  458. B10001,
  459. B01010,
  460. B00100};
  461. void lcd_set_custom_characters(void)
  462. {
  463. lcd.createChar_P(LCD_STR_BEDTEMP[0], lcd_chardata_bedTemp);
  464. lcd.createChar_P(LCD_STR_DEGREE[0], lcd_chardata_degree);
  465. lcd.createChar_P(LCD_STR_THERMOMETER[0], lcd_chardata_thermometer);
  466. lcd.createChar_P(LCD_STR_UPLEVEL[0], lcd_chardata_uplevel);
  467. lcd.createChar_P(LCD_STR_REFRESH[0], lcd_chardata_refresh);
  468. lcd.createChar_P(LCD_STR_FOLDER[0], lcd_chardata_folder);
  469. lcd.createChar_P(LCD_STR_FEEDRATE[0], lcd_chardata_feedrate);
  470. lcd.createChar_P(LCD_STR_CLOCK[0], lcd_chardata_clock);
  471. //lcd.createChar_P(LCD_STR_ARROW_UP[0], lcd_chardata_arrup);
  472. //lcd.createChar_P(LCD_STR_ARROW_DOWN[0], lcd_chardata_arrdown);
  473. }
  474. void lcd_set_custom_characters_arrows(void)
  475. {
  476. lcd.createChar_P(1, lcd_chardata_arrdown);
  477. }
  478. const uint8_t lcd_chardata_progress[8] PROGMEM = {
  479. B11111,
  480. B11111,
  481. B11111,
  482. B11111,
  483. B11111,
  484. B11111,
  485. B11111,
  486. B11111};
  487. void lcd_set_custom_characters_progress(void)
  488. {
  489. lcd.createChar_P(1, lcd_chardata_progress);
  490. }
  491. const uint8_t lcd_chardata_arr2down[8] PROGMEM = {
  492. B00000,
  493. B00000,
  494. B10001,
  495. B01010,
  496. B00100,
  497. B10001,
  498. B01010,
  499. B00100};
  500. const uint8_t lcd_chardata_confirm[8] PROGMEM = {
  501. B00000,
  502. B00001,
  503. B00011,
  504. B10110,
  505. B11100,
  506. B01000,
  507. B00000};
  508. void lcd_set_custom_characters_nextpage(void)
  509. {
  510. lcd.createChar_P(1, lcd_chardata_arr2down);
  511. lcd.createChar_P(2, lcd_chardata_confirm);
  512. }
  513. void lcd_set_custom_characters_degree(void)
  514. {
  515. lcd.createChar_P(1, lcd_chardata_degree);
  516. }