menu.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. extern int32_t lcd_encoder;
  13. #define MENU_DEPTH_MAX 4
  14. static menu_record_t menu_stack[MENU_DEPTH_MAX];
  15. uint8_t menu_data[MENU_DATA_SIZE];
  16. #ifndef __AVR__
  17. #error "menu_data is non-portable to non 8bit processor"
  18. #endif
  19. uint8_t menu_depth = 0;
  20. uint8_t menu_line = 0;
  21. uint8_t menu_item = 0;
  22. uint8_t menu_row = 0;
  23. uint8_t menu_top = 0;
  24. uint8_t menu_clicked = 0;
  25. uint8_t menu_entering = 0;
  26. uint8_t menu_leaving = 0;
  27. menu_func_t menu_menu = 0;
  28. static_assert(sizeof(menu_data)>= sizeof(menu_data_edit_t),"menu_data_edit_t doesn't fit into menu_data");
  29. void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
  30. {
  31. asm("cli");
  32. if (menu_menu != menu)
  33. {
  34. menu_menu = menu;
  35. lcd_encoder = encoder;
  36. asm("sei");
  37. if (reset_menu_state)
  38. {
  39. // Resets the global shared C union.
  40. // This ensures, that the menu entered will find out, that it shall initialize itself.
  41. memset(&menu_data, 0, sizeof(menu_data));
  42. }
  43. if (feedback) lcd_quick_feedback();
  44. }
  45. else
  46. asm("sei");
  47. }
  48. void menu_start(void)
  49. {
  50. if (lcd_encoder > 0x8000) lcd_encoder = 0;
  51. if (lcd_encoder < 0) lcd_encoder = 0;
  52. if (lcd_encoder < menu_top)
  53. menu_top = lcd_encoder;
  54. menu_line = menu_top;
  55. menu_clicked = LCD_CLICKED;
  56. }
  57. void menu_end(void)
  58. {
  59. if (lcd_encoder >= menu_item)
  60. lcd_encoder = menu_item - 1;
  61. if (((uint8_t)lcd_encoder) >= menu_top + LCD_HEIGHT)
  62. {
  63. menu_top = lcd_encoder - LCD_HEIGHT + 1;
  64. lcd_draw_update = 1;
  65. menu_line = menu_top - 1;
  66. menu_row = -1;
  67. }
  68. }
  69. void menu_back(void)
  70. {
  71. if (menu_depth > 0)
  72. {
  73. menu_depth--;
  74. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
  75. }
  76. }
  77. static void menu_back_no_reset(void)
  78. {
  79. if (menu_depth > 0)
  80. {
  81. menu_depth--;
  82. menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
  83. }
  84. }
  85. void menu_back_if_clicked(void)
  86. {
  87. if (lcd_clicked())
  88. menu_back();
  89. }
  90. void menu_back_if_clicked_fb(void)
  91. {
  92. if (lcd_clicked())
  93. {
  94. lcd_quick_feedback();
  95. menu_back();
  96. }
  97. }
  98. void menu_submenu(menu_func_t submenu)
  99. {
  100. if (menu_depth <= MENU_DEPTH_MAX)
  101. {
  102. menu_stack[menu_depth].menu = menu_menu;
  103. menu_stack[menu_depth++].position = lcd_encoder;
  104. menu_goto(submenu, 0, true, true);
  105. }
  106. }
  107. static void menu_submenu_no_reset(menu_func_t submenu)
  108. {
  109. if (menu_depth <= MENU_DEPTH_MAX)
  110. {
  111. menu_stack[menu_depth].menu = menu_menu;
  112. menu_stack[menu_depth++].position = lcd_encoder;
  113. menu_goto(submenu, 0, true, false);
  114. }
  115. }
  116. uint8_t menu_item_ret(void)
  117. {
  118. lcd_beeper_quick_feedback();
  119. lcd_draw_update = 2;
  120. lcd_button_pressed = false;
  121. return 1;
  122. }
  123. /*
  124. int menu_draw_item_printf_P(char type_char, const char* format, ...)
  125. {
  126. va_list args;
  127. va_start(args, format);
  128. int ret = 0;
  129. lcd_set_cursor(0, menu_row);
  130. if (lcd_encoder == menu_item)
  131. lcd_print('>');
  132. else
  133. lcd_print(' ');
  134. int cnt = vfprintf_P(lcdout, format, args);
  135. for (int i = cnt; i < 18; i++)
  136. lcd_print(' ');
  137. lcd_print(type_char);
  138. va_end(args);
  139. return ret;
  140. }
  141. */
  142. static int menu_draw_item_puts_P(char type_char, const char* str)
  143. {
  144. lcd_set_cursor(0, menu_row);
  145. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  146. return cnt;
  147. }
  148. /*
  149. int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
  150. {
  151. lcd_set_cursor(0, menu_row);
  152. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  153. return cnt;
  154. }
  155. */
  156. void menu_item_dummy(void)
  157. {
  158. menu_item++;
  159. }
  160. uint8_t menu_item_text_P(const char* str)
  161. {
  162. if (menu_item == menu_line)
  163. {
  164. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  165. if (menu_clicked && (lcd_encoder == menu_item))
  166. return menu_item_ret();
  167. }
  168. menu_item++;
  169. return 0;
  170. }
  171. uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
  172. {
  173. if (menu_item == menu_line)
  174. {
  175. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
  176. if (menu_clicked && (lcd_encoder == menu_item))
  177. {
  178. menu_submenu(submenu);
  179. return menu_item_ret();
  180. }
  181. }
  182. menu_item++;
  183. return 0;
  184. }
  185. uint8_t menu_item_back_P(const char* str)
  186. {
  187. if (menu_item == menu_line)
  188. {
  189. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
  190. if (menu_clicked && (lcd_encoder == menu_item))
  191. {
  192. menu_back();
  193. return menu_item_ret();
  194. }
  195. }
  196. menu_item++;
  197. return 0;
  198. }
  199. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  200. {
  201. if (menu_item == menu_line)
  202. {
  203. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  204. if (menu_clicked && (lcd_encoder == menu_item))
  205. {
  206. menu_clicked = false;
  207. lcd_button_pressed = 0;
  208. lcd_buttons &= 0xff^EN_C;
  209. lcd_update_enabled = 0;
  210. if (func) func();
  211. lcd_update_enabled = 1;
  212. return menu_item_ret();
  213. }
  214. }
  215. menu_item++;
  216. return 0;
  217. }
  218. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  219. {
  220. if (menu_item == menu_line)
  221. {
  222. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  223. if (menu_clicked && (lcd_encoder == menu_item))
  224. {
  225. if (str_gcode) enquecommand_P(str_gcode);
  226. return menu_item_ret();
  227. }
  228. }
  229. menu_item++;
  230. return 0;
  231. }
  232. const char menu_20x_space[] PROGMEM = " ";
  233. const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
  234. const char menu_fmt_float31[] PROGMEM = "%c%.12S:%s%+06.1f";
  235. const char menu_fmt_float13[] PROGMEM = "%c%.12S:%s%+06.3f";
  236. const char menu_fmt_float13off[] PROGMEM = "%c%.12S:%s%";
  237. template<typename T>
  238. static void menu_draw_P(char chr, const char* str, int16_t val);
  239. template<>
  240. void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
  241. {
  242. int text_len = strlen_P(str);
  243. if (text_len > 15) text_len = 15;
  244. char spaces[21];
  245. strcpy_P(spaces, menu_20x_space);
  246. spaces[15 - text_len] = 0;
  247. lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
  248. }
  249. template<>
  250. void menu_draw_P<uint8_t*>(char chr, const char* str, int16_t val)
  251. {
  252. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  253. int text_len = strlen_P(str);
  254. if (text_len > 15) text_len = 15;
  255. char spaces[21];
  256. strcpy_P(spaces, menu_20x_space);
  257. spaces[12 - text_len] = 0;
  258. float factor = 1.0 + static_cast<float>(val) / 1000.0;
  259. if (val <= _md->minEditValue)
  260. {
  261. lcd_printf_P(menu_fmt_float13off, chr, str, spaces);
  262. lcd_puts_P(_i(" [off]"));
  263. }
  264. else
  265. {
  266. lcd_printf_P(menu_fmt_float13, chr, str, spaces, factor);
  267. }
  268. }
  269. //draw up to 12 chars of text, ':' and float number in format +123.0
  270. void menu_draw_float31(char chr, const char* str, float val)
  271. {
  272. int text_len = strlen_P(str);
  273. if (text_len > 12) text_len = 12;
  274. char spaces[21];
  275. strcpy_P(spaces, menu_20x_space);
  276. spaces[12 - text_len] = 0;
  277. lcd_printf_P(menu_fmt_float31, chr, str, spaces, val);
  278. }
  279. //draw up to 12 chars of text, ':' and float number in format +1.234
  280. void menu_draw_float13(char chr, const char* str, float val)
  281. {
  282. int text_len = strlen_P(str);
  283. if (text_len > 12) text_len = 12;
  284. char spaces[21];
  285. strcpy_P(spaces, menu_20x_space);
  286. spaces[12 - text_len] = 0;
  287. lcd_printf_P(menu_fmt_float13, chr, str, spaces, val);
  288. }
  289. template <typename T>
  290. static void _menu_edit_P(void)
  291. {
  292. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  293. if (lcd_draw_update)
  294. {
  295. if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
  296. if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
  297. lcd_set_cursor(0, 1);
  298. menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
  299. }
  300. if (LCD_CLICKED)
  301. {
  302. *((T)(_md->editValue)) = lcd_encoder;
  303. menu_back_no_reset();
  304. }
  305. }
  306. template <typename T>
  307. uint8_t menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val)
  308. {
  309. menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
  310. if (menu_item == menu_line)
  311. {
  312. if (lcd_draw_update)
  313. {
  314. lcd_set_cursor(0, menu_row);
  315. menu_draw_P<T>((lcd_encoder == menu_item)?'>':' ', str, *pval);
  316. }
  317. if (menu_clicked && (lcd_encoder == menu_item))
  318. {
  319. menu_submenu_no_reset(_menu_edit_P<T>);
  320. _md->editLabel = str;
  321. _md->editValue = pval;
  322. _md->minEditValue = min_val;
  323. _md->maxEditValue = max_val;
  324. lcd_encoder = *pval;
  325. return menu_item_ret();
  326. }
  327. }
  328. menu_item++;
  329. return 0;
  330. }
  331. template uint8_t menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val);
  332. template uint8_t menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val);
  333. #undef _menu_data