menu.cpp 7.5 KB

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