menu.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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_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_data_reset(void)
  33. {
  34. // Resets the global shared C union.
  35. // This ensures, that the menu entered will find out, that it shall initialize itself.
  36. memset(&menu_data, 0, sizeof(menu_data));
  37. }
  38. void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
  39. {
  40. CRITICAL_SECTION_START;
  41. if (menu_menu != menu)
  42. {
  43. menu_menu = menu;
  44. lcd_encoder = encoder;
  45. menu_top = 0; //reset menu view. Needed if menu_back() is called from deep inside a menu, such as Support
  46. CRITICAL_SECTION_END;
  47. if (reset_menu_state)
  48. menu_data_reset();
  49. if (feedback) lcd_quick_feedback();
  50. }
  51. else
  52. CRITICAL_SECTION_END;
  53. }
  54. void menu_start(void)
  55. {
  56. if (lcd_encoder > 0x8000) lcd_encoder = 0;
  57. if (lcd_encoder < 0)
  58. {
  59. lcd_encoder = 0;
  60. Sound_MakeSound(e_SOUND_TYPE_BlindAlert);
  61. }
  62. if (lcd_encoder < menu_top)
  63. menu_top = lcd_encoder;
  64. menu_line = menu_top;
  65. menu_clicked = LCD_CLICKED;
  66. }
  67. void menu_end(void)
  68. {
  69. if (lcd_encoder >= menu_item)
  70. {
  71. lcd_encoder = menu_item - 1;
  72. Sound_MakeSound(e_SOUND_TYPE_BlindAlert);
  73. }
  74. if (((uint8_t)lcd_encoder) >= menu_top + LCD_HEIGHT)
  75. {
  76. menu_top = lcd_encoder - LCD_HEIGHT + 1;
  77. lcd_draw_update = 1;
  78. menu_line = menu_top - 1;
  79. menu_row = -1;
  80. }
  81. }
  82. void menu_back(uint8_t nLevel)
  83. {
  84. menu_depth = ((menu_depth > nLevel) ? (menu_depth - nLevel) : 0);
  85. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
  86. }
  87. void menu_back(void)
  88. {
  89. menu_back(1);
  90. }
  91. void menu_back_no_reset(void)
  92. {
  93. if (menu_depth > 0)
  94. {
  95. menu_depth--;
  96. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
  97. }
  98. }
  99. void menu_back_if_clicked(void)
  100. {
  101. if (lcd_clicked())
  102. menu_back();
  103. }
  104. void menu_back_if_clicked_fb(void)
  105. {
  106. if (lcd_clicked())
  107. {
  108. lcd_quick_feedback();
  109. menu_back();
  110. }
  111. }
  112. void menu_submenu(menu_func_t submenu)
  113. {
  114. if (menu_depth < MENU_DEPTH_MAX)
  115. {
  116. menu_stack[menu_depth].menu = menu_menu;
  117. menu_stack[menu_depth++].position = lcd_encoder;
  118. menu_goto(submenu, 0, true, true);
  119. }
  120. }
  121. void menu_submenu_no_reset(menu_func_t submenu)
  122. {
  123. if (menu_depth < MENU_DEPTH_MAX)
  124. {
  125. menu_stack[menu_depth].menu = menu_menu;
  126. menu_stack[menu_depth++].position = lcd_encoder;
  127. menu_goto(submenu, 0, true, false);
  128. }
  129. }
  130. uint8_t menu_item_ret(void)
  131. {
  132. lcd_beeper_quick_feedback();
  133. lcd_draw_update = 2;
  134. lcd_button_pressed = false;
  135. return 1;
  136. }
  137. /*
  138. int menu_draw_item_printf_P(char type_char, const char* format, ...)
  139. {
  140. va_list args;
  141. va_start(args, format);
  142. int ret = 0;
  143. lcd_set_cursor(0, menu_row);
  144. if (lcd_encoder == menu_item)
  145. lcd_print('>');
  146. else
  147. lcd_print(' ');
  148. int cnt = vfprintf_P(lcdout, format, args);
  149. for (int i = cnt; i < 18; i++)
  150. lcd_print(' ');
  151. lcd_print(type_char);
  152. va_end(args);
  153. return ret;
  154. }
  155. */
  156. static char menu_selection_mark(){
  157. return (lcd_encoder == menu_item)?'>':' ';
  158. }
  159. static void menu_draw_item_puts_P(char type_char, const char* str)
  160. {
  161. lcd_set_cursor(0, menu_row);
  162. lcd_printf_P(PSTR("%c%-18.18S%c"), menu_selection_mark(), str, type_char);
  163. }
  164. static void menu_draw_toggle_puts_P(const char* str, const char* toggle, const uint8_t settings)
  165. {
  166. //settings:
  167. //xxxxxcba
  168. //a = selection mark. If it's set(1), then '>' will be used as the first character on the line. Else leave blank
  169. //b = toggle string is from progmem
  170. //c = do not set cursor at all. Must be handled externally.
  171. char lineStr[LCD_WIDTH + 1];
  172. const char eol = (toggle == NULL)?LCD_STR_ARROW_RIGHT[0]:' ';
  173. if (toggle == NULL) toggle = _T(MSG_NA);
  174. sprintf_P(lineStr, PSTR("%c%-18.18S"), (settings & 0x01)?'>':' ', str);
  175. sprintf_P(lineStr + LCD_WIDTH - ((settings & 0x02)?strlen_P(toggle):strlen(toggle)) - 3, (settings & 0x02)?PSTR("[%S]%c"):PSTR("[%s]%c"), toggle, eol);
  176. if (!(settings & 0x04)) lcd_set_cursor(0, menu_row);
  177. fputs(lineStr, lcdout);
  178. }
  179. //! @brief Format sheet name
  180. //!
  181. //! @param[in] sheet_E Sheet in EEPROM
  182. //! @param[out] buffer for formatted output
  183. void menu_format_sheet_E(const Sheet &sheet_E, SheetFormatBuffer &buffer)
  184. {
  185. uint_least8_t index = sprintf_P(buffer.c, PSTR("%.10S "), _T(MSG_SHEET));
  186. eeprom_read_block(&(buffer.c[index]), sheet_E.name, 7);
  187. //index += 7;
  188. buffer.c[index + 7] = '\0';
  189. }
  190. //! @brief Format sheet name in select menu
  191. //!
  192. //! @param[in] sheet_E Sheet in EEPROM
  193. //! @param[out] buffer for formatted output
  194. void menu_format_sheet_select_E(const Sheet &sheet_E, SheetFormatBuffer &buffer)
  195. {
  196. uint_least8_t index = sprintf_P(buffer.c,PSTR("%-9.9S["), _T(MSG_SHEET));
  197. eeprom_read_block(&(buffer.c[index]), sheet_E.name, sizeof(sheet_E.name)/sizeof(sheet_E.name[0]));
  198. for (const uint_least8_t start = index; static_cast<uint_least8_t>(index - start) < sizeof(sheet_E.name)/sizeof(sheet_E.name[0]); ++index)
  199. {
  200. if (buffer.c[index] == '\0') break;
  201. }
  202. buffer.c[index] = ']';
  203. buffer.c[index + 1] = '\0';
  204. }
  205. static void menu_draw_item_select_sheet_E(char type_char, const Sheet &sheet)
  206. {
  207. lcd_set_cursor(0, menu_row);
  208. SheetFormatBuffer buffer;
  209. menu_format_sheet_select_E(sheet, buffer);
  210. lcd_printf_P(PSTR("%c%-18.18s%c"), menu_selection_mark(), buffer.c, type_char);
  211. }
  212. static void menu_draw_item_puts_E(char type_char, const Sheet &sheet)
  213. {
  214. lcd_set_cursor(0, menu_row);
  215. SheetFormatBuffer buffer;
  216. menu_format_sheet_E(sheet, buffer);
  217. lcd_printf_P(PSTR("%c%-18.18s%c"), menu_selection_mark(), buffer.c, type_char);
  218. }
  219. static void menu_draw_item_puts_P(char type_char, const char* str, char num)
  220. {
  221. lcd_set_cursor(0, menu_row);
  222. lcd_printf_P(PSTR("%c%-.16S "), menu_selection_mark(), str);
  223. lcd_putc(num);
  224. lcd_putc_at(19, menu_row, 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 __attribute__((noinline)) 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. bool __attribute__((noinline)) menu_item_leave(){
  310. return ((menu_item == menu_line) && menu_clicked && (lcd_encoder == menu_item)) || menu_leaving;
  311. }
  312. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  313. {
  314. if (menu_item == menu_line)
  315. {
  316. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  317. if (menu_clicked && (lcd_encoder == menu_item))
  318. {
  319. menu_clicked = false;
  320. lcd_consume_click();
  321. lcd_update_enabled = 0;
  322. if (func) func();
  323. lcd_update_enabled = 1;
  324. return menu_item_ret();
  325. }
  326. }
  327. menu_item++;
  328. return 0;
  329. }
  330. //! @brief Menu item function taking single parameter
  331. //!
  332. //! Ideal for numbered lists calling functions with number parameter.
  333. //! @param str Item caption
  334. //! @param number aditional character to be added after str, e.g. number
  335. //! @param func pointer to function taking uint8_t with no return value
  336. //! @param fn_par value to be passed to function
  337. //! @retval 0
  338. //! @retval 1 Item was clicked
  339. uint8_t menu_item_function_P(const char* str, char number, void (*func)(uint8_t), uint8_t fn_par)
  340. {
  341. if (menu_item == menu_line)
  342. {
  343. if (lcd_draw_update) menu_draw_item_puts_P(' ', str, number);
  344. if (menu_clicked && (lcd_encoder == menu_item))
  345. {
  346. menu_clicked = false;
  347. lcd_consume_click();
  348. lcd_update_enabled = 0;
  349. if (func) func(fn_par);
  350. lcd_update_enabled = 1;
  351. return menu_item_ret();
  352. }
  353. }
  354. menu_item++;
  355. return 0;
  356. }
  357. uint8_t menu_item_toggle_P(const char* str, const char* toggle, menu_func_t func, const uint8_t settings)
  358. {
  359. if (menu_item == menu_line)
  360. {
  361. if (lcd_draw_update) menu_draw_toggle_puts_P(str, toggle, settings | (menu_selection_mark()=='>'));
  362. if (menu_clicked && (lcd_encoder == menu_item))
  363. {
  364. if (toggle == NULL) // print N/A warning message
  365. {
  366. menu_submenu(func);
  367. return menu_item_ret();
  368. }
  369. else // do the actual toggling
  370. {
  371. menu_clicked = false;
  372. lcd_consume_click();
  373. lcd_update_enabled = 0;
  374. if (func) func();
  375. lcd_update_enabled = 1;
  376. return menu_item_ret();
  377. }
  378. }
  379. }
  380. menu_item++;
  381. return 0;
  382. }
  383. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  384. {
  385. if (menu_item == menu_line)
  386. {
  387. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  388. if (menu_clicked && (lcd_encoder == menu_item))
  389. {
  390. if (str_gcode) enquecommand_P(str_gcode);
  391. return menu_item_ret();
  392. }
  393. }
  394. menu_item++;
  395. return 0;
  396. }
  397. const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
  398. const char menu_fmt_float31[] PROGMEM = "%-12.12S%+8.1f";
  399. const char menu_fmt_float13[] PROGMEM = "%c%-13.13S%+5.3f";
  400. template<typename T>
  401. static void menu_draw_P(char chr, const char* str, int16_t val);
  402. template<>
  403. void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
  404. {
  405. int text_len = strlen_P(str);
  406. if (text_len > 15) text_len = 15;
  407. char spaces[LCD_WIDTH + 1] = {0};
  408. memset(spaces,' ', LCD_WIDTH);
  409. if (val <= -100) spaces[15 - text_len - 1] = 0;
  410. else spaces[15 - text_len] = 0;
  411. lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
  412. }
  413. template<>
  414. void menu_draw_P<uint8_t*>(char chr, const char* str, int16_t val)
  415. {
  416. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  417. float factor = 1.0f + static_cast<float>(val) / 1000.0f;
  418. if (val <= _md->minEditValue)
  419. {
  420. menu_draw_toggle_puts_P(str, _T(MSG_OFF), 0x04 | 0x02 | (chr=='>'));
  421. }
  422. else
  423. {
  424. lcd_printf_P(menu_fmt_float13, chr, str, factor);
  425. }
  426. }
  427. //! @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
  428. //! for displaying large values of extruder positions, which caused text overflow in the previous implementation.
  429. //!
  430. //! @param str string label to print
  431. //! @param val value to print aligned to the right side of the display
  432. //!
  433. //! Implementation comments:
  434. //! The text needs to come with a colon ":", this function does not append it anymore.
  435. //! That resulted in a much shorter implementation (234628B -> 234476B)
  436. //! There are similar functions around which may be shortened in a similar way
  437. void menu_draw_float31(const char* str, float val)
  438. {
  439. lcd_printf_P(menu_fmt_float31, str, val);
  440. }
  441. //! @brief Draw up to 14 chars of text and a float number in format +1.234
  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. //! This function uses similar optimization principles as menu_draw_float31
  448. //! (i.e. str must include a ':' at its end)
  449. //! FLASH usage dropped 234476B -> 234392B
  450. //! Moreover, this function gets inlined in the final code, so removing it doesn't really help ;)
  451. void menu_draw_float13(const char* str, float val)
  452. {
  453. lcd_printf_P(menu_fmt_float13, ' ', str, val);
  454. }
  455. template <typename T>
  456. static void _menu_edit_P(void)
  457. {
  458. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  459. if (lcd_draw_update)
  460. {
  461. if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
  462. if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
  463. lcd_set_cursor(0, 1);
  464. menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
  465. }
  466. if (LCD_CLICKED)
  467. {
  468. *((T)(_md->editValue)) = lcd_encoder;
  469. menu_back_no_reset();
  470. }
  471. }
  472. template <typename T>
  473. uint8_t menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val)
  474. {
  475. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  476. if (menu_item == menu_line)
  477. {
  478. if (lcd_draw_update)
  479. {
  480. lcd_set_cursor(0, menu_row);
  481. menu_draw_P<T>(menu_selection_mark(), str, *pval);
  482. }
  483. if (menu_clicked && (lcd_encoder == menu_item))
  484. {
  485. menu_submenu_no_reset(_menu_edit_P<T>);
  486. _md->editLabel = str;
  487. _md->editValue = pval;
  488. _md->minEditValue = min_val;
  489. _md->maxEditValue = max_val;
  490. lcd_encoder = *pval;
  491. return menu_item_ret();
  492. }
  493. }
  494. menu_item++;
  495. return 0;
  496. }
  497. template uint8_t menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val);
  498. template uint8_t menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val);
  499. static uint8_t progressbar_block_count = 0;
  500. static uint16_t progressbar_total = 0;
  501. void menu_progressbar_init(uint16_t total, const char* title)
  502. {
  503. lcd_clear();
  504. progressbar_block_count = 0;
  505. progressbar_total = total;
  506. lcd_set_cursor(0, 1);
  507. lcd_printf_P(PSTR("%-20.20S\n"), title);
  508. }
  509. void menu_progressbar_update(uint16_t newVal)
  510. {
  511. uint8_t newCnt = (newVal * LCD_WIDTH) / progressbar_total;
  512. if (newCnt > LCD_WIDTH)
  513. newCnt = LCD_WIDTH;
  514. while (newCnt > progressbar_block_count)
  515. {
  516. lcd_print('\xFF');
  517. progressbar_block_count++;
  518. }
  519. }
  520. void menu_progressbar_finish(void)
  521. {
  522. progressbar_total = 1;
  523. menu_progressbar_update(1);
  524. _delay(300);
  525. }