menu.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. extern int32_t lcd_encoder;
  14. #define MENU_DEPTH_MAX 6
  15. static menu_record_t menu_stack[MENU_DEPTH_MAX];
  16. uint8_t menu_data[MENU_DATA_SIZE];
  17. #ifndef __AVR__
  18. #error "menu_data is non-portable to non 8bit processor"
  19. #endif
  20. uint8_t menu_depth = 0;
  21. uint8_t menu_block_entering_on_serious_errors = SERIOUS_ERR_NONE;
  22. uint8_t menu_line = 0;
  23. uint8_t menu_item = 0;
  24. uint8_t menu_row = 0;
  25. uint8_t menu_top = 0;
  26. uint8_t menu_clicked = 0;
  27. uint8_t menu_entering = 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_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
  32. {
  33. asm("cli");
  34. if (menu_menu != menu)
  35. {
  36. menu_menu = menu;
  37. lcd_encoder = encoder;
  38. asm("sei");
  39. if (reset_menu_state)
  40. {
  41. // Resets the global shared C union.
  42. // This ensures, that the menu entered will find out, that it shall initialize itself.
  43. memset(&menu_data, 0, sizeof(menu_data));
  44. }
  45. if (feedback) lcd_quick_feedback();
  46. }
  47. else
  48. asm("sei");
  49. }
  50. void menu_start(void)
  51. {
  52. if (lcd_encoder > 0x8000) lcd_encoder = 0;
  53. if (lcd_encoder < 0) lcd_encoder = 0;
  54. if (lcd_encoder < menu_top)
  55. menu_top = lcd_encoder;
  56. menu_line = menu_top;
  57. menu_clicked = LCD_CLICKED;
  58. }
  59. void menu_end(void)
  60. {
  61. if (lcd_encoder >= menu_item)
  62. lcd_encoder = menu_item - 1;
  63. if (((uint8_t)lcd_encoder) >= menu_top + LCD_HEIGHT)
  64. {
  65. menu_top = lcd_encoder - LCD_HEIGHT + 1;
  66. lcd_draw_update = 1;
  67. menu_line = menu_top - 1;
  68. menu_row = -1;
  69. }
  70. }
  71. void menu_back(uint8_t nLevel)
  72. {
  73. menu_depth = ((menu_depth > nLevel) ? (menu_depth - nLevel) : 0);
  74. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
  75. }
  76. void menu_back(void)
  77. {
  78. menu_back(1);
  79. }
  80. static void menu_back_no_reset(void)
  81. {
  82. if (menu_depth > 0)
  83. {
  84. menu_depth--;
  85. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
  86. }
  87. }
  88. void menu_back_if_clicked(void)
  89. {
  90. if (lcd_clicked())
  91. menu_back();
  92. }
  93. void menu_back_if_clicked_fb(void)
  94. {
  95. if (lcd_clicked())
  96. {
  97. lcd_quick_feedback();
  98. menu_back();
  99. }
  100. }
  101. void menu_submenu(menu_func_t submenu)
  102. {
  103. if (menu_depth < MENU_DEPTH_MAX)
  104. {
  105. menu_stack[menu_depth].menu = menu_menu;
  106. menu_stack[menu_depth++].position = lcd_encoder;
  107. menu_goto(submenu, 0, true, true);
  108. }
  109. }
  110. static void menu_submenu_no_reset(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, false);
  117. }
  118. }
  119. uint8_t menu_item_ret(void)
  120. {
  121. lcd_beeper_quick_feedback();
  122. lcd_draw_update = 2;
  123. lcd_button_pressed = false;
  124. return 1;
  125. }
  126. /*
  127. int menu_draw_item_printf_P(char type_char, const char* format, ...)
  128. {
  129. va_list args;
  130. va_start(args, format);
  131. int ret = 0;
  132. lcd_set_cursor(0, menu_row);
  133. if (lcd_encoder == menu_item)
  134. lcd_print('>');
  135. else
  136. lcd_print(' ');
  137. int cnt = vfprintf_P(lcdout, format, args);
  138. for (int i = cnt; i < 18; i++)
  139. lcd_print(' ');
  140. lcd_print(type_char);
  141. va_end(args);
  142. return ret;
  143. }
  144. */
  145. static void menu_draw_item_puts_P(char type_char, const char* str)
  146. {
  147. lcd_set_cursor(0, menu_row);
  148. lcd_printf_P(PSTR("%c%-18.18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  149. }
  150. static void menu_draw_item_puts_P(char type_char, const char *str_P, const Sheet &sheet)
  151. {
  152. lcd_set_cursor(0, menu_row);
  153. char buffer[19];
  154. uint_least8_t index = sprintf_P(buffer, PSTR("%.10S "), str_P);
  155. eeprom_read_block(&(buffer[index]), sheet.name, 7);
  156. index += 7;
  157. buffer[index] = '\0';
  158. lcd_printf_P(PSTR("%c%-18.18s%c"), (lcd_encoder == menu_item)?'>':' ', buffer, type_char);
  159. }
  160. static void menu_draw_item_puts_P(char type_char, const char* str, char num)
  161. {
  162. lcd_set_cursor(0, menu_row);
  163. lcd_printf_P(PSTR("%c%-.16S "), (lcd_encoder == menu_item)?'>':' ', str);
  164. lcd_putc(num);
  165. lcd_set_cursor(19, menu_row);
  166. lcd_putc(type_char);
  167. }
  168. /*
  169. int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
  170. {
  171. lcd_set_cursor(0, menu_row);
  172. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  173. return cnt;
  174. }
  175. */
  176. void menu_item_dummy(void)
  177. {
  178. menu_item++;
  179. }
  180. uint8_t menu_item_text_P(const char* str)
  181. {
  182. if (menu_item == menu_line)
  183. {
  184. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  185. if (menu_clicked && (lcd_encoder == menu_item))
  186. return menu_item_ret();
  187. }
  188. menu_item++;
  189. return 0;
  190. }
  191. uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
  192. {
  193. if (menu_item == menu_line)
  194. {
  195. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
  196. if (menu_clicked && (lcd_encoder == menu_item))
  197. {
  198. menu_submenu(submenu);
  199. return menu_item_ret();
  200. }
  201. }
  202. menu_item++;
  203. return 0;
  204. }
  205. uint8_t menu_item_submenu_P(const char* str_P, const Sheet &sheet, menu_func_t submenu)
  206. {
  207. if (menu_item == menu_line)
  208. {
  209. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str_P, sheet);
  210. if (menu_clicked && (lcd_encoder == menu_item))
  211. {
  212. menu_submenu(submenu);
  213. return menu_item_ret();
  214. }
  215. }
  216. menu_item++;
  217. return 0;
  218. }
  219. uint8_t menu_item_back_P(const char* str)
  220. {
  221. if (menu_item == menu_line)
  222. {
  223. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
  224. if (menu_clicked && (lcd_encoder == menu_item))
  225. {
  226. menu_back();
  227. return menu_item_ret();
  228. }
  229. }
  230. menu_item++;
  231. return 0;
  232. }
  233. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  234. {
  235. if (menu_item == menu_line)
  236. {
  237. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  238. if (menu_clicked && (lcd_encoder == menu_item))
  239. {
  240. menu_clicked = false;
  241. lcd_consume_click();
  242. lcd_update_enabled = 0;
  243. if (func) func();
  244. lcd_update_enabled = 1;
  245. return menu_item_ret();
  246. }
  247. }
  248. menu_item++;
  249. return 0;
  250. }
  251. //! @brief Menu item function taking single parameter
  252. //!
  253. //! Ideal for numbered lists calling functions with number parameter.
  254. //! @param str Item caption
  255. //! @param number aditional character to be added after str, e.g. number
  256. //! @param func pointer to function taking uint8_t with no return value
  257. //! @param fn_par value to be passed to function
  258. //! @retval 0
  259. //! @retval 1 Item was clicked
  260. uint8_t menu_item_function_P(const char* str, char number, void (*func)(uint8_t), uint8_t fn_par)
  261. {
  262. if (menu_item == menu_line)
  263. {
  264. if (lcd_draw_update) menu_draw_item_puts_P(' ', str, number);
  265. if (menu_clicked && (lcd_encoder == menu_item))
  266. {
  267. menu_clicked = false;
  268. lcd_consume_click();
  269. lcd_update_enabled = 0;
  270. if (func) func(fn_par);
  271. lcd_update_enabled = 1;
  272. return menu_item_ret();
  273. }
  274. }
  275. menu_item++;
  276. return 0;
  277. }
  278. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  279. {
  280. if (menu_item == menu_line)
  281. {
  282. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  283. if (menu_clicked && (lcd_encoder == menu_item))
  284. {
  285. if (str_gcode) enquecommand_P(str_gcode);
  286. return menu_item_ret();
  287. }
  288. }
  289. menu_item++;
  290. return 0;
  291. }
  292. const char menu_20x_space[] PROGMEM = " ";
  293. const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
  294. const char menu_fmt_float31[] PROGMEM = "%-12.12S%+8.1f";
  295. const char menu_fmt_float13[] PROGMEM = "%c%-13.13S%+5.3f";
  296. const char menu_fmt_float13off[] PROGMEM = "%c%-13.13S%6.6s";
  297. template<typename T>
  298. static void menu_draw_P(char chr, const char* str, int16_t val);
  299. template<>
  300. void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
  301. {
  302. int text_len = strlen_P(str);
  303. if (text_len > 15) text_len = 15;
  304. char spaces[21];
  305. strcpy_P(spaces, menu_20x_space);
  306. if (val <= -100) spaces[15 - text_len - 1] = 0;
  307. else spaces[15 - text_len] = 0;
  308. lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
  309. }
  310. template<>
  311. void menu_draw_P<uint8_t*>(char chr, const char* str, int16_t val)
  312. {
  313. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  314. float factor = 1.0f + static_cast<float>(val) / 1000.0f;
  315. if (val <= _md->minEditValue)
  316. {
  317. lcd_printf_P(menu_fmt_float13off, chr, str, " [off]");
  318. }
  319. else
  320. {
  321. lcd_printf_P(menu_fmt_float13, chr, str, factor);
  322. }
  323. }
  324. //! @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
  325. //! for displaying large values of extruder positions, which caused text overflow in the previous implementation.
  326. //!
  327. //! @param str string label to print
  328. //! @param val value to print aligned to the right side of the display
  329. //!
  330. //! Implementation comments:
  331. //! The text needs to come with a colon ":", this function does not append it anymore.
  332. //! That resulted in a much shorter implementation (234628B -> 234476B)
  333. //! There are similar functions around which may be shortened in a similar way
  334. void menu_draw_float31(const char* str, float val)
  335. {
  336. lcd_printf_P(menu_fmt_float31, str, val);
  337. }
  338. //! @brief Draw up to 14 chars of text and a float number in format +1.234
  339. //!
  340. //! @param str string label to print
  341. //! @param val value to print aligned to the right side of the display
  342. //!
  343. //! Implementation comments:
  344. //! This function uses similar optimization principles as menu_draw_float31
  345. //! (i.e. str must include a ':' at its end)
  346. //! FLASH usage dropped 234476B -> 234392B
  347. //! Moreover, this function gets inlined in the final code, so removing it doesn't really help ;)
  348. void menu_draw_float13(const char* str, float val)
  349. {
  350. lcd_printf_P(menu_fmt_float13, ' ', str, val);
  351. }
  352. template <typename T>
  353. static void _menu_edit_P(void)
  354. {
  355. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  356. if (lcd_draw_update)
  357. {
  358. if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
  359. if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
  360. lcd_set_cursor(0, 1);
  361. menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
  362. }
  363. if (LCD_CLICKED)
  364. {
  365. *((T)(_md->editValue)) = lcd_encoder;
  366. menu_back_no_reset();
  367. }
  368. }
  369. template <typename T>
  370. uint8_t menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val)
  371. {
  372. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  373. if (menu_item == menu_line)
  374. {
  375. if (lcd_draw_update)
  376. {
  377. lcd_set_cursor(0, menu_row);
  378. menu_draw_P<T>((lcd_encoder == menu_item)?'>':' ', str, *pval);
  379. }
  380. if (menu_clicked && (lcd_encoder == menu_item))
  381. {
  382. menu_submenu_no_reset(_menu_edit_P<T>);
  383. _md->editLabel = str;
  384. _md->editValue = pval;
  385. _md->minEditValue = min_val;
  386. _md->maxEditValue = max_val;
  387. lcd_encoder = *pval;
  388. return menu_item_ret();
  389. }
  390. }
  391. menu_item++;
  392. return 0;
  393. }
  394. template uint8_t menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val);
  395. template uint8_t menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val);
  396. #undef _menu_data