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

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