menu.cpp 7.2 KB

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