lcd.cpp 23 KB

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