lcd.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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_clear(void)
  207. {
  208. lcd.clear();
  209. }
  210. /* Arduino < 1.0.0 is missing a function to print PROGMEM strings, so we need to implement our own */
  211. void lcd_printPGM(const char* str)
  212. {
  213. char c;
  214. while((c = pgm_read_byte(str++)) != '\0')
  215. {
  216. lcd.write(c);
  217. }
  218. }
  219. void lcd_print_at_PGM(uint8_t x, uint8_t y, const char* str)
  220. {
  221. lcd.setCursor(x, y);
  222. char c;
  223. while((c = pgm_read_byte(str++)) != '\0')
  224. {
  225. lcd.write(c);
  226. }
  227. }
  228. void lcd_implementation_write(char c)
  229. {
  230. lcd.write(c);
  231. }
  232. void lcd_print(int8_t i)
  233. {
  234. lcd.print(i);
  235. }
  236. void lcd_print_at(uint8_t x, uint8_t y, int8_t i)
  237. {
  238. lcd.setCursor(x, y);
  239. lcd.print(i);
  240. }
  241. void lcd_print(int i)
  242. {
  243. lcd.print(i);
  244. }
  245. void lcd_print_at(uint8_t x, uint8_t y, int i)
  246. {
  247. lcd.setCursor(x, y);
  248. lcd.print(i);
  249. }
  250. void lcd_print(float f)
  251. {
  252. lcd.print(f);
  253. }
  254. void lcd_print(const char *str)
  255. {
  256. lcd.print(str);
  257. }
  258. void lcd_print_at(uint8_t x, uint8_t y, const char *str)
  259. {
  260. lcd.setCursor(x, y);
  261. lcd.print(str);
  262. }
  263. void lcd_drawedit(const char* pstr, char* value)
  264. {
  265. lcd.setCursor(1, 1);
  266. lcd_printPGM(pstr);
  267. lcd.print(':');
  268. #if LCD_WIDTH < 20
  269. lcd.setCursor(LCD_WIDTH - strlen(value), 1);
  270. #else
  271. lcd.setCursor(LCD_WIDTH -1 - strlen(value), 1);
  272. #endif
  273. lcd.print(value);
  274. }
  275. void lcd_drawedit_2(const char* pstr, char* value)
  276. {
  277. lcd.setCursor(0, 1);
  278. lcd_printPGM(pstr);
  279. lcd.print(':');
  280. lcd.setCursor((LCD_WIDTH - strlen(value))/2, 3);
  281. lcd.print(value);
  282. lcd.print(" mm");
  283. }
  284. ////////////////////////////////////////////////////////////////////////////////
  285. // Custom character data
  286. const uint8_t lcd_chardata_bedTemp[8] PROGMEM = {
  287. B00000,
  288. B11111,
  289. B10101,
  290. B10001,
  291. B10101,
  292. B11111,
  293. B00000,
  294. B00000}; //thanks Sonny Mounicou
  295. const uint8_t lcd_chardata_degree[8] PROGMEM = {
  296. B01100,
  297. B10010,
  298. B10010,
  299. B01100,
  300. B00000,
  301. B00000,
  302. B00000,
  303. B00000};
  304. const uint8_t lcd_chardata_thermometer[8] PROGMEM = {
  305. B00100,
  306. B01010,
  307. B01010,
  308. B01010,
  309. B01010,
  310. B10001,
  311. B10001,
  312. B01110};
  313. const uint8_t lcd_chardata_uplevel[8] PROGMEM = {
  314. B00100,
  315. B01110,
  316. B11111,
  317. B00100,
  318. B11100,
  319. B00000,
  320. B00000,
  321. B00000}; //thanks joris
  322. const uint8_t lcd_chardata_refresh[8] PROGMEM = {
  323. B00000,
  324. B00110,
  325. B11001,
  326. B11000,
  327. B00011,
  328. B10011,
  329. B01100,
  330. B00000}; //thanks joris
  331. const uint8_t lcd_chardata_folder[8] PROGMEM = {
  332. B00000,
  333. B11100,
  334. B11111,
  335. B10001,
  336. B10001,
  337. B11111,
  338. B00000,
  339. B00000}; //thanks joris
  340. const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  341. B11100,
  342. B10000,
  343. B11000,
  344. B10111,
  345. B00101,
  346. B00110,
  347. B00101,
  348. B00000}; //thanks Sonny Mounicou
  349. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  350. B11100,
  351. B10100,
  352. B11000,
  353. B10100,
  354. B00000,
  355. B00111,
  356. B00010,
  357. B00010};*/
  358. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  359. B01100,
  360. B10011,
  361. B00000,
  362. B01100,
  363. B10011,
  364. B00000,
  365. B01100,
  366. B10011};*/
  367. /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = {
  368. B00000,
  369. B00100,
  370. B10010,
  371. B01001,
  372. B10010,
  373. B00100,
  374. B00000,
  375. B00000};*/
  376. const uint8_t lcd_chardata_clock[8] PROGMEM = {
  377. B00000,
  378. B01110,
  379. B10011,
  380. B10101,
  381. B10001,
  382. B01110,
  383. B00000,
  384. B00000}; //thanks Sonny Mounicou
  385. const uint8_t lcd_chardata_arrup[8] PROGMEM = {
  386. B00100,
  387. B01110,
  388. B11111,
  389. B00000,
  390. B00000,
  391. B00000,
  392. B00000,
  393. B00000};
  394. const uint8_t lcd_chardata_arrdown[8] PROGMEM = {
  395. B00000,
  396. B00000,
  397. B00000,
  398. B00000,
  399. B00000,
  400. B10001,
  401. B01010,
  402. B00100};
  403. void lcd_set_custom_characters(void)
  404. {
  405. lcd.createChar_P(LCD_STR_BEDTEMP[0], lcd_chardata_bedTemp);
  406. lcd.createChar_P(LCD_STR_DEGREE[0], lcd_chardata_degree);
  407. lcd.createChar_P(LCD_STR_THERMOMETER[0], lcd_chardata_thermometer);
  408. lcd.createChar_P(LCD_STR_UPLEVEL[0], lcd_chardata_uplevel);
  409. lcd.createChar_P(LCD_STR_REFRESH[0], lcd_chardata_refresh);
  410. lcd.createChar_P(LCD_STR_FOLDER[0], lcd_chardata_folder);
  411. lcd.createChar_P(LCD_STR_FEEDRATE[0], lcd_chardata_feedrate);
  412. lcd.createChar_P(LCD_STR_CLOCK[0], lcd_chardata_clock);
  413. //lcd.createChar_P(LCD_STR_ARROW_UP[0], lcd_chardata_arrup);
  414. //lcd.createChar_P(LCD_STR_ARROW_DOWN[0], lcd_chardata_arrdown);
  415. }
  416. void lcd_set_custom_characters_arrows(void)
  417. {
  418. lcd.createChar_P(1, lcd_chardata_arrdown);
  419. }
  420. const uint8_t lcd_chardata_progress[8] PROGMEM = {
  421. B11111,
  422. B11111,
  423. B11111,
  424. B11111,
  425. B11111,
  426. B11111,
  427. B11111,
  428. B11111};
  429. void lcd_set_custom_characters_progress(void)
  430. {
  431. lcd.createChar_P(1, lcd_chardata_progress);
  432. }
  433. const uint8_t lcd_chardata_arr2down[8] PROGMEM = {
  434. B00000,
  435. B00000,
  436. B10001,
  437. B01010,
  438. B00100,
  439. B10001,
  440. B01010,
  441. B00100};
  442. const uint8_t lcd_chardata_confirm[8] PROGMEM = {
  443. B00000,
  444. B00001,
  445. B00011,
  446. B10110,
  447. B11100,
  448. B01000,
  449. B00000};
  450. void lcd_set_custom_characters_nextpage(void)
  451. {
  452. lcd.createChar_P(1, lcd_chardata_arr2down);
  453. lcd.createChar_P(2, lcd_chardata_confirm);
  454. }
  455. void lcd_set_custom_characters_degree(void)
  456. {
  457. lcd.createChar_P(1, lcd_chardata_degree);
  458. }