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

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