S&B Volcano vaporizer remote control with Pi Pico W
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "textbox.h"
  23. #include "lcd.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. bool menu_got_input = false;
  32. static void menu_buttons(enum buttons btn, bool state) {
  33. menu_got_input = true;
  34. if (state && ((btn == BTN_ENTER) || (btn == BTN_A))) {
  35. if (enter_callback) {
  36. enter_callback(menu.selection);
  37. }
  38. return;
  39. } else if (state && (btn == BTN_B)) {
  40. if (up_callback) {
  41. up_callback(menu.selection);
  42. }
  43. return;
  44. } else if (state && (btn == BTN_X)) {
  45. if (down_callback) {
  46. down_callback(menu.selection);
  47. }
  48. return;
  49. } else if (state && (btn == BTN_Y)) {
  50. if (exit_callback) {
  51. exit_callback();
  52. }
  53. } else if ((!state) || ((btn != BTN_UP) && (btn != BTN_DOWN))) {
  54. return;
  55. }
  56. if (state && (btn == BTN_UP)) {
  57. if (menu.selection < 0) {
  58. menu.selection = menu.length - 1;
  59. } else {
  60. menu.selection -= 1;
  61. }
  62. } else if (state && (btn == BTN_DOWN)) {
  63. if (menu.selection < 0) {
  64. menu.selection = 0;
  65. } else {
  66. menu.selection += 1;
  67. }
  68. }
  69. if (menu.selection < 0) {
  70. menu.selection += menu.length;
  71. }
  72. if (menu.selection >= menu.length) {
  73. menu.selection -= menu.length;
  74. }
  75. if (menu.selection >= 0) {
  76. while (menu.selection < menu.off) {
  77. menu.off -= 1;
  78. }
  79. while (menu.selection >= (menu.off + menu.lines)) {
  80. menu.off += 1;
  81. }
  82. }
  83. }
  84. void menu_init(void (*enter)(int),
  85. void (*up)(int),
  86. void (*down)(int),
  87. void (*exit)(void)) {
  88. menu.off = 0;
  89. menu.selection = -1;
  90. menu.length = 0;
  91. menu.lines = MENU_MAX_LINES;
  92. menu.y_off = 0;
  93. enter_callback = enter;
  94. up_callback = up;
  95. down_callback = down;
  96. exit_callback = exit;
  97. buttons_callback(menu_buttons);
  98. }
  99. void menu_deinit(void) {
  100. buttons_callback(NULL);
  101. }
  102. void menu_run(void (*draw)(struct menu_state *), bool centered) {
  103. if (draw) {
  104. draw(&menu);
  105. }
  106. if (strncmp(menu.buff, prev_buff, MENU_MAX_LEN) != 0) {
  107. strncpy(prev_buff, menu.buff, MENU_MAX_LEN);
  108. text_box(menu.buff, centered,
  109. "fixed_10x20",
  110. 0, LCD_WIDTH,
  111. 50 + menu.y_off, MENU_BOX_HEIGHT(menu.lines, 20, 2),
  112. 0);
  113. }
  114. }