/*
* state_volcano_run.c
*
* Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See .
*/
#include
#include
#include "config.h"
#include "log.h"
#include "workflow.h"
#include "state.h"
#include "state_volcano_run.h"
#include "menu.h"
static uint16_t wf_index = 0;
static bd_addr_t ble_addr = {0};
static bd_addr_type_t ble_type = 0;
static bool wait_for_connect = false;
static bool wait_for_disconnect = false;
void state_volcano_run_index(uint16_t index) {
wf_index = index;
}
void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
memcpy(ble_addr, addr, sizeof(bd_addr_t));
ble_type = type;
}
void state_volcano_run_enter(void) {
debug("workflow connect");
ble_connect(ble_addr, ble_type);
wait_for_connect = true;
}
void state_volcano_run_exit(void) {
wf_reset();
}
static void draw(struct menu_state *menu) {
struct wf_state state = wf_status();
snprintf(menu->buff, MENU_MAX_LEN, "%d / %d", state.step, state.count);
// TODO visualize
}
void state_volcano_run_run(void) {
if (wait_for_connect && ble_is_connected()) {
wait_for_connect = false;
debug("workflow start");
wf_start(wf_index);
}
menu_run(draw);
if (!wait_for_connect) {
struct wf_state state = wf_status();
if (state.status == WF_IDLE) {
debug("workflow disconnect");
ble_disconnect();
wait_for_disconnect = true;
}
}
if (wait_for_disconnect && !ble_is_connected()) {
wait_for_disconnect = false;
debug("workflow done");
state_switch(STATE_SCAN);
}
}