menu.cpp 5.4 KB

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