lcd.cpp 24 KB

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