lcd.cpp 23 KB

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