menu.cpp 6.8 KB

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