menu.cpp 15 KB

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