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.

workflow.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * 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. #include "config.h"
  19. #include "log.h"
  20. #include "volcano.h"
  21. #include "workflow.h"
  22. #define WF_MAX_STEPS 42
  23. #define WF_MAX_FLOWS 6
  24. struct workflow {
  25. const char *name;
  26. const char *author;
  27. struct wf_step steps[WF_MAX_STEPS];
  28. uint16_t count;
  29. };
  30. #define NOTIFY \
  31. { .op = OP_WAIT_TIME, .val = 1000 }, \
  32. { .op = OP_PUMP_TIME, .val = 1000 }
  33. static const struct workflow wf[WF_MAX_FLOWS] = {
  34. {
  35. .name = "Default",
  36. .author = "xythobuz",
  37. .steps = {
  38. { .op = OP_WAIT_TEMPERATURE, .val = 1850 },
  39. { .op = OP_WAIT_TIME, .val = 10000 },
  40. { .op = OP_PUMP_TIME, .val = 5000 },
  41. { .op = OP_WAIT_TEMPERATURE, .val = 1950 },
  42. { .op = OP_WAIT_TIME, .val = 5000 },
  43. { .op = OP_PUMP_TIME, .val = 20000 },
  44. { .op = OP_WAIT_TEMPERATURE, .val = 2050 },
  45. { .op = OP_WAIT_TIME, .val = 5000 },
  46. { .op = OP_PUMP_TIME, .val = 20000 },
  47. NOTIFY, NOTIFY, NOTIFY, NOTIFY,
  48. { .op = OP_SET_TEMPERATURE, .val = 1900 },
  49. },
  50. .count = 18,
  51. }, {
  52. .name = "Vorbi",
  53. .author = "Rinor",
  54. .steps = {
  55. { .op = OP_WAIT_TEMPERATURE, .val = 1760 },
  56. { .op = OP_WAIT_TIME, .val = 10000 },
  57. { .op = OP_PUMP_TIME, .val = 6000 },
  58. { .op = OP_WAIT_TEMPERATURE, .val = 1870 },
  59. { .op = OP_WAIT_TIME, .val = 5000 },
  60. { .op = OP_PUMP_TIME, .val = 10000 },
  61. { .op = OP_WAIT_TEMPERATURE, .val = 2040 },
  62. { .op = OP_WAIT_TIME, .val = 3000 },
  63. { .op = OP_PUMP_TIME, .val = 10000 },
  64. { .op = OP_WAIT_TEMPERATURE, .val = 2170 },
  65. { .op = OP_WAIT_TIME, .val = 5000 },
  66. { .op = OP_PUMP_TIME, .val = 10000 },
  67. },
  68. .count = 12,
  69. }, {
  70. .name = "Relaxo",
  71. .author = "xythobuz",
  72. .steps = {
  73. { .op = OP_WAIT_TEMPERATURE, .val = 1750 },
  74. { .op = OP_WAIT_TIME, .val = 10000 },
  75. { .op = OP_PUMP_TIME, .val = 5000 },
  76. { .op = OP_WAIT_TEMPERATURE, .val = 1850 },
  77. { .op = OP_WAIT_TIME, .val = 5000 },
  78. { .op = OP_PUMP_TIME, .val = 20000 },
  79. { .op = OP_WAIT_TEMPERATURE, .val = 1950 },
  80. { .op = OP_WAIT_TIME, .val = 5000 },
  81. { .op = OP_PUMP_TIME, .val = 20000 },
  82. NOTIFY, NOTIFY, NOTIFY, NOTIFY,
  83. { .op = OP_SET_TEMPERATURE, .val = 1900 },
  84. },
  85. .count = 18,
  86. }, {
  87. .name = "Hotty",
  88. .author = "xythobuz",
  89. .steps = {
  90. { .op = OP_WAIT_TEMPERATURE, .val = 1900 },
  91. { .op = OP_WAIT_TIME, .val = 10000 },
  92. { .op = OP_PUMP_TIME, .val = 5000 },
  93. { .op = OP_WAIT_TEMPERATURE, .val = 2050 },
  94. { .op = OP_WAIT_TIME, .val = 5000 },
  95. { .op = OP_PUMP_TIME, .val = 20000 },
  96. { .op = OP_WAIT_TEMPERATURE, .val = 2200 },
  97. { .op = OP_WAIT_TIME, .val = 5000 },
  98. { .op = OP_PUMP_TIME, .val = 20000 },
  99. NOTIFY, NOTIFY, NOTIFY, NOTIFY,
  100. { .op = OP_SET_TEMPERATURE, .val = 1900 },
  101. },
  102. .count = 18,
  103. },
  104. };
  105. static const uint16_t count = 4;
  106. static enum wf_status status = WF_IDLE;
  107. static uint16_t wf_i = 0;
  108. static uint16_t step = 0;
  109. static uint32_t start_t = 0;
  110. static uint16_t start_val = 0;
  111. static uint16_t curr_val = 0;
  112. static void do_step(void) {
  113. switch (wf[wf_i].steps[step].op) {
  114. case OP_SET_TEMPERATURE:
  115. case OP_WAIT_TEMPERATURE:
  116. debug("workflow temp %.1f C", wf[wf_i].steps[step].val / 10.0);
  117. start_val = volcano_get_current_temp();
  118. volcano_set_target_temp(wf[wf_i].steps[step].val);
  119. break;
  120. case OP_PUMP_TIME:
  121. volcano_set_pump_state(true);
  122. start_t = to_ms_since_boot(get_absolute_time());
  123. start_val = 0;
  124. debug("workflow pump %.3f s", wf[wf_i].steps[step].val / 1000.0);
  125. break;
  126. case OP_WAIT_TIME:
  127. start_t = to_ms_since_boot(get_absolute_time());
  128. start_val = 0;
  129. debug("workflow time %.3f s", wf[wf_i].steps[step].val / 1000.0);
  130. break;
  131. }
  132. curr_val = start_val;
  133. }
  134. uint16_t wf_count(void) {
  135. return count;
  136. }
  137. const char *wf_name(uint16_t index) {
  138. if (index >= count) {
  139. debug("invalid index %d", index);
  140. return NULL;
  141. }
  142. return wf[index].name;
  143. }
  144. const char *wf_author(uint16_t index) {
  145. if (index >= count) {
  146. debug("invalid index %d", index);
  147. return NULL;
  148. }
  149. return wf[index].author;
  150. }
  151. struct wf_state wf_status(void) {
  152. struct wf_state s = {
  153. .status = status,
  154. .index = step,
  155. .count = wf[wf_i].count,
  156. .step = wf[wf_i].steps[step],
  157. .start_val = start_val,
  158. .curr_val = curr_val,
  159. };
  160. return s;
  161. }
  162. void wf_start(uint16_t index) {
  163. if (status != WF_IDLE) {
  164. debug("workflow already running");
  165. return;
  166. }
  167. if (index >= count) {
  168. debug("invalid index %d", index);
  169. return;
  170. }
  171. status = WF_RUNNING;
  172. wf_i = index;
  173. step = 0;
  174. // discover characteristics
  175. volcano_set_pump_state(false);
  176. volcano_set_heater_state(false);
  177. volcano_set_target_temp(1850);
  178. volcano_set_heater_state(true);
  179. do_step();
  180. }
  181. void wf_reset(void) {
  182. status = WF_IDLE;
  183. }
  184. void wf_run(void) {
  185. if (status == WF_IDLE) {
  186. return;
  187. }
  188. bool done = false;
  189. switch (wf[wf_i].steps[step].op) {
  190. case OP_SET_TEMPERATURE:
  191. done = true;
  192. break;
  193. case OP_WAIT_TEMPERATURE: {
  194. uint16_t temp = volcano_get_current_temp();
  195. curr_val = temp;
  196. done = (temp >= (wf[wf_i].steps[step].val - 5));
  197. break;
  198. }
  199. case OP_PUMP_TIME:
  200. case OP_WAIT_TIME: {
  201. uint32_t now = to_ms_since_boot(get_absolute_time());
  202. uint32_t diff = now - start_t;
  203. curr_val = diff;
  204. done = (diff >= wf[wf_i].steps[step].val);
  205. break;
  206. }
  207. }
  208. if (done) {
  209. if (wf[wf_i].steps[step].op == OP_PUMP_TIME) {
  210. volcano_set_pump_state(false);
  211. }
  212. step++;
  213. if (step >= wf[wf_i].count) {
  214. status = WF_IDLE;
  215. volcano_set_heater_state(false);
  216. debug("workflow finished");
  217. } else {
  218. do_step();
  219. }
  220. }
  221. }