menu.cpp 6.6 KB

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