menu.cpp 15 KB

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