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_edit_workflow.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * state_edit_workflow.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. // TODO adding and removing whole steps
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "config.h"
  22. #include "mem.h"
  23. #include "menu.h"
  24. #include "workflow.h"
  25. #include "state.h"
  26. #include "state_value.h"
  27. #include "state_edit_workflow.h"
  28. static uint16_t wf_index = 0;
  29. static void enter_cb(int selection) {
  30. static char buff[20];
  31. if ((selection >= 0) && (selection < wf_steps(wf_index))) {
  32. struct wf_step *step = wf_get_step(wf_index, selection);
  33. switch (step->op) {
  34. case OP_SET_TEMPERATURE:
  35. case OP_WAIT_TEMPERATURE:
  36. snprintf(buff, sizeof(buff),
  37. "%s Temp.",
  38. step->op == OP_WAIT_TEMPERATURE ? "Wait" : "Set");
  39. state_value_set(&step->val,
  40. sizeof(step->val),
  41. 400, 2300, VAL_STEP_INCREMENT, 10,
  42. buff);
  43. break;
  44. case OP_WAIT_TIME:
  45. case OP_PUMP_TIME:
  46. snprintf(buff, sizeof(buff),
  47. "%s Time",
  48. step->op == OP_WAIT_TIME ? "Wait" : "Pump");
  49. state_value_set(&step->val,
  50. sizeof(step->val),
  51. 0, 60000, VAL_STEP_INCREMENT, 1000,
  52. buff);
  53. break;
  54. }
  55. state_value_return(STATE_EDIT_WORKFLOW);
  56. state_switch(STATE_VALUE);
  57. }
  58. }
  59. static void lower_cb(int selection) {
  60. if ((selection > 0) && (selection < wf_steps(wf_index))) {
  61. wf_move_step_down(wf_index, selection);
  62. }
  63. }
  64. static void upper_cb(int selection) {
  65. if ((selection >= 0) && (selection < (wf_steps(wf_index) - 1))) {
  66. wf_move_step_up(wf_index, selection);
  67. }
  68. }
  69. static void exit_cb(void) {
  70. state_switch(STATE_WORKFLOW);
  71. }
  72. void state_edit_wf_index(uint16_t index) {
  73. wf_index = index;
  74. }
  75. void state_edit_wf_enter(void) {
  76. menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
  77. }
  78. void state_edit_wf_exit(void) {
  79. menu_deinit();
  80. }
  81. static void draw(struct menu_state *menu) {
  82. menu->length = wf_steps(wf_index);
  83. int pos = 0;
  84. for (uint16_t i = 0; i < menu->length; i++) {
  85. if ((i < menu->off)
  86. || ((i - menu->off) >= MENU_MAX_LINES)) {
  87. continue;
  88. }
  89. if (i == menu->selection) {
  90. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
  91. } else {
  92. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
  93. }
  94. struct wf_step *step = wf_get_step(wf_index, i);
  95. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  96. "% 2d: %s\n", i, wf_step_str(step));
  97. }
  98. if (menu->selection < 0) {
  99. menu->selection = 0;
  100. }
  101. }
  102. void state_edit_wf_run(void) {
  103. menu_run(draw, false);
  104. }