S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

menu.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * menu.c
  3. *
  4. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <string.h>
  19. #include "config.h"
  20. #include "buttons.h"
  21. #include "text.h"
  22. #include "lcd.h"
  23. #include "mem.h"
  24. #include "menu.h"
  25. static char prev_buff[MENU_MAX_LEN] = {0};
  26. static struct menu_state menu = { .off = 0, .selection = -1, .length = 0, .buff = {0} };
  27. static void (*enter_callback)(int) = NULL;
  28. static void (*up_callback)(int) = NULL;
  29. static void (*down_callback)(int) = NULL;
  30. static void (*exit_callback)(void) = NULL;
  31. #ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  32. bool menu_got_input = false;
  33. #endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  34. static void menu_buttons(enum buttons btn, bool state) {
  35. #ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  36. menu_got_input = true;
  37. #endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  38. if (state && (btn == BTN_LEFT)) {
  39. uint16_t backlight_value = lcd_get_backlight();
  40. if (backlight_value > 0x00FF) {
  41. backlight_value = backlight_value >> 1;
  42. }
  43. lcd_set_backlight(backlight_value);
  44. mem_data()->backlight = backlight_value;
  45. return;
  46. } else if (state && (btn == BTN_RIGHT)) {
  47. uint16_t backlight_value = lcd_get_backlight();
  48. if (backlight_value < 0xFF00) {
  49. backlight_value = backlight_value << 1;
  50. }
  51. lcd_set_backlight(backlight_value);
  52. mem_data()->backlight = backlight_value;
  53. return;
  54. } else if (state && ((btn == BTN_ENTER) || (btn == BTN_A))) {
  55. if (enter_callback) {
  56. enter_callback(menu.selection);
  57. }
  58. return;
  59. } else if (state && (btn == BTN_B)) {
  60. if (up_callback) {
  61. up_callback(menu.selection);
  62. }
  63. return;
  64. } else if (state && (btn == BTN_X)) {
  65. if (down_callback) {
  66. down_callback(menu.selection);
  67. }
  68. return;
  69. } else if (state && (btn == BTN_Y)) {
  70. if (exit_callback) {
  71. exit_callback();
  72. }
  73. } else if ((!state) || ((btn != BTN_UP) && (btn != BTN_DOWN))) {
  74. return;
  75. }
  76. if (state && (btn == BTN_UP)) {
  77. if (menu.selection < 0) {
  78. menu.selection = menu.length - 1;
  79. } else {
  80. menu.selection -= 1;
  81. }
  82. } else if (state && (btn == BTN_DOWN)) {
  83. if (menu.selection < 0) {
  84. menu.selection = 0;
  85. } else {
  86. menu.selection += 1;
  87. }
  88. }
  89. if (menu.selection < 0) {
  90. menu.selection += menu.length;
  91. }
  92. if (menu.selection >= menu.length) {
  93. menu.selection -= menu.length;
  94. }
  95. if (menu.selection >= 0) {
  96. while (menu.selection < menu.off) {
  97. menu.off -= 1;
  98. }
  99. while (menu.selection >= (menu.off + MENU_MAX_LINES)) {
  100. menu.off += 1;
  101. }
  102. }
  103. }
  104. void menu_init(void (*enter)(int),
  105. void (*up)(int),
  106. void (*down)(int),
  107. void (*exit)(void)) {
  108. menu.off = 0;
  109. menu.selection = -1;
  110. menu.length = 0;
  111. enter_callback = enter;
  112. up_callback = up;
  113. down_callback = down;
  114. exit_callback = exit;
  115. buttons_callback(menu_buttons);
  116. }
  117. void menu_deinit(void) {
  118. buttons_callback(NULL);
  119. mem_write();
  120. }
  121. void menu_run(void (*draw)(struct menu_state *), bool centered) {
  122. if (draw) {
  123. draw(&menu);
  124. }
  125. if (strncmp(menu.buff, prev_buff, MENU_MAX_LEN) != 0) {
  126. strncpy(prev_buff, menu.buff, MENU_MAX_LEN);
  127. text_box(menu.buff, centered,
  128. "fixed_10x20",
  129. 0, LCD_WIDTH,
  130. 50, (MENU_MAX_LINES * 20) + ((MENU_MAX_LINES - 1) * 2),
  131. 0);
  132. }
  133. }