menu.cpp 7.2 KB

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