menu.cpp 10 KB

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