menu.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_line = 0;
  22. uint8_t menu_item = 0;
  23. uint8_t menu_row = 0;
  24. uint8_t menu_top = 0;
  25. uint8_t menu_clicked = 0;
  26. uint8_t menu_entering = 0;
  27. uint8_t menu_leaving = 0;
  28. menu_func_t menu_menu = 0;
  29. static_assert(sizeof(menu_data)>= sizeof(menu_data_edit_t),"menu_data_edit_t doesn't fit into menu_data");
  30. void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
  31. {
  32. asm("cli");
  33. if (menu_menu != menu)
  34. {
  35. menu_menu = menu;
  36. lcd_encoder = encoder;
  37. asm("sei");
  38. if (reset_menu_state)
  39. {
  40. // Resets the global shared C union.
  41. // This ensures, that the menu entered will find out, that it shall initialize itself.
  42. memset(&menu_data, 0, sizeof(menu_data));
  43. }
  44. if (feedback) lcd_quick_feedback();
  45. }
  46. else
  47. asm("sei");
  48. }
  49. void menu_start(void)
  50. {
  51. if (lcd_encoder > 0x8000) lcd_encoder = 0;
  52. if (lcd_encoder < 0) lcd_encoder = 0;
  53. if (lcd_encoder < menu_top)
  54. menu_top = lcd_encoder;
  55. menu_line = menu_top;
  56. menu_clicked = LCD_CLICKED;
  57. }
  58. void menu_end(void)
  59. {
  60. if (lcd_encoder >= menu_item)
  61. lcd_encoder = menu_item - 1;
  62. if (((uint8_t)lcd_encoder) >= menu_top + LCD_HEIGHT)
  63. {
  64. menu_top = lcd_encoder - LCD_HEIGHT + 1;
  65. lcd_draw_update = 1;
  66. menu_line = menu_top - 1;
  67. menu_row = -1;
  68. }
  69. }
  70. void menu_back(uint8_t nLevel)
  71. {
  72. menu_depth = ((menu_depth > nLevel) ? (menu_depth - nLevel) : 0);
  73. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
  74. }
  75. void menu_back(void)
  76. {
  77. menu_back(1);
  78. }
  79. static void menu_back_no_reset(void)
  80. {
  81. if (menu_depth > 0)
  82. {
  83. menu_depth--;
  84. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
  85. }
  86. }
  87. void menu_back_if_clicked(void)
  88. {
  89. if (lcd_clicked())
  90. menu_back();
  91. }
  92. void menu_back_if_clicked_fb(void)
  93. {
  94. if (lcd_clicked())
  95. {
  96. lcd_quick_feedback();
  97. menu_back();
  98. }
  99. }
  100. void menu_submenu(menu_func_t submenu)
  101. {
  102. if (menu_depth < MENU_DEPTH_MAX)
  103. {
  104. menu_stack[menu_depth].menu = menu_menu;
  105. menu_stack[menu_depth++].position = lcd_encoder;
  106. menu_goto(submenu, 0, true, true);
  107. }
  108. }
  109. static void menu_submenu_no_reset(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, false);
  116. }
  117. }
  118. uint8_t menu_item_ret(void)
  119. {
  120. lcd_beeper_quick_feedback();
  121. lcd_draw_update = 2;
  122. lcd_button_pressed = false;
  123. return 1;
  124. }
  125. /*
  126. int menu_draw_item_printf_P(char type_char, const char* format, ...)
  127. {
  128. va_list args;
  129. va_start(args, format);
  130. int ret = 0;
  131. lcd_set_cursor(0, menu_row);
  132. if (lcd_encoder == menu_item)
  133. lcd_print('>');
  134. else
  135. lcd_print(' ');
  136. int cnt = vfprintf_P(lcdout, format, args);
  137. for (int i = cnt; i < 18; i++)
  138. lcd_print(' ');
  139. lcd_print(type_char);
  140. va_end(args);
  141. return ret;
  142. }
  143. */
  144. static void menu_draw_item_puts_P(char type_char, const char* str)
  145. {
  146. lcd_set_cursor(0, menu_row);
  147. lcd_printf_P(PSTR("%c%-18.18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  148. }
  149. static int menu_draw_item_puts_P(char type_char, const char* str, char num)
  150. {
  151. lcd_set_cursor(0, menu_row);
  152. lcd_printf_P(PSTR("%c%-.16S "), (lcd_encoder == menu_item)?'>':' ', str);
  153. lcd_putc(num);
  154. lcd_set_cursor(19, menu_row);
  155. lcd_putc(type_char);
  156. }
  157. /*
  158. int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
  159. {
  160. lcd_set_cursor(0, menu_row);
  161. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  162. return cnt;
  163. }
  164. */
  165. void menu_item_dummy(void)
  166. {
  167. menu_item++;
  168. }
  169. uint8_t menu_item_text_P(const char* str)
  170. {
  171. if (menu_item == menu_line)
  172. {
  173. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  174. if (menu_clicked && (lcd_encoder == menu_item))
  175. return menu_item_ret();
  176. }
  177. menu_item++;
  178. return 0;
  179. }
  180. uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
  181. {
  182. if (menu_item == menu_line)
  183. {
  184. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
  185. if (menu_clicked && (lcd_encoder == menu_item))
  186. {
  187. menu_submenu(submenu);
  188. return menu_item_ret();
  189. }
  190. }
  191. menu_item++;
  192. return 0;
  193. }
  194. uint8_t menu_item_back_P(const char* str)
  195. {
  196. if (menu_item == menu_line)
  197. {
  198. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
  199. if (menu_clicked && (lcd_encoder == menu_item))
  200. {
  201. menu_back();
  202. return menu_item_ret();
  203. }
  204. }
  205. menu_item++;
  206. return 0;
  207. }
  208. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  209. {
  210. if (menu_item == menu_line)
  211. {
  212. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  213. if (menu_clicked && (lcd_encoder == menu_item))
  214. {
  215. menu_clicked = false;
  216. lcd_consume_click();
  217. lcd_update_enabled = 0;
  218. if (func) func();
  219. lcd_update_enabled = 1;
  220. return menu_item_ret();
  221. }
  222. }
  223. menu_item++;
  224. return 0;
  225. }
  226. //! @brief Menu item function taking single parameter
  227. //!
  228. //! Ideal for numbered lists calling functions with number parameter.
  229. //! @param str Item caption
  230. //! @param number aditional character to be added after str, e.g. number
  231. //! @param func pointer to function taking uint8_t with no return value
  232. //! @param fn_par value to be passed to function
  233. //! @retval 0
  234. //! @retval 1 Item was clicked
  235. uint8_t menu_item_function_P(const char* str, char number, void (*func)(uint8_t), uint8_t fn_par)
  236. {
  237. if (menu_item == menu_line)
  238. {
  239. if (lcd_draw_update) menu_draw_item_puts_P(' ', str, number);
  240. if (menu_clicked && (lcd_encoder == menu_item))
  241. {
  242. menu_clicked = false;
  243. lcd_consume_click();
  244. lcd_update_enabled = 0;
  245. if (func) func(fn_par);
  246. lcd_update_enabled = 1;
  247. return menu_item_ret();
  248. }
  249. }
  250. menu_item++;
  251. return 0;
  252. }
  253. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  254. {
  255. if (menu_item == menu_line)
  256. {
  257. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  258. if (menu_clicked && (lcd_encoder == menu_item))
  259. {
  260. if (str_gcode) enquecommand_P(str_gcode);
  261. return menu_item_ret();
  262. }
  263. }
  264. menu_item++;
  265. return 0;
  266. }
  267. const char menu_20x_space[] PROGMEM = " ";
  268. const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
  269. const char menu_fmt_float31[] PROGMEM = "%-12.12s%+8.1f";
  270. const char menu_fmt_float13[] PROGMEM = "%c%.12S:%s%+06.3f";
  271. const char menu_fmt_float13off[] PROGMEM = "%c%.12S:%s%";
  272. template<typename T>
  273. static void menu_draw_P(char chr, const char* str, int16_t val);
  274. template<>
  275. void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
  276. {
  277. int text_len = strlen_P(str);
  278. if (text_len > 15) text_len = 15;
  279. char spaces[21];
  280. strcpy_P(spaces, menu_20x_space);
  281. if (val <= -100) spaces[15 - text_len - 1] = 0;
  282. else spaces[15 - text_len] = 0;
  283. lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
  284. }
  285. template<>
  286. void menu_draw_P<uint8_t*>(char chr, const char* str, int16_t val)
  287. {
  288. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  289. int text_len = strlen_P(str);
  290. if (text_len > 15) text_len = 15;
  291. char spaces[21];
  292. strcpy_P(spaces, menu_20x_space);
  293. spaces[12 - text_len] = 0;
  294. float factor = 1.0 + static_cast<float>(val) / 1000.0;
  295. if (val <= _md->minEditValue)
  296. {
  297. lcd_printf_P(menu_fmt_float13off, chr, str, spaces);
  298. lcd_puts_P(_i(" [off]"));
  299. }
  300. else
  301. {
  302. lcd_printf_P(menu_fmt_float13, chr, str, spaces, factor);
  303. }
  304. }
  305. //! Draw up to 10 chars of text, ':' and float number in format from +0.0 to +12345.0. The increased range is necessary
  306. //! for displaying large values of extruder positions, which caused text overflow in the previous implementation.
  307. //! @param chr first character to print on the line
  308. //! @param str string label to print, will be appended with ':' automatically inside the function
  309. //! @param val value to print aligned to the right side of the display
  310. //!
  311. //! Implementation comments:
  312. //! The text needs to be prerendered into the prerendered[] to enable left alignment of text str including the colon behind it.
  313. //! If we didn't want the colon behind it, the whole operation would have been solved with a single vsprintf call,
  314. //! but such line would look different compared to every other similar menu item
  315. //! So it is almost the same amount of code like before, but with added string prerendering
  316. void menu_draw_float31(char chr, const char* str, float val)
  317. {
  318. uint8_t txtlen = strlen_P(str);
  319. if( txtlen > 10 )txtlen = 10;
  320. char prerendered[21];
  321. strcpy_P(prerendered, menu_20x_space);
  322. prerendered[0] = chr; // start with the initial byte/space for menu navigation
  323. strncpy_P(prerendered+1, str, 10); // render the text and limit it to max 10 characters
  324. prerendered[txtlen+1] = ':'; // put the colon behind it
  325. prerendered[txtlen+2] = 0; // terminate the string to be used inside the printf
  326. lcd_printf_P(menu_fmt_float31, prerendered, val);
  327. }
  328. //draw up to 12 chars of text, ':' and float number in format +1.234
  329. void menu_draw_float13(char chr, const char* str, float val)
  330. {
  331. int text_len = strlen_P(str);
  332. if (text_len > 12) text_len = 12;
  333. char spaces[21];
  334. strcpy_P(spaces, menu_20x_space);
  335. spaces[12 - text_len] = 0;
  336. lcd_printf_P(menu_fmt_float13, chr, str, spaces, val);
  337. }
  338. template <typename T>
  339. static void _menu_edit_P(void)
  340. {
  341. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  342. if (lcd_draw_update)
  343. {
  344. if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
  345. if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
  346. lcd_set_cursor(0, 1);
  347. menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
  348. }
  349. if (LCD_CLICKED)
  350. {
  351. *((T)(_md->editValue)) = lcd_encoder;
  352. menu_back_no_reset();
  353. }
  354. }
  355. template <typename T>
  356. uint8_t menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val)
  357. {
  358. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  359. if (menu_item == menu_line)
  360. {
  361. if (lcd_draw_update)
  362. {
  363. lcd_set_cursor(0, menu_row);
  364. menu_draw_P<T>((lcd_encoder == menu_item)?'>':' ', str, *pval);
  365. }
  366. if (menu_clicked && (lcd_encoder == menu_item))
  367. {
  368. menu_submenu_no_reset(_menu_edit_P<T>);
  369. _md->editLabel = str;
  370. _md->editValue = pval;
  371. _md->minEditValue = min_val;
  372. _md->maxEditValue = max_val;
  373. lcd_encoder = *pval;
  374. return menu_item_ret();
  375. }
  376. }
  377. menu_item++;
  378. return 0;
  379. }
  380. template uint8_t menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val);
  381. template uint8_t menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val);
  382. #undef _menu_data