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 3.2KB

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