lcd.cpp 23 KB

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