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 "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_leaving = 0;
  29. menu_func_t menu_menu = 0;
  30. static_assert(sizeof(menu_data)>= sizeof(menu_data_edit_t),"menu_data_edit_t doesn't fit into menu_data");
  31. void menu_data_reset(void)
  32. {
  33. // Resets the global shared C union.
  34. // This ensures, that the menu entered will find out, that it shall initialize itself.
  35. memset(&menu_data, 0, sizeof(menu_data));
  36. }
  37. void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
  38. {
  39. CRITICAL_SECTION_START;
  40. if (menu_menu != menu)
  41. {
  42. menu_menu = menu;
  43. lcd_encoder = encoder;
  44. CRITICAL_SECTION_END;
  45. if (reset_menu_state)
  46. menu_data_reset();
  47. if (feedback) lcd_quick_feedback();
  48. }
  49. else
  50. CRITICAL_SECTION_END;
  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. 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_if_clicked(void)
  98. {
  99. if (lcd_clicked())
  100. menu_back();
  101. }
  102. void menu_back_if_clicked_fb(void)
  103. {
  104. if (lcd_clicked())
  105. {
  106. lcd_quick_feedback();
  107. menu_back();
  108. }
  109. }
  110. void menu_submenu(menu_func_t submenu)
  111. {
  112. if (menu_depth < MENU_DEPTH_MAX)
  113. {
  114. menu_stack[menu_depth].menu = menu_menu;
  115. menu_stack[menu_depth++].position = lcd_encoder;
  116. menu_goto(submenu, 0, true, true);
  117. }
  118. }
  119. void menu_submenu_no_reset(menu_func_t submenu)
  120. {
  121. if (menu_depth < MENU_DEPTH_MAX)
  122. {
  123. menu_stack[menu_depth].menu = menu_menu;
  124. menu_stack[menu_depth++].position = lcd_encoder;
  125. menu_goto(submenu, 0, true, false);
  126. }
  127. }
  128. uint8_t menu_item_ret(void)
  129. {
  130. lcd_beeper_quick_feedback();
  131. lcd_draw_update = 2;
  132. lcd_button_pressed = false;
  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_set_cursor(19, menu_row);
  223. lcd_putc(type_char);
  224. }
  225. /*
  226. int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
  227. {
  228. lcd_set_cursor(0, menu_row);
  229. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  230. return cnt;
  231. }
  232. */
  233. void menu_item_dummy(void)
  234. {
  235. menu_item++;
  236. }
  237. uint8_t menu_item_text_P(const char* str)
  238. {
  239. if (menu_item == menu_line)
  240. {
  241. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  242. if (menu_clicked && (lcd_encoder == menu_item))
  243. return menu_item_ret();
  244. }
  245. menu_item++;
  246. return 0;
  247. }
  248. uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
  249. {
  250. if (menu_item == menu_line)
  251. {
  252. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
  253. if (menu_clicked && (lcd_encoder == menu_item))
  254. {
  255. menu_submenu(submenu);
  256. return menu_item_ret();
  257. }
  258. }
  259. menu_item++;
  260. return 0;
  261. }
  262. uint8_t menu_item_submenu_E(const Sheet &sheet, menu_func_t submenu)
  263. {
  264. if (menu_item == menu_line)
  265. {
  266. if (lcd_draw_update) menu_draw_item_puts_E(LCD_STR_ARROW_RIGHT[0], sheet);
  267. if (menu_clicked && (lcd_encoder == menu_item))
  268. {
  269. menu_submenu(submenu);
  270. return menu_item_ret();
  271. }
  272. }
  273. menu_item++;
  274. return 0;
  275. }
  276. uint8_t menu_item_function_E(const Sheet &sheet, menu_func_t func)
  277. {
  278. if (menu_item == menu_line)
  279. {
  280. if (lcd_draw_update) menu_draw_item_select_sheet_E(' ', sheet);
  281. if (menu_clicked && (lcd_encoder == menu_item))
  282. {
  283. menu_clicked = false;
  284. lcd_consume_click();
  285. lcd_update_enabled = 0;
  286. if (func) func();
  287. lcd_update_enabled = 1;
  288. return menu_item_ret();
  289. }
  290. }
  291. menu_item++;
  292. return 0;
  293. }
  294. uint8_t menu_item_back_P(const char* str)
  295. {
  296. if (menu_item == menu_line)
  297. {
  298. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
  299. if (menu_clicked && (lcd_encoder == menu_item))
  300. {
  301. menu_back();
  302. return menu_item_ret();
  303. }
  304. }
  305. menu_item++;
  306. return 0;
  307. }
  308. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  309. {
  310. if (menu_item == menu_line)
  311. {
  312. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  313. if (menu_clicked && (lcd_encoder == menu_item))
  314. {
  315. menu_clicked = false;
  316. lcd_consume_click();
  317. lcd_update_enabled = 0;
  318. if (func) func();
  319. lcd_update_enabled = 1;
  320. return menu_item_ret();
  321. }
  322. }
  323. menu_item++;
  324. return 0;
  325. }
  326. //! @brief Menu item function taking single parameter
  327. //!
  328. //! Ideal for numbered lists calling functions with number parameter.
  329. //! @param str Item caption
  330. //! @param number aditional character to be added after str, e.g. number
  331. //! @param func pointer to function taking uint8_t with no return value
  332. //! @param fn_par value to be passed to function
  333. //! @retval 0
  334. //! @retval 1 Item was clicked
  335. uint8_t menu_item_function_P(const char* str, char number, void (*func)(uint8_t), uint8_t fn_par)
  336. {
  337. if (menu_item == menu_line)
  338. {
  339. if (lcd_draw_update) menu_draw_item_puts_P(' ', str, number);
  340. if (menu_clicked && (lcd_encoder == menu_item))
  341. {
  342. menu_clicked = false;
  343. lcd_consume_click();
  344. lcd_update_enabled = 0;
  345. if (func) func(fn_par);
  346. lcd_update_enabled = 1;
  347. return menu_item_ret();
  348. }
  349. }
  350. menu_item++;
  351. return 0;
  352. }
  353. uint8_t menu_item_toggle_P(const char* str, const char* toggle, menu_func_t func, const uint8_t settings)
  354. {
  355. if (menu_item == menu_line)
  356. {
  357. if (lcd_draw_update) menu_draw_toggle_puts_P(str, toggle, settings | (menu_selection_mark()=='>'));
  358. if (menu_clicked && (lcd_encoder == menu_item))
  359. {
  360. if (toggle == NULL) // print N/A warning message
  361. {
  362. menu_submenu(func);
  363. return menu_item_ret();
  364. }
  365. else // do the actual toggling
  366. {
  367. menu_clicked = false;
  368. lcd_consume_click();
  369. lcd_update_enabled = 0;
  370. if (func) func();
  371. lcd_update_enabled = 1;
  372. return menu_item_ret();
  373. }
  374. }
  375. }
  376. menu_item++;
  377. return 0;
  378. }
  379. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  380. {
  381. if (menu_item == menu_line)
  382. {
  383. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  384. if (menu_clicked && (lcd_encoder == menu_item))
  385. {
  386. if (str_gcode) enquecommand_P(str_gcode);
  387. return menu_item_ret();
  388. }
  389. }
  390. menu_item++;
  391. return 0;
  392. }
  393. const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
  394. const char menu_fmt_float31[] PROGMEM = "%-12.12S%+8.1f";
  395. const char menu_fmt_float13[] PROGMEM = "%c%-13.13S%+5.3f";
  396. template<typename T>
  397. static void menu_draw_P(char chr, const char* str, int16_t val);
  398. template<>
  399. void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
  400. {
  401. int text_len = strlen_P(str);
  402. if (text_len > 15) text_len = 15;
  403. char spaces[LCD_WIDTH + 1] = {0};
  404. memset(spaces,' ', LCD_WIDTH);
  405. if (val <= -100) spaces[15 - text_len - 1] = 0;
  406. else spaces[15 - text_len] = 0;
  407. lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
  408. }
  409. template<>
  410. void menu_draw_P<uint8_t*>(char chr, const char* str, int16_t val)
  411. {
  412. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  413. float factor = 1.0f + static_cast<float>(val) / 1000.0f;
  414. if (val <= _md->minEditValue)
  415. {
  416. menu_draw_toggle_puts_P(str, _T(MSG_OFF), 0x04 | 0x02 | (chr=='>'));
  417. }
  418. else
  419. {
  420. lcd_printf_P(menu_fmt_float13, chr, str, factor);
  421. }
  422. }
  423. //! @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
  424. //! for displaying large values of extruder positions, which caused text overflow in the previous implementation.
  425. //!
  426. //! @param str string label to print
  427. //! @param val value to print aligned to the right side of the display
  428. //!
  429. //! Implementation comments:
  430. //! The text needs to come with a colon ":", this function does not append it anymore.
  431. //! That resulted in a much shorter implementation (234628B -> 234476B)
  432. //! There are similar functions around which may be shortened in a similar way
  433. void menu_draw_float31(const char* str, float val)
  434. {
  435. lcd_printf_P(menu_fmt_float31, str, val);
  436. }
  437. //! @brief Draw up to 14 chars of text and a float number in format +1.234
  438. //!
  439. //! @param str string label to print
  440. //! @param val value to print aligned to the right side of the display
  441. //!
  442. //! Implementation comments:
  443. //! This function uses similar optimization principles as menu_draw_float31
  444. //! (i.e. str must include a ':' at its end)
  445. //! FLASH usage dropped 234476B -> 234392B
  446. //! Moreover, this function gets inlined in the final code, so removing it doesn't really help ;)
  447. void menu_draw_float13(const char* str, float val)
  448. {
  449. lcd_printf_P(menu_fmt_float13, ' ', str, val);
  450. }
  451. template <typename T>
  452. static void _menu_edit_P(void)
  453. {
  454. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  455. if (lcd_draw_update)
  456. {
  457. if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
  458. if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
  459. lcd_set_cursor(0, 1);
  460. menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
  461. }
  462. if (LCD_CLICKED)
  463. {
  464. *((T)(_md->editValue)) = lcd_encoder;
  465. menu_back_no_reset();
  466. }
  467. }
  468. template <typename T>
  469. uint8_t menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val)
  470. {
  471. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  472. if (menu_item == menu_line)
  473. {
  474. if (lcd_draw_update)
  475. {
  476. lcd_set_cursor(0, menu_row);
  477. menu_draw_P<T>(menu_selection_mark(), str, *pval);
  478. }
  479. if (menu_clicked && (lcd_encoder == menu_item))
  480. {
  481. menu_submenu_no_reset(_menu_edit_P<T>);
  482. _md->editLabel = str;
  483. _md->editValue = pval;
  484. _md->minEditValue = min_val;
  485. _md->maxEditValue = max_val;
  486. lcd_encoder = *pval;
  487. return menu_item_ret();
  488. }
  489. }
  490. menu_item++;
  491. return 0;
  492. }
  493. template uint8_t menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val);
  494. template uint8_t menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val);
  495. #undef _menu_data