123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
-
-
- #define WF_CONFIRM_WRITES
-
- #include <stdio.h>
-
- #include "config.h"
- #include "log.h"
- #include "mem.h"
- #include "volcano.h"
- #include "workflow.h"
-
- #ifdef WF_CONFIRM_WRITES
- #define DO_WHILE(x, y) \
- do { \
- x; \
- } while (y)
- #else
- #define DO_WHILE(x, y) x
- #endif
-
- static enum wf_status status = WF_IDLE;
- static uint16_t wf_i = 0;
- static uint16_t step = 0;
- static uint32_t start_t = 0;
- static uint16_t start_val = 0;
- static uint16_t curr_val = 0;
-
- static void do_step(void) {
- switch (mem_data()->wf[wf_i].steps[step].op) {
- case OP_SET_TEMPERATURE:
- case OP_WAIT_TEMPERATURE:
- debug("workflow temp %.1f C", mem_data()->wf[wf_i].steps[step].val / 10.0);
- start_val = volcano_get_current_temp();
- DO_WHILE(volcano_set_target_temp(mem_data()->wf[wf_i].steps[step].val),
- volcano_get_target_temp() != mem_data()->wf[wf_i].steps[step].val);
- break;
-
- case OP_PUMP_TIME:
- DO_WHILE(volcano_set_pump_state(true),
- !(volcano_get_state() & VOLCANO_STATE_PUMP));
- start_t = to_ms_since_boot(get_absolute_time());
- start_val = 0;
- debug("workflow pump %.3f s", mem_data()->wf[wf_i].steps[step].val / 1000.0);
- break;
-
- case OP_WAIT_TIME:
- start_t = to_ms_since_boot(get_absolute_time());
- start_val = 0;
- debug("workflow time %.3f s", mem_data()->wf[wf_i].steps[step].val / 1000.0);
- break;
- }
-
- curr_val = start_val;
- }
-
- uint16_t wf_count(void) {
- return mem_data()->wf_count;
- }
-
- void wf_move_down(uint16_t index) {
- if ((index < 1) || (index >= mem_data()->wf_count)) {
- debug("invalid index %d", index);
- return;
- }
-
- struct workflow tmp = mem_data()->wf[index - 1];
- mem_data()->wf[index - 1] = mem_data()->wf[index];
- mem_data()->wf[index] = tmp;
- }
-
- void wf_move_up(uint16_t index) {
- if (index >= (mem_data()->wf_count - 1)) {
- debug("invalid index %d", index);
- return;
- }
-
- struct workflow tmp = mem_data()->wf[index + 1];
- mem_data()->wf[index + 1] = mem_data()->wf[index];
- mem_data()->wf[index] = tmp;
- }
-
- uint16_t wf_steps(uint16_t index) {
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return 0;
- }
- return mem_data()->wf[index].count;
- }
-
- void wf_move_step_down(uint16_t index, uint16_t step_i) {
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return;
- }
- if ((step_i < 1) || (step_i >= mem_data()->wf[index].count)) {
- debug("invalid step %d", step_i);
- return;
- }
-
- struct wf_step tmp = mem_data()->wf[index].steps[step_i - 1];
- mem_data()->wf[index].steps[step_i - 1] = mem_data()->wf[index].steps[step_i];
- mem_data()->wf[index].steps[step_i] = tmp;
- }
-
- void wf_move_step_up(uint16_t index, uint16_t step_i) {
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return;
- }
- if (step_i >= (mem_data()->wf[index].count - 1)) {
- debug("invalid step %d", step_i);
- return;
- }
-
- struct wf_step tmp = mem_data()->wf[index].steps[step_i + 1];
- mem_data()->wf[index].steps[step_i + 1] = mem_data()->wf[index].steps[step_i];
- mem_data()->wf[index].steps[step_i] = tmp;
- }
-
- struct wf_step *wf_get_step(uint16_t index, uint16_t step_i) {
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return NULL;
- }
- return &mem_data()->wf[index].steps[step_i];
- }
-
- const char *wf_step_str(struct wf_step *step_p) {
- static char buff[20];
-
- switch (step_p->op) {
- case OP_SET_TEMPERATURE:
- snprintf(buff, sizeof(buff),
- "set temp %.1f C", step_p->val / 10.0f);
- break;
-
- case OP_WAIT_TEMPERATURE:
- snprintf(buff, sizeof(buff),
- "wait temp %.1f C", step_p->val / 10.0f);
- break;
-
- case OP_WAIT_TIME:
- case OP_PUMP_TIME:
- snprintf(buff, sizeof(buff),
- "%s time %.1f s",
- (step_p->op == OP_WAIT_TIME) ? "wait" : "pump",
- step_p->val / 1000.0f);
- break;
- }
-
- return buff;
- }
-
- const char *wf_name(uint16_t index) {
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return NULL;
- }
- return mem_data()->wf[index].name;
- }
-
- const char *wf_author(uint16_t index) {
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return NULL;
- }
- return mem_data()->wf[index].author;
- }
-
- struct wf_state wf_status(void) {
- struct wf_state s = {
- .status = status,
- .index = step,
- .count = mem_data()->wf[wf_i].count,
- .step = &mem_data()->wf[wf_i].steps[step],
- .start_val = start_val,
- .curr_val = curr_val,
- };
- return s;
- }
-
- void wf_start(uint16_t index) {
- if (status != WF_IDLE) {
- debug("workflow already running");
- return;
- }
- if (index >= mem_data()->wf_count) {
- debug("invalid index %d", index);
- return;
- }
-
- status = WF_RUNNING;
- wf_i = index;
- step = 0;
-
-
-
- DO_WHILE(volcano_set_heater_state(true),
- !(volcano_get_state() & VOLCANO_STATE_HEATER));
- volcano_discover_characteristics(true, false);
-
- do_step();
- }
-
- void wf_reset(void) {
- status = WF_IDLE;
- }
-
- void wf_run(void) {
- if (status == WF_IDLE) {
- return;
- }
-
- bool done = false;
-
- switch (mem_data()->wf[wf_i].steps[step].op) {
- case OP_SET_TEMPERATURE:
- done = true;
- break;
-
- case OP_WAIT_TEMPERATURE: {
- uint16_t temp = volcano_get_current_temp();
-
-
- if (start_val == 0) {
- start_val = temp;
- }
-
- curr_val = temp;
- done = (temp >= (mem_data()->wf[wf_i].steps[step].val - 5));
- break;
- }
-
- case OP_PUMP_TIME:
- case OP_WAIT_TIME: {
- uint32_t now = to_ms_since_boot(get_absolute_time());
- uint32_t diff = now - start_t;
- curr_val = diff;
- done = (diff >= mem_data()->wf[wf_i].steps[step].val);
- break;
- }
- }
-
- if (done) {
- if (mem_data()->wf[wf_i].steps[step].op == OP_PUMP_TIME) {
- DO_WHILE(volcano_set_pump_state(false),
- volcano_get_state() & VOLCANO_STATE_PUMP);
- }
-
- step++;
- if (step >= mem_data()->wf[wf_i].count) {
- status = WF_IDLE;
- DO_WHILE(volcano_set_heater_state(false),
- volcano_get_state() & VOLCANO_STATE_HEATER);
- debug("workflow finished");
- } else {
- do_step();
- }
- }
- }
|