blk_box_tc/main/drivers/state_tracking.cpp

87 lines
2.1 KiB
C++

#include "state_tracking.h"
#include <vector>
enum state_t {
STATE_IDLE = 0,
STATE_RECORDING = 1,
STATE_PLAYBACK = 2,
};
static std::vector<Event> registered_events;
static bool should_close_recording_stream;
static FILE* recording_stream;
static uint32_t recording_start_time;
static bool should_close_playback_stream;
static FILE* playback_stream;
static state_t state = STATE_IDLE;
void register_event(const char* name, void (*replay_callback)(uint32_t, const char*)) {
Event event = {
.name = name,
.replay_callback = replay_callback,
};
registered_events.push_back(event);
}
void event_occured(const char* name, const char* arg) {
if (state != STATE_RECORDING) return;
if (name == nullptr) return;
if (recording_stream == nullptr) {
ESP_LOGE("state_tracking", "We are in state recording, but recording stream is null");
return;
}
arg = (arg == nullptr) ? "" : arg;
uint32_t ticks = xTaskGetTickCount() - recording_start_time;
fprintf(recording_stream, "%ld,%s:%s\n", ticks, name, (arg == nullptr) ? "" : arg);
fflush(recording_stream);
}
bool set_recording_source(FILE* stream, bool should_close) {
if (state == STATE_RECORDING) return false;
recording_stream = stream;
should_close_recording_stream = should_close;
return true;
}
bool set_playback_source(FILE* stream, bool should_close) {
if (state == STATE_PLAYBACK) return false;
playback_stream = stream;
should_close_playback_stream = should_close;
return true;
}
bool start_recording() {
if (state != STATE_IDLE) return false;
if (recording_stream == nullptr) return false;
state = STATE_RECORDING;
recording_start_time = xTaskGetTickCount();
return true;
}
bool stop_recording() {
if (state != STATE_RECORDING) return false;
state = STATE_IDLE;
fflush(recording_stream);
if (should_close_recording_stream) {
fclose(recording_stream);
recording_stream = nullptr;
}
return true;
}
bool start_playback() {
// TODO: impl
return false;
}
bool stop_playback() {
// TODO: impl
return false;
}