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.

state_value.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * state_value.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 <stdio.h>
  19. #include "config.h"
  20. #include "buttons.h"
  21. #include "log.h"
  22. #include "lcd.h"
  23. #include "menu.h"
  24. #include "text.h"
  25. #include "textbox.h"
  26. #include "state_value.h"
  27. static void *val_p = NULL;
  28. static size_t val_len = 0;
  29. static ssize_t val_min = 0;
  30. static ssize_t val_max = 0;
  31. static ssize_t val_step = 0;
  32. static const char *val_name = NULL;
  33. static enum value_step_mode val_mode = VAL_STEP_INCREMENT;
  34. static enum system_state val_ret_state = STATE_SCAN;
  35. static ssize_t val = 0;
  36. void state_value_set(void *value, size_t length,
  37. ssize_t min, ssize_t max,
  38. enum value_step_mode mode, ssize_t step,
  39. const char *name) {
  40. val_p = value;
  41. val_len = length;
  42. val_min = min;
  43. val_max = max;
  44. val_mode = mode;
  45. val_step = step;
  46. val_name = name;
  47. }
  48. void state_value_return(enum system_state state) {
  49. val_ret_state = state;
  50. }
  51. static void draw(void) {
  52. static char buff[100];
  53. if ((val_p == NULL) || (val_len <= 0) || (val_name == NULL)) {
  54. snprintf(buff, sizeof(buff),
  55. "error");
  56. } else {
  57. if (val_mode == VAL_STEP_INCREMENT) {
  58. snprintf(buff, sizeof(buff),
  59. "%s:\n\n%d -> %d -> %d",
  60. val_name,
  61. val_min / val_step,
  62. val / val_step,
  63. val_max / val_step);
  64. } else {
  65. snprintf(buff, sizeof(buff),
  66. "%s:\n\n%d -> %d -> %d",
  67. val_name,
  68. __builtin_ffs(val_min),
  69. __builtin_ffs(val),
  70. __builtin_ffs(val_max));
  71. }
  72. }
  73. text_box(buff, true,
  74. "fixed_10x20",
  75. 0, LCD_WIDTH,
  76. 50, MENU_BOX_HEIGHT(MENU_MAX_LINES, 20, 2),
  77. 0);
  78. }
  79. static void write(void) {
  80. if ((val_p == NULL) || (val_len <= 0)) {
  81. debug("invalid params");
  82. return;
  83. }
  84. switch (val_len) {
  85. case 1:
  86. *((uint8_t *)val_p) = val;
  87. break;
  88. case 2:
  89. *((uint16_t *)val_p) = val;
  90. break;
  91. case 4:
  92. *((uint32_t *)val_p) = val;
  93. break;
  94. default:
  95. debug("invalid len %d", val_len);
  96. return;
  97. }
  98. }
  99. static void step(ssize_t v) {
  100. if ((val_p == NULL) || (val_len <= 0)) {
  101. debug("invalid params");
  102. return;
  103. }
  104. if (((v > 0) && (val >= val_max))
  105. || ((v < 0) && (val <= val_min))) {
  106. return;
  107. }
  108. switch (val_mode) {
  109. case VAL_STEP_INCREMENT:
  110. val += v * val_step;
  111. break;
  112. case VAL_STEP_SHIFT:
  113. if ((val_step * v) > 0) {
  114. val <<= val_step;
  115. } else {
  116. val >>= val_step;
  117. }
  118. break;
  119. }
  120. // apply new value while editing
  121. write();
  122. }
  123. static void value_buttons(enum buttons btn, bool state) {
  124. if (state && (btn == BTN_Y)) {
  125. state_switch(val_ret_state);
  126. } else if (state && (btn == BTN_LEFT)) {
  127. step(-1);
  128. draw();
  129. } else if (state && (btn == BTN_RIGHT)) {
  130. step(1);
  131. draw();
  132. }
  133. }
  134. void state_value_enter(void) {
  135. buttons_callback(value_buttons);
  136. switch (val_len) {
  137. case 1:
  138. val = *((uint8_t *)val_p);
  139. break;
  140. case 2:
  141. val = *((uint16_t *)val_p);
  142. break;
  143. case 4:
  144. val = *((uint32_t *)val_p);
  145. break;
  146. default:
  147. debug("invalid len %d", val_len);
  148. return;
  149. }
  150. draw();
  151. }
  152. void state_value_exit(void) {
  153. buttons_callback(NULL);
  154. write();
  155. }
  156. void state_value_run(void) { }