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_workflow.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * state_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 workflows
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "pico/stdlib.h"
  22. #include "config.h"
  23. #include "mem.h"
  24. #include "menu.h"
  25. #include "workflow.h"
  26. #include "state.h"
  27. #include "state_volcano_run.h"
  28. #include "state_edit_workflow.h"
  29. #include "state_workflow.h"
  30. static bool edit_mode = false;
  31. static uint32_t auto_connect_time = 0;
  32. static void exit_cb(void) {
  33. if (edit_mode) {
  34. state_switch(STATE_SETTINGS);
  35. } else {
  36. state_switch(STATE_SCAN);
  37. }
  38. }
  39. static void enter_cb(int selection) {
  40. if ((selection >= 0) && (selection < wf_count())) {
  41. if (edit_mode) {
  42. state_edit_wf_index(selection);
  43. state_switch(STATE_EDIT_WORKFLOW);
  44. } else {
  45. state_volcano_run_index(selection);
  46. state_switch(STATE_VOLCANO_RUN);
  47. }
  48. } else {
  49. exit_cb();
  50. }
  51. }
  52. static void lower_cb(int selection) {
  53. if (!edit_mode) {
  54. return;
  55. }
  56. if ((selection > 0) && (selection < wf_count())) {
  57. wf_move_down(selection);
  58. }
  59. }
  60. static void upper_cb(int selection) {
  61. if (!edit_mode) {
  62. return;
  63. }
  64. if ((selection >= 0) && (selection < (wf_count() - 1))) {
  65. wf_move_up(selection);
  66. }
  67. }
  68. void state_wf_edit(bool edit) {
  69. edit_mode = edit;
  70. }
  71. void state_wf_enter(void) {
  72. if (edit_mode) {
  73. menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
  74. } else {
  75. menu_init(enter_cb, NULL, NULL, exit_cb);
  76. }
  77. if (mem_data()->wf_auto_connect) {
  78. auto_connect_time = to_ms_since_boot(get_absolute_time());
  79. }
  80. }
  81. void state_wf_exit(void) {
  82. menu_deinit();
  83. }
  84. static void draw(struct menu_state *menu) {
  85. menu->length = wf_count();
  86. int pos = 0;
  87. for (uint16_t i = 0; i < menu->length; i++) {
  88. if ((i < menu->off)
  89. || ((i - menu->off) >= MENU_MAX_LINES)) {
  90. continue;
  91. }
  92. if (i == menu->selection) {
  93. if ((auto_connect_time != 0) && (!menu_got_input) && (!edit_mode)) {
  94. uint32_t now = to_ms_since_boot(get_absolute_time());
  95. uint32_t diff = now - auto_connect_time;
  96. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  97. "%ld ",
  98. (VOLCANO_AUTO_CONNECT_TIMEOUT_MS / 1000) - (diff / 1000));
  99. } else {
  100. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
  101. }
  102. } else {
  103. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
  104. }
  105. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  106. "'%s' by %s\n", wf_name(i), wf_author(i));
  107. }
  108. ADD_STATIC_ELEMENT("... go back");
  109. if (menu->selection < 0) {
  110. menu->selection = 0;
  111. }
  112. }
  113. void state_wf_run(void) {
  114. menu_run(draw, false);
  115. if ((auto_connect_time != 0) && (!menu_got_input) && (!edit_mode)) {
  116. uint32_t now = to_ms_since_boot(get_absolute_time());
  117. if ((now - auto_connect_time) >= VOLCANO_AUTO_CONNECT_TIMEOUT_MS) {
  118. state_volcano_run_index(0);
  119. state_switch(STATE_VOLCANO_RUN);
  120. auto_connect_time = 0;
  121. menu_got_input = true;
  122. }
  123. }
  124. }