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_string.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * state_string.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 <string.h>
  20. #include "config.h"
  21. #include "buttons.h"
  22. #include "lcd.h"
  23. #include "menu.h"
  24. #include "text.h"
  25. #include "textbox.h"
  26. #include "state_string.h"
  27. static char *str_p = NULL;
  28. static size_t str_len = 0;
  29. static const char *str_name = NULL;
  30. static enum system_state str_ret_state = STATE_SCAN;
  31. static size_t edit = 0, offset = 0;
  32. void state_string_set(char *value, size_t length,
  33. const char *name) {
  34. str_p = value;
  35. str_len = length;
  36. str_name = name;
  37. }
  38. void state_string_return(enum system_state state) {
  39. str_ret_state = state;
  40. }
  41. static void draw(void) {
  42. static char buff[42];
  43. if ((str_p == NULL) || (str_len <= 0) || (str_name == NULL)) {
  44. snprintf(buff, sizeof(buff), "error");
  45. } else {
  46. snprintf(buff, sizeof(buff), "%s:\n\n'%s'", str_name, str_p + offset);
  47. }
  48. text_box(buff, false,
  49. "fixed_10x20",
  50. 0, LCD_WIDTH,
  51. 50, MENU_BOX_HEIGHT(MENU_MAX_LINES, 20, 2),
  52. 0);
  53. size_t ch = edit - offset + 1;
  54. lcd_write_rect(ch * 10,
  55. 50 + 3 * 22,
  56. ch * 10 + 10,
  57. 50 + 3 * 22 + 3,
  58. LCD_WHITE);
  59. }
  60. static void string_buttons(enum buttons btn, bool state) {
  61. if (state && (btn == BTN_Y)) {
  62. state_switch(str_ret_state);
  63. } else if (state && (btn == BTN_LEFT)) {
  64. if (edit > 0) {
  65. edit--;
  66. }
  67. } else if (state && (btn == BTN_RIGHT)) {
  68. if (edit < (str_len - 1)) {
  69. edit++;
  70. }
  71. size_t l = strlen(str_p);
  72. while (edit >= l) {
  73. str_p[l++] = ' ';
  74. }
  75. } else if (state && (btn == BTN_UP)) {
  76. char *c = str_p + edit;
  77. if ((*c >= ' ') && (*c < '~')) {
  78. (*c)++;
  79. }
  80. } else if (state && (btn == BTN_DOWN)) {
  81. char *c = str_p + edit;
  82. if ((*c > ' ') && (*c <= '~')) {
  83. (*c)--;
  84. }
  85. } else if (state && (btn == BTN_B)) {
  86. char *c = str_p + edit;
  87. *c = '\0';
  88. } else {
  89. return;
  90. }
  91. while (edit < offset) {
  92. offset -= 1;
  93. }
  94. while (edit >= (offset + (LCD_WIDTH / 10 - 3))) {
  95. offset += 1;
  96. }
  97. draw();
  98. }
  99. void state_string_enter(void) {
  100. buttons_callback(string_buttons);
  101. edit = 0;
  102. offset = 0;
  103. draw();
  104. }
  105. void state_string_exit(void) {
  106. buttons_callback(NULL);
  107. }
  108. void state_string_run(void) { }