menu.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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) menu_goto(menu_stack[--menu_depth].menu, menu_stack[menu_depth].position, true, true);
  63. }
  64. void menu_back_if_clicked(void)
  65. {
  66. if (lcd_clicked())
  67. menu_back();
  68. }
  69. void menu_back_if_clicked_fb(void)
  70. {
  71. if (lcd_clicked())
  72. {
  73. lcd_quick_feedback();
  74. menu_back();
  75. }
  76. }
  77. void menu_submenu(menu_func_t submenu)
  78. {
  79. if (menu_depth <= MENU_DEPTH_MAX)
  80. {
  81. menu_stack[menu_depth].menu = menu_menu;
  82. menu_stack[menu_depth++].position = lcd_encoder;
  83. menu_goto(submenu, 0, true, true);
  84. }
  85. }
  86. uint8_t menu_item_ret(void)
  87. {
  88. lcd_beeper_quick_feedback();
  89. lcd_draw_update = 2;
  90. lcd_button_pressed = false;
  91. return 1;
  92. }
  93. /*
  94. int menu_item_printf_P(char type_char, const char* format, ...)
  95. {
  96. va_list args;
  97. va_start(args, format);
  98. int ret = 0;
  99. lcd_set_cursor(0, menu_row);
  100. if (lcd_encoder == menu_item)
  101. lcd_print('>');
  102. else
  103. lcd_print(' ');
  104. int cnt = vfprintf_P(lcdout, format, args);
  105. for (int i = cnt; i < 18; i++)
  106. lcd_print(' ');
  107. lcd_print(type_char);
  108. va_end(args);
  109. return ret;
  110. }
  111. */
  112. int menu_draw_item_puts_P(char type_char, const char* str)
  113. {
  114. lcd_set_cursor(0, menu_row);
  115. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  116. return cnt;
  117. }
  118. /*
  119. int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
  120. {
  121. lcd_set_cursor(0, menu_row);
  122. int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
  123. return cnt;
  124. }
  125. */
  126. void menu_item_dummy(void)
  127. {
  128. menu_item++;
  129. }
  130. uint8_t menu_item_text_P(const char* str)
  131. {
  132. if (menu_item == menu_line)
  133. {
  134. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  135. if (menu_clicked && (lcd_encoder == menu_item))
  136. return menu_item_ret();
  137. }
  138. menu_item++;
  139. return 0;
  140. }
  141. uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
  142. {
  143. if (menu_item == menu_line)
  144. {
  145. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
  146. if (menu_clicked && (lcd_encoder == menu_item))
  147. {
  148. menu_submenu(submenu);
  149. return menu_item_ret();
  150. }
  151. }
  152. menu_item++;
  153. return 0;
  154. }
  155. uint8_t menu_item_back_P(const char* str)
  156. {
  157. if (menu_item == menu_line)
  158. {
  159. if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
  160. if (menu_clicked && (lcd_encoder == menu_item))
  161. {
  162. menu_back();
  163. return menu_item_ret();
  164. }
  165. }
  166. menu_item++;
  167. return 0;
  168. }
  169. uint8_t menu_item_function_P(const char* str, menu_func_t func)
  170. {
  171. if (menu_item == menu_line)
  172. {
  173. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  174. if (menu_clicked && (lcd_encoder == menu_item))
  175. {
  176. menu_clicked = false;
  177. lcd_update_enabled = 0;
  178. if (func) func();
  179. lcd_update_enabled = 1;
  180. return menu_item_ret();
  181. }
  182. }
  183. menu_item++;
  184. return 0;
  185. }
  186. uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
  187. {
  188. if (menu_item == menu_line)
  189. {
  190. if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
  191. if (menu_clicked && (lcd_encoder == menu_item))
  192. {
  193. if (str_gcode) enquecommand_P(str_gcode);
  194. return menu_item_ret();
  195. }
  196. }
  197. menu_item++;
  198. return 0;
  199. }
  200. const char menu_fmt_int3[] PROGMEM = "%c%S:\x1b[%hhu;16H%3d";
  201. #define _menu_data (*((menu_data_edit_t*)menu_data))
  202. void _menu_edit_int3(void)
  203. {
  204. if (lcd_draw_update)
  205. {
  206. if (lcd_encoder < _menu_data.minEditValue) lcd_encoder = _menu_data.minEditValue;
  207. if (lcd_encoder > _menu_data.maxEditValue) lcd_encoder = _menu_data.maxEditValue;
  208. lcd_set_cursor(0, 1);
  209. lcd_printf_P(menu_fmt_int3, ' ', _menu_data.editLabel, (uint8_t)1, (int)lcd_encoder);
  210. }
  211. if (LCD_CLICKED)
  212. {
  213. *((int*)(_menu_data.editValue)) = (int)lcd_encoder;
  214. menu_back();
  215. }
  216. }
  217. uint8_t menu_item_edit_int3(const char* str, int16_t* pval, int16_t min_val, int16_t max_val)
  218. {
  219. if (menu_item == menu_line)
  220. {
  221. if (lcd_draw_update)
  222. {
  223. lcd_set_cursor(0, menu_row);
  224. lcd_printf_P(menu_fmt_int3, (lcd_encoder == menu_item)?'>':' ', str, menu_row, *pval);
  225. }
  226. if (menu_clicked && (lcd_encoder == menu_item))
  227. {
  228. menu_submenu(_menu_edit_int3);
  229. _menu_data.editLabel = str;
  230. _menu_data.editValue = pval;
  231. _menu_data.minEditValue = min_val;
  232. _menu_data.maxEditValue = max_val;
  233. lcd_encoder = *pval;
  234. return menu_item_ret();
  235. }
  236. }
  237. menu_item++;
  238. return 0;
  239. }
  240. #undef _menu_data