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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "menu.h"
  23. static char prev_buff[MENU_MAX_LEN] = {0};
  24. static char buff[MENU_MAX_LEN] = {0};
  25. static struct menu_state menu = { .off = 0, .selection = -1, .length = 0, .buff = buff };
  26. static void (*enter_callback)(int) = NULL;
  27. static void menu_buttons(enum buttons btn, bool state) {
  28. if (state && (btn == BTN_LEFT)) {
  29. // TODO brightness down
  30. return;
  31. } else if (state && (btn == BTN_RIGHT)) {
  32. // TODO brightness up
  33. return;
  34. } else 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_UP) && (btn != BTN_DOWN))) {
  40. return;
  41. }
  42. if (state && (btn == BTN_UP)) {
  43. if (menu.selection < 0) {
  44. menu.selection = menu.length - 1;
  45. } else {
  46. menu.selection -= 1;
  47. }
  48. } else if (state && (btn == BTN_DOWN)) {
  49. if (menu.selection < 0) {
  50. menu.selection = 0;
  51. } else {
  52. menu.selection += 1;
  53. }
  54. }
  55. if (menu.selection < 0) {
  56. menu.selection += menu.length;
  57. }
  58. if (menu.selection >= menu.length) {
  59. menu.selection -= menu.length;
  60. }
  61. if (menu.selection >= 0) {
  62. while (menu.selection < menu.off) {
  63. menu.off -= 1;
  64. }
  65. while (menu.selection >= (menu.off + MENU_MAX_LINES)) {
  66. menu.off += 1;
  67. }
  68. }
  69. }
  70. void menu_init(void (*cb)(int)) {
  71. menu.off = 0;
  72. menu.selection = -1;
  73. menu.length = 0;
  74. enter_callback = cb;
  75. buttons_callback(menu_buttons);
  76. }
  77. void menu_deinit(void) {
  78. buttons_callback(NULL);
  79. }
  80. void menu_run(void (*draw)(struct menu_state *)) {
  81. if (draw) {
  82. draw(&menu);
  83. }
  84. if (strncmp(buff, prev_buff, MENU_MAX_LEN) != 0) {
  85. strncpy(prev_buff, buff, MENU_MAX_LEN);
  86. text_box(buff);
  87. }
  88. }