menu.cpp 14 KB

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