lcd.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. //menu.cpp
  2. #include "lcd.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <avr/pgmspace.h>
  6. #include <util/delay.h>
  7. #include "Timer.h"
  8. #include "Configuration.h"
  9. #include "pins.h"
  10. #include <binary.h>
  11. #include <Arduino.h>
  12. #include "Marlin.h"
  13. #include "fastio.h"
  14. //-//
  15. #include "sound.h"
  16. #define LCD_DEFAULT_DELAY 100
  17. #if (defined(LCD_PINS_D0) && defined(LCD_PINS_D1) && defined(LCD_PINS_D2) && defined(LCD_PINS_D3))
  18. #define LCD_8BIT
  19. #endif
  20. // #define VT100
  21. // commands
  22. #define LCD_CLEARDISPLAY 0x01
  23. #define LCD_RETURNHOME 0x02
  24. #define LCD_ENTRYMODESET 0x04
  25. #define LCD_DISPLAYCONTROL 0x08
  26. #define LCD_CURSORSHIFT 0x10
  27. #define LCD_FUNCTIONSET 0x20
  28. #define LCD_SETCGRAMADDR 0x40
  29. #define LCD_SETDDRAMADDR 0x80
  30. // flags for display entry mode
  31. #define LCD_ENTRYRIGHT 0x00
  32. #define LCD_ENTRYLEFT 0x02
  33. #define LCD_ENTRYSHIFTINCREMENT 0x01
  34. #define LCD_ENTRYSHIFTDECREMENT 0x00
  35. // flags for display on/off control
  36. #define LCD_DISPLAYON 0x04
  37. #define LCD_DISPLAYOFF 0x00
  38. #define LCD_CURSORON 0x02
  39. #define LCD_CURSOROFF 0x00
  40. #define LCD_BLINKON 0x01
  41. #define LCD_BLINKOFF 0x00
  42. // flags for display/cursor shift
  43. #define LCD_DISPLAYMOVE 0x08
  44. #define LCD_CURSORMOVE 0x00
  45. #define LCD_MOVERIGHT 0x04
  46. #define LCD_MOVELEFT 0x00
  47. // flags for function set
  48. #define LCD_8BITMODE 0x10
  49. #define LCD_4BITMODE 0x00
  50. #define LCD_2LINE 0x08
  51. #define LCD_1LINE 0x00
  52. #define LCD_5x10DOTS 0x04
  53. #define LCD_5x8DOTS 0x00
  54. // bitmasks for flag argument settings
  55. #define LCD_RS_FLAG 0x01
  56. #define LCD_HALF_FLAG 0x02
  57. FILE _lcdout; // = {0}; Global variable is always zero initialized, no need to explicitly state that.
  58. uint8_t lcd_displayfunction = 0;
  59. uint8_t lcd_displaycontrol = 0;
  60. uint8_t lcd_displaymode = 0;
  61. uint8_t lcd_currline;
  62. #ifdef VT100
  63. uint8_t lcd_escape[8];
  64. #endif
  65. static void lcd_display(void);
  66. #if 0
  67. static void lcd_no_display(void);
  68. static void lcd_no_cursor(void);
  69. static void lcd_cursor(void);
  70. static void lcd_no_blink(void);
  71. static void lcd_blink(void);
  72. static void lcd_scrollDisplayLeft(void);
  73. static void lcd_scrollDisplayRight(void);
  74. static void lcd_leftToRight(void);
  75. static void lcd_rightToLeft(void);
  76. static void lcd_autoscroll(void);
  77. static void lcd_no_autoscroll(void);
  78. #endif
  79. #ifdef VT100
  80. void lcd_escape_write(uint8_t chr);
  81. #endif
  82. static void lcd_pulseEnable(void)
  83. {
  84. WRITE(LCD_PINS_ENABLE,HIGH);
  85. _delay_us(1); // enable pulse must be >450ns
  86. WRITE(LCD_PINS_ENABLE,LOW);
  87. }
  88. static void lcd_writebits(uint8_t value)
  89. {
  90. #ifdef LCD_8BIT
  91. WRITE(LCD_PINS_D0, value & 0x01);
  92. WRITE(LCD_PINS_D1, value & 0x02);
  93. WRITE(LCD_PINS_D2, value & 0x04);
  94. WRITE(LCD_PINS_D3, value & 0x08);
  95. #endif
  96. WRITE(LCD_PINS_D4, value & 0x10);
  97. WRITE(LCD_PINS_D5, value & 0x20);
  98. WRITE(LCD_PINS_D6, value & 0x40);
  99. WRITE(LCD_PINS_D7, value & 0x80);
  100. lcd_pulseEnable();
  101. }
  102. static void lcd_send(uint8_t data, uint8_t flags, uint16_t duration = LCD_DEFAULT_DELAY)
  103. {
  104. WRITE(LCD_PINS_RS,flags&LCD_RS_FLAG);
  105. _delay_us(5);
  106. lcd_writebits(data);
  107. #ifndef LCD_8BIT
  108. if (!(flags & LCD_HALF_FLAG))
  109. {
  110. _delay_us(LCD_DEFAULT_DELAY);
  111. lcd_writebits(data<<4);
  112. }
  113. #endif
  114. delayMicroseconds(duration);
  115. }
  116. static void lcd_command(uint8_t value, uint16_t delayExtra = 0)
  117. {
  118. lcd_send(value, LOW, LCD_DEFAULT_DELAY + delayExtra);
  119. }
  120. static void lcd_write(uint8_t value)
  121. {
  122. if (value == '\n' || value == '\r')
  123. {
  124. if (lcd_currline > 3) lcd_currline = -1;
  125. lcd_set_cursor(0, lcd_currline + 1); // LF
  126. return;
  127. }
  128. #ifdef VT100
  129. if (lcd_escape[0] || (value == 0x1b)){
  130. lcd_escape_write(value);
  131. return;
  132. }
  133. #endif
  134. lcd_send(value, HIGH);
  135. }
  136. static void lcd_begin(uint8_t clear)
  137. {
  138. lcd_currline = 0;
  139. lcd_send(LCD_FUNCTIONSET | LCD_8BITMODE, LOW | LCD_HALF_FLAG, 4500); // wait min 4.1ms
  140. // second try
  141. lcd_send(LCD_FUNCTIONSET | LCD_8BITMODE, LOW | LCD_HALF_FLAG, 150);
  142. // third go!
  143. lcd_send(LCD_FUNCTIONSET | LCD_8BITMODE, LOW | LCD_HALF_FLAG, 150);
  144. #ifndef LCD_8BIT
  145. // set to 4-bit interface
  146. lcd_send(LCD_FUNCTIONSET | LCD_4BITMODE, LOW | LCD_HALF_FLAG, 150);
  147. #endif
  148. // finally, set # lines, font size, etc.0
  149. lcd_command(LCD_FUNCTIONSET | lcd_displayfunction);
  150. // turn the display on with no cursor or blinking default
  151. lcd_displaycontrol = LCD_CURSOROFF | LCD_BLINKOFF;
  152. lcd_display();
  153. // clear it off
  154. if (clear) lcd_clear();
  155. // Initialize to default text direction (for romance languages)
  156. lcd_displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  157. // set the entry mode
  158. lcd_command(LCD_ENTRYMODESET | lcd_displaymode);
  159. #ifdef VT100
  160. lcd_escape[0] = 0;
  161. #endif
  162. }
  163. static int lcd_putchar(char c, FILE *)
  164. {
  165. lcd_write(c);
  166. return c;
  167. }
  168. void lcd_init(void)
  169. {
  170. SET_OUTPUT(LCD_PINS_RS);
  171. SET_OUTPUT(LCD_PINS_ENABLE);
  172. #ifdef LCD_8BIT
  173. lcd_displayfunction |= LCD_8BITMODE;
  174. #endif
  175. lcd_displayfunction |= LCD_2LINE;
  176. _delay_us(50000);
  177. lcd_begin(1); //first time init
  178. fdev_setup_stream(lcdout, lcd_putchar, NULL, _FDEV_SETUP_WRITE); //setup lcdout stream
  179. }
  180. void lcd_refresh(void)
  181. {
  182. lcd_begin(1);
  183. lcd_set_custom_characters();
  184. }
  185. void lcd_refresh_noclear(void)
  186. {
  187. lcd_begin(0);
  188. lcd_set_custom_characters();
  189. }
  190. void lcd_clear(void)
  191. {
  192. lcd_command(LCD_CLEARDISPLAY, 1600); // clear display, set cursor position to zero
  193. lcd_currline = 0;
  194. }
  195. void lcd_home(void)
  196. {
  197. lcd_command(LCD_RETURNHOME, 1600); // set cursor position to zero
  198. lcd_currline = 0;
  199. }
  200. // Turn the display on/off (quickly)
  201. void lcd_display(void)
  202. {
  203. lcd_displaycontrol |= LCD_DISPLAYON;
  204. lcd_command(LCD_DISPLAYCONTROL | lcd_displaycontrol);
  205. }
  206. #if 0
  207. void lcd_no_display(void)
  208. {
  209. lcd_displaycontrol &= ~LCD_DISPLAYON;
  210. lcd_command(LCD_DISPLAYCONTROL | lcd_displaycontrol);
  211. }
  212. // Turns the underline cursor on/off
  213. void lcd_no_cursor(void)
  214. {
  215. lcd_displaycontrol &= ~LCD_CURSORON;
  216. lcd_command(LCD_DISPLAYCONTROL | lcd_displaycontrol);
  217. }
  218. void lcd_cursor(void)
  219. {
  220. lcd_displaycontrol |= LCD_CURSORON;
  221. lcd_command(LCD_DISPLAYCONTROL | lcd_displaycontrol);
  222. }
  223. // Turn on and off the blinking cursor
  224. void lcd_no_blink(void)
  225. {
  226. lcd_displaycontrol &= ~LCD_BLINKON;
  227. lcd_command(LCD_DISPLAYCONTROL | lcd_displaycontrol);
  228. }
  229. void lcd_blink(void)
  230. {
  231. lcd_displaycontrol |= LCD_BLINKON;
  232. lcd_command(LCD_DISPLAYCONTROL | lcd_displaycontrol);
  233. }
  234. // These commands scroll the display without changing the RAM
  235. void lcd_scrollDisplayLeft(void)
  236. {
  237. lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
  238. }
  239. void lcd_scrollDisplayRight(void)
  240. {
  241. lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
  242. }
  243. // This is for text that flows Left to Right
  244. void lcd_leftToRight(void)
  245. {
  246. lcd_displaymode |= LCD_ENTRYLEFT;
  247. lcd_command(LCD_ENTRYMODESET | lcd_displaymode);
  248. }
  249. // This is for text that flows Right to Left
  250. void lcd_rightToLeft(void)
  251. {
  252. lcd_displaymode &= ~LCD_ENTRYLEFT;
  253. lcd_command(LCD_ENTRYMODESET | lcd_displaymode);
  254. }
  255. // This will 'right justify' text from the cursor
  256. void lcd_autoscroll(void)
  257. {
  258. lcd_displaymode |= LCD_ENTRYSHIFTINCREMENT;
  259. lcd_command(LCD_ENTRYMODESET | lcd_displaymode);
  260. }
  261. // This will 'left justify' text from the cursor
  262. void lcd_no_autoscroll(void)
  263. {
  264. lcd_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
  265. lcd_command(LCD_ENTRYMODESET | lcd_displaymode);
  266. }
  267. #endif
  268. void lcd_set_cursor(uint8_t col, uint8_t row)
  269. {
  270. int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  271. if (row >= LCD_HEIGHT)
  272. row = LCD_HEIGHT - 1; // we count rows starting w/0
  273. lcd_currline = row;
  274. lcd_command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
  275. }
  276. // Allows us to fill the first 8 CGRAM locations
  277. // with custom characters
  278. void lcd_createChar_P(uint8_t location, const uint8_t* charmap)
  279. {
  280. location &= 0x7; // we only have 8 locations 0-7
  281. lcd_command(LCD_SETCGRAMADDR | (location << 3));
  282. for (int i=0; i<8; i++)
  283. lcd_send(pgm_read_byte(&charmap[i]), HIGH);
  284. }
  285. #ifdef VT100
  286. //Supported VT100 escape codes:
  287. //EraseScreen "\x1b[2J"
  288. //CursorHome "\x1b[%d;%dH"
  289. //CursorShow "\x1b[?25h"
  290. //CursorHide "\x1b[?25l"
  291. void lcd_escape_write(uint8_t chr)
  292. {
  293. #define escape_cnt (lcd_escape[0]) //escape character counter
  294. #define is_num_msk (lcd_escape[1]) //numeric character bit mask
  295. #define chr_is_num (is_num_msk & 0x01) //current character is numeric
  296. #define e_2_is_num (is_num_msk & 0x04) //escape char 2 is numeric
  297. #define e_3_is_num (is_num_msk & 0x08) //...
  298. #define e_4_is_num (is_num_msk & 0x10)
  299. #define e_5_is_num (is_num_msk & 0x20)
  300. #define e_6_is_num (is_num_msk & 0x40)
  301. #define e_7_is_num (is_num_msk & 0x80)
  302. #define e2_num (lcd_escape[2] - '0') //number from character 2
  303. #define e3_num (lcd_escape[3] - '0') //number from character 3
  304. #define e23_num (10*e2_num+e3_num) //number from characters 2 and 3
  305. #define e4_num (lcd_escape[4] - '0') //number from character 4
  306. #define e5_num (lcd_escape[5] - '0') //number from character 5
  307. #define e45_num (10*e4_num+e5_num) //number from characters 4 and 5
  308. #define e6_num (lcd_escape[6] - '0') //number from character 6
  309. #define e56_num (10*e5_num+e6_num) //number from characters 5 and 6
  310. if (escape_cnt > 1) // escape length > 1 = "\x1b["
  311. {
  312. lcd_escape[escape_cnt] = chr; // store current char
  313. if ((chr >= '0') && (chr <= '9')) // char is numeric
  314. is_num_msk |= (1 | (1 << escape_cnt)); //set mask
  315. else
  316. is_num_msk &= ~1; //clear mask
  317. }
  318. switch (escape_cnt++)
  319. {
  320. case 0:
  321. if (chr == 0x1b) return; // escape = "\x1b"
  322. break;
  323. case 1:
  324. is_num_msk = 0x00; // reset 'is number' bit mask
  325. if (chr == '[') return; // escape = "\x1b["
  326. break;
  327. case 2:
  328. switch (chr)
  329. {
  330. case '2': return; // escape = "\x1b[2"
  331. case '?': return; // escape = "\x1b[?"
  332. default:
  333. if (chr_is_num) return; // escape = "\x1b[%1d"
  334. }
  335. break;
  336. case 3:
  337. switch (lcd_escape[2])
  338. {
  339. case '?': // escape = "\x1b[?"
  340. if (chr == '2') return; // escape = "\x1b[?2"
  341. break;
  342. case '2':
  343. if (chr == 'J') // escape = "\x1b[2J"
  344. { lcd_clear(); lcd_currline = 0; break; } // EraseScreen
  345. default:
  346. if (e_2_is_num && // escape = "\x1b[%1d"
  347. ((chr == ';') || // escape = "\x1b[%1d;"
  348. chr_is_num)) // escape = "\x1b[%2d"
  349. return;
  350. }
  351. break;
  352. case 4:
  353. switch (lcd_escape[2])
  354. {
  355. case '?': // "\x1b[?"
  356. if ((lcd_escape[3] == '2') && (chr == '5')) return; // escape = "\x1b[?25"
  357. break;
  358. default:
  359. if (e_2_is_num) // escape = "\x1b[%1d"
  360. {
  361. if ((lcd_escape[3] == ';') && chr_is_num) return; // escape = "\x1b[%1d;%1d"
  362. else if (e_3_is_num && (chr == ';')) return; // escape = "\x1b[%2d;"
  363. }
  364. }
  365. break;
  366. case 5:
  367. switch (lcd_escape[2])
  368. {
  369. case '?':
  370. if ((lcd_escape[3] == '2') && (lcd_escape[4] == '5')) // escape = "\x1b[?25"
  371. switch (chr)
  372. {
  373. case 'h': // escape = "\x1b[?25h"
  374. lcd_cursor(); // CursorShow
  375. break;
  376. case 'l': // escape = "\x1b[?25l"
  377. lcd_no_cursor(); // CursorHide
  378. break;
  379. }
  380. break;
  381. default:
  382. if (e_2_is_num) // escape = "\x1b[%1d"
  383. {
  384. if ((lcd_escape[3] == ';') && e_4_is_num) // escape = "\x1b%1d;%1dH"
  385. {
  386. if (chr == 'H') // escape = "\x1b%1d;%1dH"
  387. lcd_set_cursor(e4_num, e2_num); // CursorHome
  388. else if (chr_is_num)
  389. return; // escape = "\x1b%1d;%2d"
  390. }
  391. else if (e_3_is_num && (lcd_escape[4] == ';') && chr_is_num)
  392. return; // escape = "\x1b%2d;%1d"
  393. }
  394. }
  395. break;
  396. case 6:
  397. if (e_2_is_num) // escape = "\x1b[%1d"
  398. {
  399. if ((lcd_escape[3] == ';') && e_4_is_num && e_5_is_num && (chr == 'H')) // escape = "\x1b%1d;%2dH"
  400. lcd_set_cursor(e45_num, e2_num); // CursorHome
  401. else if (e_3_is_num && (lcd_escape[4] == ';') && e_5_is_num) // escape = "\x1b%2d;%1d"
  402. {
  403. if (chr == 'H') // escape = "\x1b%2d;%1dH"
  404. lcd_set_cursor(e5_num, e23_num); // CursorHome
  405. else if (chr_is_num) // "\x1b%2d;%2d"
  406. return;
  407. }
  408. }
  409. break;
  410. case 7:
  411. if (e_2_is_num && e_3_is_num && (lcd_escape[4] == ';')) // "\x1b[%2d;"
  412. if (e_5_is_num && e_6_is_num && (chr == 'H')) // "\x1b[%2d;%2dH"
  413. lcd_set_cursor(e56_num, e23_num); // CursorHome
  414. break;
  415. }
  416. escape_cnt = 0; // reset escape
  417. }
  418. #endif //VT100
  419. int lcd_putc(int c)
  420. {
  421. return fputc(c, lcdout);
  422. }
  423. int lcd_puts_P(const char* str)
  424. {
  425. return fputs_P(str, lcdout);
  426. }
  427. int lcd_puts_at_P(uint8_t c, uint8_t r, const char* str)
  428. {
  429. lcd_set_cursor(c, r);
  430. return fputs_P(str, lcdout);
  431. }
  432. int lcd_printf_P(const char* format, ...)
  433. {
  434. va_list args;
  435. va_start(args, format);
  436. int ret = vfprintf_P(lcdout, format, args);
  437. va_end(args);
  438. return ret;
  439. }
  440. void lcd_space(uint8_t n)
  441. {
  442. while (n--) lcd_putc(' ');
  443. }
  444. void lcd_print(const char* s)
  445. {
  446. while (*s) lcd_write(*(s++));
  447. }
  448. void lcd_print(char c, int base)
  449. {
  450. lcd_print((long) c, base);
  451. }
  452. void lcd_print(unsigned char b, int base)
  453. {
  454. lcd_print((unsigned long) b, base);
  455. }
  456. void lcd_print(int n, int base)
  457. {
  458. lcd_print((long) n, base);
  459. }
  460. void lcd_print(unsigned int n, int base)
  461. {
  462. lcd_print((unsigned long) n, base);
  463. }
  464. void lcd_print(long n, int base)
  465. {
  466. if (base == 0)
  467. lcd_write(n);
  468. else if (base == 10)
  469. {
  470. if (n < 0)
  471. {
  472. lcd_print('-');
  473. n = -n;
  474. }
  475. lcd_printNumber(n, 10);
  476. }
  477. else
  478. lcd_printNumber(n, base);
  479. }
  480. void lcd_print(unsigned long n, int base)
  481. {
  482. if (base == 0)
  483. lcd_write(n);
  484. else
  485. lcd_printNumber(n, base);
  486. }
  487. void lcd_print(double n, int digits)
  488. {
  489. lcd_printFloat(n, digits);
  490. }
  491. void lcd_printNumber(unsigned long n, uint8_t base)
  492. {
  493. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  494. unsigned long i = 0;
  495. if (n == 0)
  496. {
  497. lcd_print('0');
  498. return;
  499. }
  500. while (n > 0)
  501. {
  502. buf[i++] = n % base;
  503. n /= base;
  504. }
  505. for (; i > 0; i--)
  506. lcd_print((char) (buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
  507. }
  508. void lcd_printFloat(double number, uint8_t digits)
  509. {
  510. // Handle negative numbers
  511. if (number < 0.0)
  512. {
  513. lcd_print('-');
  514. number = -number;
  515. }
  516. // Round correctly so that print(1.999, 2) prints as "2.00"
  517. double rounding = 0.5;
  518. for (uint8_t i=0; i<digits; ++i)
  519. rounding /= 10.0;
  520. number += rounding;
  521. // Extract the integer part of the number and print it
  522. unsigned long int_part = (unsigned long)number;
  523. double remainder = number - (double)int_part;
  524. lcd_print(int_part);
  525. // Print the decimal point, but only if there are digits beyond
  526. if (digits > 0)
  527. lcd_print('.');
  528. // Extract digits from the remainder one at a time
  529. while (digits-- > 0)
  530. {
  531. remainder *= 10.0;
  532. int toPrint = int(remainder);
  533. lcd_print(toPrint);
  534. remainder -= toPrint;
  535. }
  536. }
  537. uint8_t lcd_draw_update = 2;
  538. int32_t lcd_encoder = 0;
  539. uint8_t lcd_encoder_bits = 0;
  540. int8_t lcd_encoder_diff = 0;
  541. uint8_t lcd_buttons = 0;
  542. uint8_t lcd_button_pressed = 0;
  543. uint8_t lcd_update_enabled = 1;
  544. uint32_t lcd_next_update_millis = 0;
  545. uint8_t lcd_status_update_delay = 0;
  546. lcd_longpress_func_t lcd_longpress_func = 0;
  547. lcd_charsetup_func_t lcd_charsetup_func = 0;
  548. lcd_lcdupdate_func_t lcd_lcdupdate_func = 0;
  549. static ShortTimer buttonBlanking;
  550. ShortTimer longPressTimer;
  551. LongTimer lcd_timeoutToStatus;
  552. //! @brief Was button clicked?
  553. //!
  554. //! Consume click event, following call would return 0.
  555. //! See #LCD_CLICKED macro for version not consuming the event.
  556. //!
  557. //! Generally is used in modal dialogs.
  558. //!
  559. //! @retval 0 not clicked
  560. //! @retval nonzero clicked
  561. uint8_t lcd_clicked(void)
  562. {
  563. bool clicked = LCD_CLICKED;
  564. if(clicked)
  565. {
  566. lcd_consume_click();
  567. }
  568. return clicked;
  569. }
  570. void lcd_beeper_quick_feedback(void)
  571. {
  572. //-//
  573. Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
  574. /*
  575. for(int8_t i = 0; i < 10; i++)
  576. {
  577. Sound_MakeCustom(100,0,false);
  578. _delay_us(100);
  579. }
  580. */
  581. }
  582. void lcd_quick_feedback(void)
  583. {
  584. lcd_draw_update = 2;
  585. lcd_button_pressed = false;
  586. lcd_beeper_quick_feedback();
  587. }
  588. void lcd_update(uint8_t lcdDrawUpdateOverride)
  589. {
  590. if (lcd_draw_update < lcdDrawUpdateOverride)
  591. lcd_draw_update = lcdDrawUpdateOverride;
  592. if (!lcd_update_enabled)
  593. return;
  594. if (lcd_lcdupdate_func)
  595. lcd_lcdupdate_func();
  596. }
  597. void lcd_update_enable(uint8_t enabled)
  598. {
  599. if (lcd_update_enabled != enabled)
  600. {
  601. lcd_update_enabled = enabled;
  602. if (enabled)
  603. { // Reset encoder position. This is equivalent to re-entering a menu.
  604. lcd_encoder = 0;
  605. lcd_encoder_diff = 0;
  606. // Enabling the normal LCD update procedure.
  607. // Reset the timeout interval.
  608. lcd_timeoutToStatus.start();
  609. // Force the keypad update now.
  610. lcd_next_update_millis = _millis() - 1;
  611. // Full update.
  612. lcd_clear();
  613. if (lcd_charsetup_func)
  614. lcd_charsetup_func();
  615. lcd_update(2);
  616. } else
  617. {
  618. // Clear the LCD always, or let it to the caller?
  619. }
  620. }
  621. }
  622. void lcd_buttons_update(void)
  623. {
  624. static uint8_t lcd_long_press_active = 0;
  625. uint8_t newbutton = 0;
  626. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  627. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  628. if (READ(BTN_ENC) == 0)
  629. { //button is pressed
  630. lcd_timeoutToStatus.start();
  631. if (!buttonBlanking.running() || buttonBlanking.expired(BUTTON_BLANKING_TIME)) {
  632. buttonBlanking.start();
  633. safetyTimer.start();
  634. if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
  635. {
  636. longPressTimer.start();
  637. lcd_button_pressed = 1;
  638. }
  639. else if (longPressTimer.expired(LONG_PRESS_TIME))
  640. {
  641. lcd_long_press_active = 1;
  642. //long press is not possible in modal mode
  643. if (lcd_longpress_func && lcd_update_enabled)
  644. lcd_longpress_func();
  645. }
  646. }
  647. }
  648. else
  649. { //button not pressed
  650. if (lcd_button_pressed)
  651. { //button was released
  652. buttonBlanking.start();
  653. if (lcd_long_press_active == 0)
  654. { //button released before long press gets activated
  655. newbutton |= EN_C;
  656. }
  657. //else if (menu_menu == lcd_move_z) lcd_quick_feedback();
  658. //lcd_button_pressed is set back to false via lcd_quick_feedback function
  659. }
  660. else
  661. lcd_long_press_active = 0;
  662. }
  663. lcd_buttons = newbutton;
  664. //manage encoder rotation
  665. uint8_t enc = 0;
  666. if (lcd_buttons & EN_A) enc |= B01;
  667. if (lcd_buttons & EN_B) enc |= B10;
  668. if (enc != lcd_encoder_bits)
  669. {
  670. switch (enc)
  671. {
  672. case encrot0:
  673. if (lcd_encoder_bits == encrot3)
  674. lcd_encoder_diff++;
  675. else if (lcd_encoder_bits == encrot1)
  676. lcd_encoder_diff--;
  677. break;
  678. case encrot1:
  679. if (lcd_encoder_bits == encrot0)
  680. lcd_encoder_diff++;
  681. else if (lcd_encoder_bits == encrot2)
  682. lcd_encoder_diff--;
  683. break;
  684. case encrot2:
  685. if (lcd_encoder_bits == encrot1)
  686. lcd_encoder_diff++;
  687. else if (lcd_encoder_bits == encrot3)
  688. lcd_encoder_diff--;
  689. break;
  690. case encrot3:
  691. if (lcd_encoder_bits == encrot2)
  692. lcd_encoder_diff++;
  693. else if (lcd_encoder_bits == encrot0)
  694. lcd_encoder_diff--;
  695. break;
  696. }
  697. }
  698. lcd_encoder_bits = enc;
  699. }
  700. ////////////////////////////////////////////////////////////////////////////////
  701. // Custom character data
  702. const uint8_t lcd_chardata_bedTemp[8] PROGMEM = {
  703. B00000,
  704. B11111,
  705. B10101,
  706. B10001,
  707. B10101,
  708. B11111,
  709. B00000,
  710. B00000}; //thanks Sonny Mounicou
  711. const uint8_t lcd_chardata_degree[8] PROGMEM = {
  712. B01100,
  713. B10010,
  714. B10010,
  715. B01100,
  716. B00000,
  717. B00000,
  718. B00000,
  719. B00000};
  720. const uint8_t lcd_chardata_thermometer[8] PROGMEM = {
  721. B00100,
  722. B01010,
  723. B01010,
  724. B01010,
  725. B01010,
  726. B10001,
  727. B10001,
  728. B01110};
  729. const uint8_t lcd_chardata_uplevel[8] PROGMEM = {
  730. B00100,
  731. B01110,
  732. B11111,
  733. B00100,
  734. B11100,
  735. B00000,
  736. B00000,
  737. B00000}; //thanks joris
  738. const uint8_t lcd_chardata_refresh[8] PROGMEM = {
  739. B00000,
  740. B00110,
  741. B11001,
  742. B11000,
  743. B00011,
  744. B10011,
  745. B01100,
  746. B00000}; //thanks joris
  747. const uint8_t lcd_chardata_folder[8] PROGMEM = {
  748. B00000,
  749. B11100,
  750. B11111,
  751. B10001,
  752. B10001,
  753. B11111,
  754. B00000,
  755. B00000}; //thanks joris
  756. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  757. B11100,
  758. B10000,
  759. B11000,
  760. B10111,
  761. B00101,
  762. B00110,
  763. B00101,
  764. B00000};*/ //thanks Sonny Mounicou
  765. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  766. B11100,
  767. B10100,
  768. B11000,
  769. B10100,
  770. B00000,
  771. B00111,
  772. B00010,
  773. B00010};*/
  774. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  775. B01100,
  776. B10011,
  777. B00000,
  778. B01100,
  779. B10011,
  780. B00000,
  781. B01100,
  782. B10011};*/
  783. const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  784. B00000,
  785. B00100,
  786. B10010,
  787. B01001,
  788. B10010,
  789. B00100,
  790. B00000,
  791. B00000};
  792. const uint8_t lcd_chardata_clock[8] PROGMEM = {
  793. B00000,
  794. B01110,
  795. B10011,
  796. B10101,
  797. B10001,
  798. B01110,
  799. B00000,
  800. B00000}; //thanks Sonny Mounicou
  801. const uint8_t lcd_chardata_arrup[8] PROGMEM = {
  802. B00100,
  803. B01110,
  804. B11111,
  805. B00000,
  806. B00000,
  807. B00000,
  808. B00000,
  809. B00000};
  810. const uint8_t lcd_chardata_arrdown[8] PROGMEM = {
  811. B00000,
  812. B00000,
  813. B00000,
  814. B00000,
  815. B00000,
  816. B10001,
  817. B01010,
  818. B00100};
  819. void lcd_set_custom_characters(void)
  820. {
  821. lcd_createChar_P(LCD_STR_BEDTEMP[0], lcd_chardata_bedTemp);
  822. lcd_createChar_P(LCD_STR_DEGREE[0], lcd_chardata_degree);
  823. lcd_createChar_P(LCD_STR_THERMOMETER[0], lcd_chardata_thermometer);
  824. lcd_createChar_P(LCD_STR_UPLEVEL[0], lcd_chardata_uplevel);
  825. lcd_createChar_P(LCD_STR_REFRESH[0], lcd_chardata_refresh);
  826. lcd_createChar_P(LCD_STR_FOLDER[0], lcd_chardata_folder);
  827. lcd_createChar_P(LCD_STR_FEEDRATE[0], lcd_chardata_feedrate);
  828. lcd_createChar_P(LCD_STR_CLOCK[0], lcd_chardata_clock);
  829. //lcd_createChar_P(LCD_STR_ARROW_UP[0], lcd_chardata_arrup);
  830. //lcd_createChar_P(LCD_STR_ARROW_DOWN[0], lcd_chardata_arrdown);
  831. }
  832. void lcd_set_custom_characters_arrows(void)
  833. {
  834. lcd_createChar_P(1, lcd_chardata_arrdown);
  835. }
  836. const uint8_t lcd_chardata_progress[8] PROGMEM = {
  837. B11111,
  838. B11111,
  839. B11111,
  840. B11111,
  841. B11111,
  842. B11111,
  843. B11111,
  844. B11111};
  845. void lcd_set_custom_characters_progress(void)
  846. {
  847. lcd_createChar_P(1, lcd_chardata_progress);
  848. }
  849. const uint8_t lcd_chardata_arr2down[8] PROGMEM = {
  850. B00000,
  851. B00000,
  852. B10001,
  853. B01010,
  854. B00100,
  855. B10001,
  856. B01010,
  857. B00100};
  858. const uint8_t lcd_chardata_confirm[8] PROGMEM = {
  859. B00000,
  860. B00001,
  861. B00011,
  862. B10110,
  863. B11100,
  864. B01000,
  865. B00000};
  866. void lcd_set_custom_characters_nextpage(void)
  867. {
  868. lcd_createChar_P(1, lcd_chardata_arr2down);
  869. lcd_createChar_P(2, lcd_chardata_confirm);
  870. }
  871. void lcd_set_custom_characters_degree(void)
  872. {
  873. lcd_createChar_P(1, lcd_chardata_degree);
  874. }