lcd.cpp 9.9 KB

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