menu.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //menu.cpp
  2. #include "menu.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <avr/pgmspace.h>
  7. #include "lcd.h"
  8. #include "Configuration.h"
  9. #include "Marlin.h"
  10. #include "ultralcd.h"
  11. #include "language.h"
  12. #include "static_assert.h"
  13. #include "sound.h"
  14. extern int32_t lcd_encoder;
  15. #define MENU_DEPTH_MAX 7
  16. static menu_record_t menu_stack[MENU_DEPTH_MAX];
  17. uint8_t menu_data[MENU_DATA_SIZE];
  18. #ifndef __AVR__
  19. #error "menu_data is non-portable to non 8bit processor"
  20. #endif
  21. uint8_t menu_depth = 0;
  22. uint8_t menu_block_entering_on_serious_errors = SERIOUS_ERR_NONE;
  23. uint8_t menu_line = 0;
  24. uint8_t menu_item = 0;
  25. uint8_t menu_row = 0;
  26. uint8_t menu_top = 0;
  27. uint8_t menu_clicked = 0;
  28. uint8_t menu_entering = 0;
  29. uint8_t menu_leaving = 0;
  30. menu_func_t menu_menu = 0;
  31. static_assert(sizeof(menu_data)>= sizeof(menu_data_edit_t),"menu_data_edit_t doesn't fit into menu_data");
  32. void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
  33. {
  34. asm("cli");
  35. if (menu_menu != menu)
  36. {
  37. menu_menu = menu;
  38. lcd_encoder = encoder;
  39. asm("sei");
  40. if (reset_menu_state)
  41. {
  42. // Resets the global shared C union.
  43. // This ensures, that the menu entered will find out, that it shall initialize itself.
  44. memset(&menu_data, 0, sizeof(menu_data));
  45. }
  46. if (feedback) lcd_quick_feedback();
  47. }
  48. else
  49. asm("sei");
  50. }
  51. void menu_start(void)
  52. {
  53. if (lcd_encoder > 0x8000) lcd_encoder = 0;
  54. if (lcd_encoder < 0)
  55. {
  56. lcd_encoder = 0;
  57. Sound_MakeSound(e_SOUND_TYPE_BlindAlert);
  58. }
  59. if (lcd_encoder < menu_top)
  60. menu_top = lcd_encoder;
  61. menu_line = menu_top;
  62. menu_clicked = LCD_CLICKED;
  63. }
  64. void menu_end(void)
  65. {
  66. if (lcd_encoder >= menu_item)
  67. {
  68. lcd_encoder = menu_item - 1;
  69. Sound_MakeSound(e_SOUND_TYPE_BlindAlert);
  70. }
  71. if (((uint8_t)lcd_encoder) >= menu_top + LCD_HEIGHT)
  72. {
  73. menu_top = lcd_encoder - LCD_HEIGHT + 1;
  74. lcd_draw_update = 1;
  75. menu_line = menu_top - 1;
  76. menu_row = -1;
  77. }
  78. }
  79. void menu_back(uint8_t nLevel)
  80. {
  81. menu_depth = ((menu_depth > nLevel) ? (menu_depth - nLevel) : 0);
  82. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
  83. }
  84. void menu_back(void)
  85. {
  86. menu_back(1);
  87. }
  88. static void menu_back_no_reset(void)
  89. {
  90. if (menu_depth > 0)
  91. {
  92. menu_depth--;
  93. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
  94. }
  95. }
  96. void menu_back_no_feedback(void)
  97. {
  98. if (menu_depth > 0)
  99. {
  100. menu_depth--;
  101. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, false, true);
  102. }
  103. }
  104. void menu_back_if_clicked(void)
  105. {
  106. if (lcd_clicked())
  107. menu_back();
  108. }
  109. void menu_back_if_clicked_fb(void)
  110. {
  111. if (lcd_clicked())
  112. {
  113. lcd_quick_feedback();
  114. menu_back();
  115. }
  116. }
  117. void menu_submenu(menu_func_t submenu)
  118. {
  119. if (menu_depth < MENU_DEPTH_MAX)
  120. {
  121. menu_stack[menu_depth].menu = menu_menu;
  122. menu_stack[menu_depth++].position = lcd_encoder;
  123. menu_goto(submenu, 0, true, true);
  124. }
  125. }
  126. static void menu_submenu_no_reset(menu_func_t submenu)
  127. {
  128. if (menu_depth < MENU_DEPTH_MAX)
  129. {
  130. menu_stack[menu_depth].menu = menu_menu;
  131. menu_stack[menu_depth++].position = lcd_encoder;
  132. menu_goto(submenu, 0, true, false);
  133. }
  134. }
  135. void menu_submenu_scroll(menu_func_t submenu)
  136. {
  137. if (menu_depth < MENU_DEPTH_MAX)
  138. {
  139. menu_stack[menu_depth].menu = menu_menu;
  140. menu_stack[menu_depth++].position = lcd_encoder;
  141. menu_goto(submenu, menu_row, false, true);
  142. }
  143. }
  144. uint8_t menu_item_ret(void)
  145. {
  146. lcd_beeper_quick_feedback();
  147. lcd_draw_update = 2;
  148. lcd_button_pressed = false;
  149. return 1;
  150. }
  151. /*
  152. int menu_draw_item_printf_P(char type_char, const char* format, ...)
  153. {
  154. va_list args;
  155. va_start(args, format);
  156. int ret = 0;
  157. lcd_set_cursor(0, menu_row);
  158. if (lcd_encoder == menu_item)
  159. lcd_print('>');
  160. else
  161. lcd_print(' ');
  162. int cnt = vfprintf_P(lcdout, format, args);
  163. for (int i = cnt; i < 18; i++)
  164. lcd_print(' ');
  165. lcd_print(type_char);
  166. va_end(args);
  167. return ret;
  168. }
  169. */
  170. static char menu_selection_mark(){
  171. return (lcd_encoder == menu_item)?'>':' ';
  172. }
  173. static void menu_draw_item_puts_P(char type_char, const char* str)
  174. {
  175. lcd_set_cursor(0, menu_row);
  176. lcd_printf_P(PSTR("%c%-18.18S%c"), menu_selection_mark(), str, type_char);
  177. }
  178. static void menu_draw_toggle_puts_P(const char* str, const char* toggle, const uint8_t settings)
  179. {
  180. //settings:
  181. //xxxxxcba
  182. //a = selection mark. If it's set(1), then '>' will be used as the first character on the line. Else leave blank
  183. //b = toggle string is from progmem
  184. //c = do not set cursor at all. Must be handled externally.
  185. char lineStr[LCD_WIDTH + 1];
  186. const char eol = (toggle == NULL)?LCD_STR_ARROW_RIGHT[0]:' ';
  187. if (toggle == NULL) toggle = _T(MSG_NA);
  188. sprintf_P(lineStr, PSTR("%c%-18.18S"), (settings & 0x01)?'>':' ', str);
  189. sprintf_P(lineStr + LCD_WIDTH - ((settings & 0x02)?strlen_P(toggle):strlen(toggle)) - 3, (settings & 0x02)?PSTR("[%S]%c"):PSTR("[%s]%c"), toggle, eol);
  190. if (!(settings & 0x04)) lcd_set_cursor(0, menu_row);
  191. fputs(lineStr, lcdout);
  192. }
  193. //! @brief Format sheet name
  194. //!
  195. //! @param[in] sheet_E Sheet in EEPROM
  196. //! @param[out] buffer for formatted output
  197. void menu_format_sheet_E(const Sheet &sheet_E, SheetFormatBuffer &buffer)
  198. {
  199. uint_least8_t index = sprintf_P(buffer.c, PSTR("%.10S "), _T(MSG_SHEET));
  200. eeprom_read_block(&(buffer.c[index]), sheet_E.name, 7);
  201. //index += 7;
  202. buffer.c[index + 7] = '\0';
  203. }
  204. //! @brief Format sheet name in select menu
  205. //!
  206. //! @param[in] sheet_E Sheet in EEPROM
  207. //! @param[out] buffer for formatted output
  208. void menu_format_sheet_select_E(const Sheet &sheet_E, SheetFormatBuffer &buffer)
  209. {
  210. uint_least8_t index = sprintf_P(buffer.c,PSTR("%-9.9S["), _T(MSG_SHEET));
  211. eeprom_read_block(&(buffer.c[index]), sheet_E.name, sizeof(sheet_E.name)/sizeof(sheet_E.name[0]));
  212. for (const uint_least8_t start = index; static_cast<uint_least8_t>(index - start) < sizeof(sheet_E.name)/sizeof(sheet_E.name[0]); ++index)
  213. {
  214. if (buffer.c[index] == '\0') break;
  215. }
  216. buffer.c[index] = ']';
  217. buffer.c[index + 1] = '\0';
  218. }
  219. static void menu_draw_item_select_sheet_E(char type_char, const Sheet &sheet)
  220. {
  221. lcd_set_cursor(0, menu_row);
  222. SheetFormatBuffer buffer;
  223. menu_format_sheet_select_E(sheet, buffer);
  224. lcd_printf_P(PSTR("%c%-18.18s%c"), menu_selection_mark(), buffer.c, type_char);
  225. }
  226. static void menu_draw_item_puts_E(char type_char, const Sheet &sheet)
  227. {
  228. lcd_set_cursor(0, menu_row);
  229. SheetFormatBuffer buffer;
  230. menu_format_sheet_E(sheet, buffer);
  231. lcd_printf_P(PSTR("%c%-18.18s%c"), menu_selection_mark(), buffer.c, type_char);
  232. }
  233. static void menu_draw_item_puts_P(char type_char, const char* str, char num)
  234. {
  235. lcd_set_cursor(0, menu_row);
  236. lcd_printf_P(PSTR("%c%-.16S "), menu_selection_mark(), str);
  237. lcd_putc(num);
  238. lcd_set_cursor(19, menu_row);
  239. lcd_putc(type_char);
  240. }
  241. /*
  242. int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
  243. {
  244. lcd_set_cursor(0, menu_row);
  245. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  246. return cnt;
  247. }
  248. */
  249. void menu_item_dummy(void)
  250. {
  251. menu_item++;
  252. }
  253. uint8_t menu_item_text_P(const char* str)
  254. {
  255. if (menu_item == menu_line)
  256. {
  257. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  258. if (menu_clicked && (lcd_encoder == menu_item))
  259. return menu_item_ret();
  260. }
  261. menu_item++;
  262. return 0;
  263. }
  264. uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
  265. {
  266. if (menu_item == menu_line)
  267. {
  268. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
  269. if (menu_clicked && (lcd_encoder == menu_item))
  270. {
  271. menu_submenu(submenu);
  272. return menu_item_ret();
  273. }
  274. }
  275. menu_item++;
  276. return 0;
  277. }
  278. uint8_t menu_item_submenu_E(const Sheet &sheet, menu_func_t submenu)
  279. {
  280. if (menu_item == menu_line)
  281. {
  282. if (lcd_draw_update) menu_draw_item_puts_E(LCD_STR_ARROW_RIGHT[0], sheet);
  283. if (menu_clicked && (lcd_encoder == menu_item))
  284. {
  285. menu_submenu(submenu);
  286. return menu_item_ret();
  287. }
  288. }
  289. menu_item++;
  290. return 0;
  291. }
  292. uint8_t menu_item_function_E(const Sheet &sheet, menu_func_t func)
  293. {
  294. if (menu_item == menu_line)
  295. {
  296. if (lcd_draw_update) menu_draw_item_select_sheet_E(' ', sheet);
  297. if (menu_clicked && (lcd_encoder == menu_item))
  298. {
  299. menu_clicked = false;
  300. lcd_consume_click();
  301. lcd_update_enabled = 0;
  302. if (func) func();
  303. lcd_update_enabled = 1;
  304. return menu_item_ret();
  305. }
  306. }
  307. menu_item++;
  308. return 0;
  309. }
  310. uint8_t menu_item_back_P(const char* str)
  311. {
  312. if (menu_item == menu_line)
  313. {
  314. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
  315. if (menu_clicked && (lcd_encoder == menu_item))
  316. {
  317. menu_back();
  318. return menu_item_ret();
  319. }
  320. }
  321. menu_item++;
  322. return 0;
  323. }
  324. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  325. {
  326. if (menu_item == menu_line)
  327. {
  328. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  329. if (menu_clicked && (lcd_encoder == menu_item))
  330. {
  331. menu_clicked = false;
  332. lcd_consume_click();
  333. lcd_update_enabled = 0;
  334. if (func) func();
  335. lcd_update_enabled = 1;
  336. return menu_item_ret();
  337. }
  338. }
  339. menu_item++;
  340. return 0;
  341. }
  342. //! @brief Menu item function taking single parameter
  343. //!
  344. //! Ideal for numbered lists calling functions with number parameter.
  345. //! @param str Item caption
  346. //! @param number aditional character to be added after str, e.g. number
  347. //! @param func pointer to function taking uint8_t with no return value
  348. //! @param fn_par value to be passed to function
  349. //! @retval 0
  350. //! @retval 1 Item was clicked
  351. uint8_t menu_item_function_P(const char* str, char number, void (*func)(uint8_t), uint8_t fn_par)
  352. {
  353. if (menu_item == menu_line)
  354. {
  355. if (lcd_draw_update) menu_draw_item_puts_P(' ', str, number);
  356. if (menu_clicked && (lcd_encoder == menu_item))
  357. {
  358. menu_clicked = false;
  359. lcd_consume_click();
  360. lcd_update_enabled = 0;
  361. if (func) func(fn_par);
  362. lcd_update_enabled = 1;
  363. return menu_item_ret();
  364. }
  365. }
  366. menu_item++;
  367. return 0;
  368. }
  369. uint8_t menu_item_toggle_P(const char* str, const char* toggle, menu_func_t func, const uint8_t settings)
  370. {
  371. if (menu_item == menu_line)
  372. {
  373. if (lcd_draw_update) menu_draw_toggle_puts_P(str, toggle, settings | (menu_selection_mark()=='>'));
  374. if (menu_clicked && (lcd_encoder == menu_item))
  375. {
  376. if (toggle == NULL) // print N/A warning message
  377. {
  378. menu_submenu(func);
  379. return menu_item_ret();
  380. }
  381. else // do the actual toggling
  382. {
  383. menu_clicked = false;
  384. lcd_consume_click();
  385. lcd_update_enabled = 0;
  386. if (func) func();
  387. lcd_update_enabled = 1;
  388. return menu_item_ret();
  389. }
  390. }
  391. }
  392. menu_item++;
  393. return 0;
  394. }
  395. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  396. {
  397. if (menu_item == menu_line)
  398. {
  399. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  400. if (menu_clicked && (lcd_encoder == menu_item))
  401. {
  402. if (str_gcode) enquecommand_P(str_gcode);
  403. return menu_item_ret();
  404. }
  405. }
  406. menu_item++;
  407. return 0;
  408. }
  409. const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
  410. const char menu_fmt_float31[] PROGMEM = "%-12.12S%+8.1f";
  411. const char menu_fmt_float13[] PROGMEM = "%c%-13.13S%+5.3f";
  412. template<typename T>
  413. static void menu_draw_P(char chr, const char* str, int16_t val);
  414. template<>
  415. void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
  416. {
  417. int text_len = strlen_P(str);
  418. if (text_len > 15) text_len = 15;
  419. char spaces[LCD_WIDTH + 1] = {0};
  420. memset(spaces,' ', LCD_WIDTH);
  421. if (val <= -100) spaces[15 - text_len - 1] = 0;
  422. else spaces[15 - text_len] = 0;
  423. lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
  424. }
  425. template<>
  426. void menu_draw_P<uint8_t*>(char chr, const char* str, int16_t val)
  427. {
  428. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  429. float factor = 1.0f + static_cast<float>(val) / 1000.0f;
  430. if (val <= _md->minEditValue)
  431. {
  432. menu_draw_toggle_puts_P(str, _T(MSG_OFF), 0x04 | 0x02 | (chr=='>'));
  433. }
  434. else
  435. {
  436. lcd_printf_P(menu_fmt_float13, chr, str, factor);
  437. }
  438. }
  439. //! @brief Draw up to 10 chars of text and a float number in format from +0.0 to +12345.0. The increased range is necessary
  440. //! for displaying large values of extruder positions, which caused text overflow in the previous implementation.
  441. //!
  442. //! @param str string label to print
  443. //! @param val value to print aligned to the right side of the display
  444. //!
  445. //! Implementation comments:
  446. //! The text needs to come with a colon ":", this function does not append it anymore.
  447. //! That resulted in a much shorter implementation (234628B -> 234476B)
  448. //! There are similar functions around which may be shortened in a similar way
  449. void menu_draw_float31(const char* str, float val)
  450. {
  451. lcd_printf_P(menu_fmt_float31, str, val);
  452. }
  453. //! @brief Draw up to 14 chars of text and a float number in format +1.234
  454. //!
  455. //! @param str string label to print
  456. //! @param val value to print aligned to the right side of the display
  457. //!
  458. //! Implementation comments:
  459. //! This function uses similar optimization principles as menu_draw_float31
  460. //! (i.e. str must include a ':' at its end)
  461. //! FLASH usage dropped 234476B -> 234392B
  462. //! Moreover, this function gets inlined in the final code, so removing it doesn't really help ;)
  463. void menu_draw_float13(const char* str, float val)
  464. {
  465. lcd_printf_P(menu_fmt_float13, ' ', str, val);
  466. }
  467. template <typename T>
  468. static void _menu_edit_P(void)
  469. {
  470. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  471. if (lcd_draw_update)
  472. {
  473. if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
  474. if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
  475. lcd_set_cursor(0, 1);
  476. menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
  477. }
  478. if (LCD_CLICKED)
  479. {
  480. *((T)(_md->editValue)) = lcd_encoder;
  481. menu_back_no_reset();
  482. }
  483. }
  484. template <typename T>
  485. uint8_t menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val)
  486. {
  487. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  488. if (menu_item == menu_line)
  489. {
  490. if (lcd_draw_update)
  491. {
  492. lcd_set_cursor(0, menu_row);
  493. menu_draw_P<T>(menu_selection_mark(), str, *pval);
  494. }
  495. if (menu_clicked && (lcd_encoder == menu_item))
  496. {
  497. menu_submenu_no_reset(_menu_edit_P<T>);
  498. _md->editLabel = str;
  499. _md->editValue = pval;
  500. _md->minEditValue = min_val;
  501. _md->maxEditValue = max_val;
  502. lcd_encoder = *pval;
  503. return menu_item_ret();
  504. }
  505. }
  506. menu_item++;
  507. return 0;
  508. }
  509. template uint8_t menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val);
  510. template uint8_t menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val);
  511. #undef _menu_data