lcd.cpp 22 KB

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