lcd.cpp 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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};
  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. void lcd_begin(uint8_t cols, 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 *stream)
  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_WIDTH, 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_WIDTH, LCD_HEIGHT, LCD_5x8DOTS, 1);
  230. lcd_set_custom_characters();
  231. }
  232. void lcd_refresh_noclear(void)
  233. {
  234. lcd_begin(LCD_WIDTH, 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. end:
  462. return 1; // assume sucess
  463. }
  464. int lcd_putc(int c)
  465. {
  466. return fputc(c, lcdout);
  467. }
  468. int lcd_puts_P(const char* str)
  469. {
  470. return fputs_P(str, lcdout);
  471. }
  472. int lcd_puts_at_P(uint8_t c, uint8_t r, const char* str)
  473. {
  474. lcd_set_cursor(c, r);
  475. return fputs_P(str, lcdout);
  476. }
  477. int lcd_printf_P(const char* format, ...)
  478. {
  479. va_list args;
  480. va_start(args, format);
  481. int ret = vfprintf_P(lcdout, format, args);
  482. va_end(args);
  483. return ret;
  484. }
  485. void lcd_space(uint8_t n)
  486. {
  487. while (n--) lcd_putc(' ');
  488. }
  489. void lcd_print(const char* s)
  490. {
  491. while (*s) lcd_write(*(s++));
  492. }
  493. void lcd_print(char c, int base)
  494. {
  495. lcd_print((long) c, base);
  496. }
  497. void lcd_print(unsigned char b, int base)
  498. {
  499. lcd_print((unsigned long) b, base);
  500. }
  501. void lcd_print(int n, int base)
  502. {
  503. lcd_print((long) n, base);
  504. }
  505. void lcd_print(unsigned int n, int base)
  506. {
  507. lcd_print((unsigned long) n, base);
  508. }
  509. void lcd_print(long n, int base)
  510. {
  511. if (base == 0)
  512. lcd_write(n);
  513. else if (base == 10)
  514. {
  515. if (n < 0)
  516. {
  517. lcd_print('-');
  518. n = -n;
  519. }
  520. lcd_printNumber(n, 10);
  521. }
  522. else
  523. lcd_printNumber(n, base);
  524. }
  525. void lcd_print(unsigned long n, int base)
  526. {
  527. if (base == 0)
  528. lcd_write(n);
  529. else
  530. lcd_printNumber(n, base);
  531. }
  532. void lcd_print(double n, int digits)
  533. {
  534. lcd_printFloat(n, digits);
  535. }
  536. void lcd_printNumber(unsigned long n, uint8_t base)
  537. {
  538. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  539. unsigned long i = 0;
  540. if (n == 0)
  541. {
  542. lcd_print('0');
  543. return;
  544. }
  545. while (n > 0)
  546. {
  547. buf[i++] = n % base;
  548. n /= base;
  549. }
  550. for (; i > 0; i--)
  551. lcd_print((char) (buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
  552. }
  553. void lcd_printFloat(double number, uint8_t digits)
  554. {
  555. // Handle negative numbers
  556. if (number < 0.0)
  557. {
  558. lcd_print('-');
  559. number = -number;
  560. }
  561. // Round correctly so that print(1.999, 2) prints as "2.00"
  562. double rounding = 0.5;
  563. for (uint8_t i=0; i<digits; ++i)
  564. rounding /= 10.0;
  565. number += rounding;
  566. // Extract the integer part of the number and print it
  567. unsigned long int_part = (unsigned long)number;
  568. double remainder = number - (double)int_part;
  569. lcd_print(int_part);
  570. // Print the decimal point, but only if there are digits beyond
  571. if (digits > 0)
  572. lcd_print('.');
  573. // Extract digits from the remainder one at a time
  574. while (digits-- > 0)
  575. {
  576. remainder *= 10.0;
  577. int toPrint = int(remainder);
  578. lcd_print(toPrint);
  579. remainder -= toPrint;
  580. }
  581. }
  582. uint8_t lcd_draw_update = 2;
  583. int32_t lcd_encoder = 0;
  584. uint8_t lcd_encoder_bits = 0;
  585. int8_t lcd_encoder_diff = 0;
  586. uint8_t lcd_buttons = 0;
  587. uint8_t lcd_button_pressed = 0;
  588. uint8_t lcd_update_enabled = 1;
  589. uint32_t lcd_next_update_millis = 0;
  590. uint8_t lcd_status_update_delay = 0;
  591. uint8_t lcd_long_press_active = 0;
  592. lcd_longpress_func_t lcd_longpress_func = 0;
  593. lcd_charsetup_func_t lcd_charsetup_func = 0;
  594. lcd_lcdupdate_func_t lcd_lcdupdate_func = 0;
  595. static ShortTimer buttonBlanking;
  596. ShortTimer longPressTimer;
  597. LongTimer lcd_timeoutToStatus;
  598. uint8_t lcd_clicked(void)
  599. {
  600. bool clicked = LCD_CLICKED;
  601. if(clicked) lcd_button_pressed = 0;
  602. return clicked;
  603. }
  604. void lcd_beeper_quick_feedback(void)
  605. {
  606. SET_OUTPUT(BEEPER);
  607. //-//
  608. Sound_MakeSound(e_SOUND_CLASS_Echo,e_SOUND_TYPE_ButtonEcho);
  609. /*
  610. for(int8_t i = 0; i < 10; i++)
  611. {
  612. WRITE(BEEPER,HIGH);
  613. delayMicroseconds(100);
  614. WRITE(BEEPER,LOW);
  615. delayMicroseconds(100);
  616. }
  617. */
  618. }
  619. void lcd_quick_feedback(void)
  620. {
  621. lcd_draw_update = 2;
  622. lcd_button_pressed = false;
  623. lcd_beeper_quick_feedback();
  624. }
  625. void lcd_update(uint8_t lcdDrawUpdateOverride)
  626. {
  627. if (lcd_draw_update < lcdDrawUpdateOverride)
  628. lcd_draw_update = lcdDrawUpdateOverride;
  629. if (!lcd_update_enabled)
  630. return;
  631. lcd_buttons_update();
  632. if (lcd_lcdupdate_func)
  633. lcd_lcdupdate_func();
  634. }
  635. void lcd_update_enable(uint8_t enabled)
  636. {
  637. if (lcd_update_enabled != enabled)
  638. {
  639. lcd_update_enabled = enabled;
  640. if (enabled)
  641. { // Reset encoder position. This is equivalent to re-entering a menu.
  642. lcd_encoder = 0;
  643. lcd_encoder_diff = 0;
  644. // Enabling the normal LCD update procedure.
  645. // Reset the timeout interval.
  646. lcd_timeoutToStatus.start();
  647. // Force the keypad update now.
  648. lcd_next_update_millis = millis() - 1;
  649. // Full update.
  650. lcd_clear();
  651. if (lcd_charsetup_func)
  652. lcd_charsetup_func();
  653. lcd_update(2);
  654. } else
  655. {
  656. // Clear the LCD always, or let it to the caller?
  657. }
  658. }
  659. }
  660. extern LongTimer safetyTimer;
  661. void lcd_buttons_update(void)
  662. {
  663. static bool _lock = false;
  664. if (_lock) return;
  665. _lock = true;
  666. uint8_t newbutton = 0;
  667. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  668. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  669. if (lcd_update_enabled)
  670. { //if we are in non-modal mode, long press can be used and short press triggers with button release
  671. if (READ(BTN_ENC) == 0)
  672. { //button is pressed
  673. lcd_timeoutToStatus.start();
  674. if (!buttonBlanking.running() || buttonBlanking.expired(BUTTON_BLANKING_TIME)) {
  675. buttonBlanking.start();
  676. safetyTimer.start();
  677. if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
  678. {
  679. longPressTimer.start();
  680. lcd_button_pressed = 1;
  681. }
  682. else
  683. {
  684. if (longPressTimer.expired(LONG_PRESS_TIME))
  685. {
  686. lcd_long_press_active = 1;
  687. if (lcd_longpress_func)
  688. lcd_longpress_func();
  689. }
  690. }
  691. }
  692. }
  693. else
  694. { //button not pressed
  695. if (lcd_button_pressed)
  696. { //button was released
  697. buttonBlanking.start();
  698. if (lcd_long_press_active == 0)
  699. { //button released before long press gets activated
  700. newbutton |= EN_C;
  701. }
  702. //else if (menu_menu == lcd_move_z) lcd_quick_feedback();
  703. //lcd_button_pressed is set back to false via lcd_quick_feedback function
  704. }
  705. else
  706. lcd_long_press_active = 0;
  707. }
  708. }
  709. else
  710. { //we are in modal mode
  711. if (READ(BTN_ENC) == 0)
  712. newbutton |= EN_C;
  713. }
  714. lcd_buttons = newbutton;
  715. //manage encoder rotation
  716. uint8_t enc = 0;
  717. if (lcd_buttons & EN_A) enc |= B01;
  718. if (lcd_buttons & EN_B) enc |= B10;
  719. if (enc != lcd_encoder_bits)
  720. {
  721. switch (enc)
  722. {
  723. case encrot0:
  724. if (lcd_encoder_bits == encrot3)
  725. lcd_encoder_diff++;
  726. else if (lcd_encoder_bits == encrot1)
  727. lcd_encoder_diff--;
  728. break;
  729. case encrot1:
  730. if (lcd_encoder_bits == encrot0)
  731. lcd_encoder_diff++;
  732. else if (lcd_encoder_bits == encrot2)
  733. lcd_encoder_diff--;
  734. break;
  735. case encrot2:
  736. if (lcd_encoder_bits == encrot1)
  737. lcd_encoder_diff++;
  738. else if (lcd_encoder_bits == encrot3)
  739. lcd_encoder_diff--;
  740. break;
  741. case encrot3:
  742. if (lcd_encoder_bits == encrot2)
  743. lcd_encoder_diff++;
  744. else if (lcd_encoder_bits == encrot0)
  745. lcd_encoder_diff--;
  746. break;
  747. }
  748. }
  749. lcd_encoder_bits = enc;
  750. _lock = false;
  751. }
  752. ////////////////////////////////////////////////////////////////////////////////
  753. // Custom character data
  754. const uint8_t lcd_chardata_bedTemp[8] PROGMEM = {
  755. B00000,
  756. B11111,
  757. B10101,
  758. B10001,
  759. B10101,
  760. B11111,
  761. B00000,
  762. B00000}; //thanks Sonny Mounicou
  763. const uint8_t lcd_chardata_degree[8] PROGMEM = {
  764. B01100,
  765. B10010,
  766. B10010,
  767. B01100,
  768. B00000,
  769. B00000,
  770. B00000,
  771. B00000};
  772. const uint8_t lcd_chardata_thermometer[8] PROGMEM = {
  773. B00100,
  774. B01010,
  775. B01010,
  776. B01010,
  777. B01010,
  778. B10001,
  779. B10001,
  780. B01110};
  781. const uint8_t lcd_chardata_uplevel[8] PROGMEM = {
  782. B00100,
  783. B01110,
  784. B11111,
  785. B00100,
  786. B11100,
  787. B00000,
  788. B00000,
  789. B00000}; //thanks joris
  790. const uint8_t lcd_chardata_refresh[8] PROGMEM = {
  791. B00000,
  792. B00110,
  793. B11001,
  794. B11000,
  795. B00011,
  796. B10011,
  797. B01100,
  798. B00000}; //thanks joris
  799. const uint8_t lcd_chardata_folder[8] PROGMEM = {
  800. B00000,
  801. B11100,
  802. B11111,
  803. B10001,
  804. B10001,
  805. B11111,
  806. B00000,
  807. B00000}; //thanks joris
  808. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  809. B11100,
  810. B10000,
  811. B11000,
  812. B10111,
  813. B00101,
  814. B00110,
  815. B00101,
  816. B00000};*/ //thanks Sonny Mounicou
  817. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  818. B11100,
  819. B10100,
  820. B11000,
  821. B10100,
  822. B00000,
  823. B00111,
  824. B00010,
  825. B00010};*/
  826. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  827. B01100,
  828. B10011,
  829. B00000,
  830. B01100,
  831. B10011,
  832. B00000,
  833. B01100,
  834. B10011};*/
  835. const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  836. B00000,
  837. B00100,
  838. B10010,
  839. B01001,
  840. B10010,
  841. B00100,
  842. B00000,
  843. B00000};
  844. const uint8_t lcd_chardata_clock[8] PROGMEM = {
  845. B00000,
  846. B01110,
  847. B10011,
  848. B10101,
  849. B10001,
  850. B01110,
  851. B00000,
  852. B00000}; //thanks Sonny Mounicou
  853. const uint8_t lcd_chardata_arrup[8] PROGMEM = {
  854. B00100,
  855. B01110,
  856. B11111,
  857. B00000,
  858. B00000,
  859. B00000,
  860. B00000,
  861. B00000};
  862. const uint8_t lcd_chardata_arrdown[8] PROGMEM = {
  863. B00000,
  864. B00000,
  865. B00000,
  866. B00000,
  867. B00000,
  868. B10001,
  869. B01010,
  870. B00100};
  871. void lcd_set_custom_characters(void)
  872. {
  873. lcd_createChar_P(LCD_STR_BEDTEMP[0], lcd_chardata_bedTemp);
  874. lcd_createChar_P(LCD_STR_DEGREE[0], lcd_chardata_degree);
  875. lcd_createChar_P(LCD_STR_THERMOMETER[0], lcd_chardata_thermometer);
  876. lcd_createChar_P(LCD_STR_UPLEVEL[0], lcd_chardata_uplevel);
  877. lcd_createChar_P(LCD_STR_REFRESH[0], lcd_chardata_refresh);
  878. lcd_createChar_P(LCD_STR_FOLDER[0], lcd_chardata_folder);
  879. lcd_createChar_P(LCD_STR_FEEDRATE[0], lcd_chardata_feedrate);
  880. lcd_createChar_P(LCD_STR_CLOCK[0], lcd_chardata_clock);
  881. //lcd_createChar_P(LCD_STR_ARROW_UP[0], lcd_chardata_arrup);
  882. //lcd_createChar_P(LCD_STR_ARROW_DOWN[0], lcd_chardata_arrdown);
  883. }
  884. void lcd_set_custom_characters_arrows(void)
  885. {
  886. lcd_createChar_P(1, lcd_chardata_arrdown);
  887. }
  888. const uint8_t lcd_chardata_progress[8] PROGMEM = {
  889. B11111,
  890. B11111,
  891. B11111,
  892. B11111,
  893. B11111,
  894. B11111,
  895. B11111,
  896. B11111};
  897. void lcd_set_custom_characters_progress(void)
  898. {
  899. lcd_createChar_P(1, lcd_chardata_progress);
  900. }
  901. const uint8_t lcd_chardata_arr2down[8] PROGMEM = {
  902. B00000,
  903. B00000,
  904. B10001,
  905. B01010,
  906. B00100,
  907. B10001,
  908. B01010,
  909. B00100};
  910. const uint8_t lcd_chardata_confirm[8] PROGMEM = {
  911. B00000,
  912. B00001,
  913. B00011,
  914. B10110,
  915. B11100,
  916. B01000,
  917. B00000};
  918. void lcd_set_custom_characters_nextpage(void)
  919. {
  920. lcd_createChar_P(1, lcd_chardata_arr2down);
  921. lcd_createChar_P(2, lcd_chardata_confirm);
  922. }
  923. void lcd_set_custom_characters_degree(void)
  924. {
  925. lcd_createChar_P(1, lcd_chardata_degree);
  926. }