menu.cpp 13 KB

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