lcd.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. //menu.cpp
  2. #include "lcd.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <avr/pgmspace.h>
  6. #include "Timer.h"
  7. extern FILE _lcdout;
  8. #define lcdout (&_lcdout)
  9. void lcd_command(uint8_t value)
  10. {
  11. lcd.command(value);
  12. }
  13. uint8_t lcd_write(uint8_t value)
  14. {
  15. return lcd.write(value);
  16. }
  17. void lcd_clear(void)
  18. {
  19. lcd.clear();
  20. }
  21. void lcd_set_cursor(uint8_t c, uint8_t r)
  22. {
  23. // lcd_printf_P(PSTR("\x1b[%hhu;%hhuH"), r, c);
  24. lcd.setCursor(c, r);
  25. }
  26. int lcd_putc(int c)
  27. {
  28. return fputc(c, lcdout);
  29. }
  30. int lcd_puts_P(const char* str)
  31. {
  32. return fputs_P(str, lcdout);
  33. }
  34. int lcd_puts_at_P(uint8_t c, uint8_t r, const char* str)
  35. {
  36. lcd_set_cursor(c, r);
  37. return fputs_P(str, lcdout);
  38. }
  39. int lcd_printf_P(const char* format, ...)
  40. {
  41. va_list args;
  42. va_start(args, format);
  43. int ret = vfprintf_P(lcdout, format, args);
  44. va_end(args);
  45. return ret;
  46. }
  47. uint8_t lcd_draw_update = 2;
  48. int32_t lcd_encoder = 0;
  49. uint8_t lcd_encoder_bits = 0;
  50. int8_t lcd_encoder_diff = 0;
  51. uint8_t lcd_buttons = 0;
  52. uint8_t lcd_button_pressed = 0;
  53. uint8_t lcd_update_enabled = 1;
  54. uint32_t lcd_timeoutToStatus = 0;
  55. uint32_t lcd_next_update_millis = 0;
  56. uint8_t lcd_status_update_delay = 0;
  57. uint8_t lcd_long_press_active = 0;
  58. lcd_longpress_func_t lcd_longpress_func = 0;
  59. lcd_charsetup_func_t lcd_charsetup_func = 0;
  60. lcd_lcdupdate_func_t lcd_lcdupdate_func = 0;
  61. uint32_t lcd_button_blanking_time = millis();
  62. ShortTimer longPressTimer;
  63. uint8_t lcd_clicked(void)
  64. {
  65. bool clicked = LCD_CLICKED;
  66. if(clicked) lcd_button_pressed = 1;
  67. return clicked;
  68. }
  69. void lcd_beeper_quick_feedback(void)
  70. {
  71. SET_OUTPUT(BEEPER);
  72. for(int8_t i = 0; i < 10; i++)
  73. {
  74. WRITE(BEEPER,HIGH);
  75. delayMicroseconds(100);
  76. WRITE(BEEPER,LOW);
  77. delayMicroseconds(100);
  78. }
  79. }
  80. void lcd_quick_feedback(void)
  81. {
  82. lcd_draw_update = 2;
  83. lcd_button_pressed = false;
  84. lcd_beeper_quick_feedback();
  85. }
  86. void lcd_update(uint8_t lcdDrawUpdateOverride)
  87. {
  88. if (lcd_draw_update < lcdDrawUpdateOverride)
  89. lcd_draw_update = lcdDrawUpdateOverride;
  90. if (!lcd_update_enabled)
  91. return;
  92. lcd_buttons_update();
  93. if (lcd_lcdupdate_func)
  94. lcd_lcdupdate_func();
  95. }
  96. void lcd_update_enable(uint8_t enabled)
  97. {
  98. if (lcd_update_enabled != enabled)
  99. {
  100. lcd_update_enabled = enabled;
  101. if (enabled)
  102. { // Reset encoder position. This is equivalent to re-entering a menu.
  103. lcd_encoder = 0;
  104. lcd_encoder_diff = 0;
  105. // Enabling the normal LCD update procedure.
  106. // Reset the timeout interval.
  107. lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  108. // Force the keypad update now.
  109. lcd_next_update_millis = millis() - 1;
  110. // Full update.
  111. lcd_clear();
  112. if (lcd_charsetup_func)
  113. lcd_charsetup_func();
  114. lcd_update(2);
  115. } else
  116. {
  117. // Clear the LCD always, or let it to the caller?
  118. }
  119. }
  120. }
  121. void lcd_buttons_update(void)
  122. {
  123. static bool _lock = false;
  124. if (_lock) return;
  125. _lock = true;
  126. uint8_t newbutton = 0;
  127. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  128. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  129. if (lcd_update_enabled)
  130. { //if we are in non-modal mode, long press can be used and short press triggers with button release
  131. if (READ(BTN_ENC) == 0)
  132. { //button is pressed
  133. lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  134. if (millis() > lcd_button_blanking_time)
  135. {
  136. lcd_button_blanking_time = millis() + BUTTON_BLANKING_TIME;
  137. if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
  138. {
  139. longPressTimer.start();
  140. lcd_button_pressed = 1;
  141. }
  142. else
  143. {
  144. if (longPressTimer.expired(LONG_PRESS_TIME))
  145. {
  146. lcd_long_press_active = 1;
  147. if (lcd_longpress_func)
  148. lcd_longpress_func();
  149. }
  150. }
  151. }
  152. }
  153. else
  154. { //button not pressed
  155. if (lcd_button_pressed)
  156. { //button was released
  157. lcd_button_blanking_time = millis() + BUTTON_BLANKING_TIME;
  158. if (lcd_long_press_active == 0)
  159. { //button released before long press gets activated
  160. newbutton |= EN_C;
  161. }
  162. //else if (menu_menu == lcd_move_z) lcd_quick_feedback();
  163. //lcd_button_pressed is set back to false via lcd_quick_feedback function
  164. }
  165. else
  166. lcd_long_press_active = 0;
  167. }
  168. }
  169. else
  170. { //we are in modal mode
  171. if (READ(BTN_ENC) == 0)
  172. newbutton |= EN_C;
  173. }
  174. lcd_buttons = newbutton;
  175. //manage encoder rotation
  176. uint8_t enc = 0;
  177. if (lcd_buttons & EN_A) enc |= B01;
  178. if (lcd_buttons & EN_B) enc |= B10;
  179. if (enc != lcd_encoder_bits)
  180. {
  181. switch (enc)
  182. {
  183. case encrot0:
  184. if (lcd_encoder_bits == encrot3)
  185. lcd_encoder_diff++;
  186. else if (lcd_encoder_bits == encrot1)
  187. lcd_encoder_diff--;
  188. break;
  189. case encrot1:
  190. if (lcd_encoder_bits == encrot0)
  191. lcd_encoder_diff++;
  192. else if (lcd_encoder_bits == encrot2)
  193. lcd_encoder_diff--;
  194. break;
  195. case encrot2:
  196. if (lcd_encoder_bits == encrot1)
  197. lcd_encoder_diff++;
  198. else if (lcd_encoder_bits == encrot3)
  199. lcd_encoder_diff--;
  200. break;
  201. case encrot3:
  202. if (lcd_encoder_bits == encrot2)
  203. lcd_encoder_diff++;
  204. else if (lcd_encoder_bits == encrot0)
  205. lcd_encoder_diff--;
  206. break;
  207. }
  208. }
  209. lcd_encoder_bits = enc;
  210. _lock = false;
  211. }
  212. LCD_CLASS 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
  213. void lcd_implementation_init(void)
  214. {
  215. lcd.begin(LCD_WIDTH, LCD_HEIGHT);
  216. lcd_set_custom_characters();
  217. lcd.clear();
  218. }
  219. void lcd_implementation_init_noclear(void)
  220. {
  221. lcd.begin_noclear(LCD_WIDTH, LCD_HEIGHT);
  222. lcd_set_custom_characters();
  223. }
  224. /* Arduino < 1.0.0 is missing a function to print PROGMEM strings, so we need to implement our own */
  225. void lcd_printPGM(const char* str)
  226. {
  227. char c;
  228. while((c = pgm_read_byte(str++)) != '\0')
  229. {
  230. lcd.write(c);
  231. }
  232. }
  233. void lcd_print_at_PGM(uint8_t x, uint8_t y, const char* str)
  234. {
  235. lcd.setCursor(x, y);
  236. char c;
  237. while((c = pgm_read_byte(str++)) != '\0')
  238. {
  239. lcd.write(c);
  240. }
  241. }
  242. void lcd_implementation_write(char c)
  243. {
  244. lcd.write(c);
  245. }
  246. void lcd_print(int8_t i)
  247. {
  248. lcd.print(i);
  249. }
  250. void lcd_print_at(uint8_t x, uint8_t y, int8_t i)
  251. {
  252. lcd.setCursor(x, y);
  253. lcd.print(i);
  254. }
  255. void lcd_print(int i)
  256. {
  257. lcd.print(i);
  258. }
  259. void lcd_print_at(uint8_t x, uint8_t y, int i)
  260. {
  261. lcd.setCursor(x, y);
  262. lcd.print(i);
  263. }
  264. void lcd_print(float f)
  265. {
  266. lcd.print(f);
  267. }
  268. void lcd_print(const char *str)
  269. {
  270. lcd.print(str);
  271. }
  272. void lcd_print_at(uint8_t x, uint8_t y, const char *str)
  273. {
  274. lcd.setCursor(x, y);
  275. lcd.print(str);
  276. }
  277. void lcd_drawedit(const char* pstr, char* value)
  278. {
  279. lcd.setCursor(1, 1);
  280. lcd_printPGM(pstr);
  281. lcd.print(':');
  282. #if LCD_WIDTH < 20
  283. lcd.setCursor(LCD_WIDTH - strlen(value), 1);
  284. #else
  285. lcd.setCursor(LCD_WIDTH -1 - strlen(value), 1);
  286. #endif
  287. lcd.print(value);
  288. }
  289. void lcd_drawedit_2(const char* pstr, char* value)
  290. {
  291. lcd.setCursor(0, 1);
  292. lcd_printPGM(pstr);
  293. lcd.print(':');
  294. lcd.setCursor((LCD_WIDTH - strlen(value))/2, 3);
  295. lcd.print(value);
  296. lcd.print(" mm");
  297. }
  298. ////////////////////////////////////////////////////////////////////////////////
  299. // Custom character data
  300. const uint8_t lcd_chardata_bedTemp[8] PROGMEM = {
  301. B00000,
  302. B11111,
  303. B10101,
  304. B10001,
  305. B10101,
  306. B11111,
  307. B00000,
  308. B00000}; //thanks Sonny Mounicou
  309. const uint8_t lcd_chardata_degree[8] PROGMEM = {
  310. B01100,
  311. B10010,
  312. B10010,
  313. B01100,
  314. B00000,
  315. B00000,
  316. B00000,
  317. B00000};
  318. const uint8_t lcd_chardata_thermometer[8] PROGMEM = {
  319. B00100,
  320. B01010,
  321. B01010,
  322. B01010,
  323. B01010,
  324. B10001,
  325. B10001,
  326. B01110};
  327. const uint8_t lcd_chardata_uplevel[8] PROGMEM = {
  328. B00100,
  329. B01110,
  330. B11111,
  331. B00100,
  332. B11100,
  333. B00000,
  334. B00000,
  335. B00000}; //thanks joris
  336. const uint8_t lcd_chardata_refresh[8] PROGMEM = {
  337. B00000,
  338. B00110,
  339. B11001,
  340. B11000,
  341. B00011,
  342. B10011,
  343. B01100,
  344. B00000}; //thanks joris
  345. const uint8_t lcd_chardata_folder[8] PROGMEM = {
  346. B00000,
  347. B11100,
  348. B11111,
  349. B10001,
  350. B10001,
  351. B11111,
  352. B00000,
  353. B00000}; //thanks joris
  354. const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  355. B11100,
  356. B10000,
  357. B11000,
  358. B10111,
  359. B00101,
  360. B00110,
  361. B00101,
  362. B00000}; //thanks Sonny Mounicou
  363. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  364. B11100,
  365. B10100,
  366. B11000,
  367. B10100,
  368. B00000,
  369. B00111,
  370. B00010,
  371. B00010};*/
  372. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  373. B01100,
  374. B10011,
  375. B00000,
  376. B01100,
  377. B10011,
  378. B00000,
  379. B01100,
  380. B10011};*/
  381. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  382. B00000,
  383. B00100,
  384. B10010,
  385. B01001,
  386. B10010,
  387. B00100,
  388. B00000,
  389. B00000};*/
  390. const uint8_t lcd_chardata_clock[8] PROGMEM = {
  391. B00000,
  392. B01110,
  393. B10011,
  394. B10101,
  395. B10001,
  396. B01110,
  397. B00000,
  398. B00000}; //thanks Sonny Mounicou
  399. const uint8_t lcd_chardata_arrup[8] PROGMEM = {
  400. B00100,
  401. B01110,
  402. B11111,
  403. B00000,
  404. B00000,
  405. B00000,
  406. B00000,
  407. B00000};
  408. const uint8_t lcd_chardata_arrdown[8] PROGMEM = {
  409. B00000,
  410. B00000,
  411. B00000,
  412. B00000,
  413. B00000,
  414. B10001,
  415. B01010,
  416. B00100};
  417. void lcd_set_custom_characters(void)
  418. {
  419. lcd.createChar_P(LCD_STR_BEDTEMP[0], lcd_chardata_bedTemp);
  420. lcd.createChar_P(LCD_STR_DEGREE[0], lcd_chardata_degree);
  421. lcd.createChar_P(LCD_STR_THERMOMETER[0], lcd_chardata_thermometer);
  422. lcd.createChar_P(LCD_STR_UPLEVEL[0], lcd_chardata_uplevel);
  423. lcd.createChar_P(LCD_STR_REFRESH[0], lcd_chardata_refresh);
  424. lcd.createChar_P(LCD_STR_FOLDER[0], lcd_chardata_folder);
  425. lcd.createChar_P(LCD_STR_FEEDRATE[0], lcd_chardata_feedrate);
  426. lcd.createChar_P(LCD_STR_CLOCK[0], lcd_chardata_clock);
  427. //lcd.createChar_P(LCD_STR_ARROW_UP[0], lcd_chardata_arrup);
  428. //lcd.createChar_P(LCD_STR_ARROW_DOWN[0], lcd_chardata_arrdown);
  429. }
  430. void lcd_set_custom_characters_arrows(void)
  431. {
  432. lcd.createChar_P(1, lcd_chardata_arrdown);
  433. }
  434. const uint8_t lcd_chardata_progress[8] PROGMEM = {
  435. B11111,
  436. B11111,
  437. B11111,
  438. B11111,
  439. B11111,
  440. B11111,
  441. B11111,
  442. B11111};
  443. void lcd_set_custom_characters_progress(void)
  444. {
  445. lcd.createChar_P(1, lcd_chardata_progress);
  446. }
  447. const uint8_t lcd_chardata_arr2down[8] PROGMEM = {
  448. B00000,
  449. B00000,
  450. B10001,
  451. B01010,
  452. B00100,
  453. B10001,
  454. B01010,
  455. B00100};
  456. const uint8_t lcd_chardata_confirm[8] PROGMEM = {
  457. B00000,
  458. B00001,
  459. B00011,
  460. B10110,
  461. B11100,
  462. B01000,
  463. B00000};
  464. void lcd_set_custom_characters_nextpage(void)
  465. {
  466. lcd.createChar_P(1, lcd_chardata_arr2down);
  467. lcd.createChar_P(2, lcd_chardata_confirm);
  468. }
  469. void lcd_set_custom_characters_degree(void)
  470. {
  471. lcd.createChar_P(1, lcd_chardata_degree);
  472. }