lcd.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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_next_update_millis = 0;
  584. uint8_t lcd_status_update_delay = 0;
  585. uint8_t lcd_long_press_active = 0;
  586. lcd_longpress_func_t lcd_longpress_func = 0;
  587. lcd_charsetup_func_t lcd_charsetup_func = 0;
  588. lcd_lcdupdate_func_t lcd_lcdupdate_func = 0;
  589. static ShortTimer buttonBlanking;
  590. ShortTimer longPressTimer;
  591. LongTimer lcd_timeoutToStatus;
  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.start();
  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.start();
  663. if (!buttonBlanking.running() || buttonBlanking.expired(BUTTON_BLANKING_TIME)) {
  664. buttonBlanking.start();
  665. if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
  666. {
  667. longPressTimer.start();
  668. lcd_button_pressed = 1;
  669. }
  670. else
  671. {
  672. if (longPressTimer.expired(LONG_PRESS_TIME))
  673. {
  674. lcd_long_press_active = 1;
  675. if (lcd_longpress_func)
  676. lcd_longpress_func();
  677. }
  678. }
  679. }
  680. }
  681. else
  682. { //button not pressed
  683. if (lcd_button_pressed)
  684. { //button was released
  685. buttonBlanking.start();
  686. if (lcd_long_press_active == 0)
  687. { //button released before long press gets activated
  688. newbutton |= EN_C;
  689. }
  690. //else if (menu_menu == lcd_move_z) lcd_quick_feedback();
  691. //lcd_button_pressed is set back to false via lcd_quick_feedback function
  692. }
  693. else
  694. lcd_long_press_active = 0;
  695. }
  696. }
  697. else
  698. { //we are in modal mode
  699. if (READ(BTN_ENC) == 0)
  700. newbutton |= EN_C;
  701. }
  702. lcd_buttons = newbutton;
  703. //manage encoder rotation
  704. uint8_t enc = 0;
  705. if (lcd_buttons & EN_A) enc |= B01;
  706. if (lcd_buttons & EN_B) enc |= B10;
  707. if (enc != lcd_encoder_bits)
  708. {
  709. switch (enc)
  710. {
  711. case encrot0:
  712. if (lcd_encoder_bits == encrot3)
  713. lcd_encoder_diff++;
  714. else if (lcd_encoder_bits == encrot1)
  715. lcd_encoder_diff--;
  716. break;
  717. case encrot1:
  718. if (lcd_encoder_bits == encrot0)
  719. lcd_encoder_diff++;
  720. else if (lcd_encoder_bits == encrot2)
  721. lcd_encoder_diff--;
  722. break;
  723. case encrot2:
  724. if (lcd_encoder_bits == encrot1)
  725. lcd_encoder_diff++;
  726. else if (lcd_encoder_bits == encrot3)
  727. lcd_encoder_diff--;
  728. break;
  729. case encrot3:
  730. if (lcd_encoder_bits == encrot2)
  731. lcd_encoder_diff++;
  732. else if (lcd_encoder_bits == encrot0)
  733. lcd_encoder_diff--;
  734. break;
  735. }
  736. }
  737. lcd_encoder_bits = enc;
  738. _lock = false;
  739. }
  740. ////////////////////////////////////////////////////////////////////////////////
  741. // Custom character data
  742. const uint8_t lcd_chardata_bedTemp[8] PROGMEM = {
  743. B00000,
  744. B11111,
  745. B10101,
  746. B10001,
  747. B10101,
  748. B11111,
  749. B00000,
  750. B00000}; //thanks Sonny Mounicou
  751. const uint8_t lcd_chardata_degree[8] PROGMEM = {
  752. B01100,
  753. B10010,
  754. B10010,
  755. B01100,
  756. B00000,
  757. B00000,
  758. B00000,
  759. B00000};
  760. const uint8_t lcd_chardata_thermometer[8] PROGMEM = {
  761. B00100,
  762. B01010,
  763. B01010,
  764. B01010,
  765. B01010,
  766. B10001,
  767. B10001,
  768. B01110};
  769. const uint8_t lcd_chardata_uplevel[8] PROGMEM = {
  770. B00100,
  771. B01110,
  772. B11111,
  773. B00100,
  774. B11100,
  775. B00000,
  776. B00000,
  777. B00000}; //thanks joris
  778. const uint8_t lcd_chardata_refresh[8] PROGMEM = {
  779. B00000,
  780. B00110,
  781. B11001,
  782. B11000,
  783. B00011,
  784. B10011,
  785. B01100,
  786. B00000}; //thanks joris
  787. const uint8_t lcd_chardata_folder[8] PROGMEM = {
  788. B00000,
  789. B11100,
  790. B11111,
  791. B10001,
  792. B10001,
  793. B11111,
  794. B00000,
  795. B00000}; //thanks joris
  796. const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  797. B11100,
  798. B10000,
  799. B11000,
  800. B10111,
  801. B00101,
  802. B00110,
  803. B00101,
  804. B00000}; //thanks Sonny Mounicou
  805. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  806. B11100,
  807. B10100,
  808. B11000,
  809. B10100,
  810. B00000,
  811. B00111,
  812. B00010,
  813. B00010};*/
  814. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  815. B01100,
  816. B10011,
  817. B00000,
  818. B01100,
  819. B10011,
  820. B00000,
  821. B01100,
  822. B10011};*/
  823. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  824. B00000,
  825. B00100,
  826. B10010,
  827. B01001,
  828. B10010,
  829. B00100,
  830. B00000,
  831. B00000};*/
  832. const uint8_t lcd_chardata_clock[8] PROGMEM = {
  833. B00000,
  834. B01110,
  835. B10011,
  836. B10101,
  837. B10001,
  838. B01110,
  839. B00000,
  840. B00000}; //thanks Sonny Mounicou
  841. const uint8_t lcd_chardata_arrup[8] PROGMEM = {
  842. B00100,
  843. B01110,
  844. B11111,
  845. B00000,
  846. B00000,
  847. B00000,
  848. B00000,
  849. B00000};
  850. const uint8_t lcd_chardata_arrdown[8] PROGMEM = {
  851. B00000,
  852. B00000,
  853. B00000,
  854. B00000,
  855. B00000,
  856. B10001,
  857. B01010,
  858. B00100};
  859. void lcd_set_custom_characters(void)
  860. {
  861. lcd_createChar_P(LCD_STR_BEDTEMP[0], lcd_chardata_bedTemp);
  862. lcd_createChar_P(LCD_STR_DEGREE[0], lcd_chardata_degree);
  863. lcd_createChar_P(LCD_STR_THERMOMETER[0], lcd_chardata_thermometer);
  864. lcd_createChar_P(LCD_STR_UPLEVEL[0], lcd_chardata_uplevel);
  865. lcd_createChar_P(LCD_STR_REFRESH[0], lcd_chardata_refresh);
  866. lcd_createChar_P(LCD_STR_FOLDER[0], lcd_chardata_folder);
  867. lcd_createChar_P(LCD_STR_FEEDRATE[0], lcd_chardata_feedrate);
  868. lcd_createChar_P(LCD_STR_CLOCK[0], lcd_chardata_clock);
  869. //lcd_createChar_P(LCD_STR_ARROW_UP[0], lcd_chardata_arrup);
  870. //lcd_createChar_P(LCD_STR_ARROW_DOWN[0], lcd_chardata_arrdown);
  871. }
  872. void lcd_set_custom_characters_arrows(void)
  873. {
  874. lcd_createChar_P(1, lcd_chardata_arrdown);
  875. }
  876. const uint8_t lcd_chardata_progress[8] PROGMEM = {
  877. B11111,
  878. B11111,
  879. B11111,
  880. B11111,
  881. B11111,
  882. B11111,
  883. B11111,
  884. B11111};
  885. void lcd_set_custom_characters_progress(void)
  886. {
  887. lcd_createChar_P(1, lcd_chardata_progress);
  888. }
  889. const uint8_t lcd_chardata_arr2down[8] PROGMEM = {
  890. B00000,
  891. B00000,
  892. B10001,
  893. B01010,
  894. B00100,
  895. B10001,
  896. B01010,
  897. B00100};
  898. const uint8_t lcd_chardata_confirm[8] PROGMEM = {
  899. B00000,
  900. B00001,
  901. B00011,
  902. B10110,
  903. B11100,
  904. B01000,
  905. B00000};
  906. void lcd_set_custom_characters_nextpage(void)
  907. {
  908. lcd_createChar_P(1, lcd_chardata_arr2down);
  909. lcd_createChar_P(2, lcd_chardata_confirm);
  910. }
  911. void lcd_set_custom_characters_degree(void)
  912. {
  913. lcd_createChar_P(1, lcd_chardata_degree);
  914. }