diff --git a/.gitignore b/.gitignore index f024b50..a965e14 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ ThermistorTable.h Makefile sim doc +datalog.out diff --git a/simulator/data_recorder.c b/simulator/data_recorder.c new file mode 100644 index 0000000..b181dc9 --- /dev/null +++ b/simulator/data_recorder.c @@ -0,0 +1,56 @@ +/* + * data_recorder.c + * + * Record simulator data to a file in real-time. + * + */ + +#include "simulator.h" +#include "data_recorder.h" +#include +#include + +static FILE *file; + +#define MAX_PINS 100 +static uint32_t values[MAX_PINS]; ///< Pin and value states +static int pin_count; ///< Number of pins we emit + +void recorder_init(const char* filename) { + sim_assert( ! file, "Recorder already initialized"); + + file = fopen(filename, "w"); + sim_assert(file, "record_init: failed to create file"); + + time_t t = time(NULL); + fprintf(file, "; Teacup_Firmware simulator data log on %s\n", + asctime(localtime(&t))); + fflush(file); +} + +void add_trace_var(const char* name, int pin) { + sim_assert(file, "add_trace_var: Recorder not initialized"); + sim_assert(pin < MAX_PINS, "pin number invalid"); + + fprintf(file, "; %d - %s\n", pin, name); + if (pin >= pin_count) + pin_count = pin + 1; + fflush(file); +} + +// Record a binary signal change +void record_pin(int pin, bool state, uint32_t t) { + if (state == values[pin]) return; + + sim_assert(file , "record_pin: Recorder not initialized"); + sim_assert(pin < MAX_PINS, "pin number invalid"); + + values[pin] = state; + + // Naive format: each line contains all values, beginning with the time + fprintf(file, "%u", t); + for (int i = 0; i < pin_count; i++) + fprintf(file, "\t%u", values[i]); + fprintf(file, "\n"); + fflush(file); +} diff --git a/simulator/data_recorder.h b/simulator/data_recorder.h new file mode 100644 index 0000000..cf20acf --- /dev/null +++ b/simulator/data_recorder.h @@ -0,0 +1,12 @@ +/* + * data_recorder.h + */ + +#ifndef DATA_RECORDER_H_ +#define DATA_RECORDER_H_ + +void recorder_init(const char* filename); +void record_pin(int pin, bool state, uint32_t time); +void add_trace_var(const char* name, int pin); + +#endif /* DATA_RECORDER_H_ */ diff --git a/simulator/simulator.c b/simulator/simulator.c index f9b07c7..e476985 100644 --- a/simulator/simulator.c +++ b/simulator/simulator.c @@ -3,6 +3,7 @@ #include #include "simulator.h" +#include "data_recorder.h" uint8_t ACSR; uint8_t TIMSK1; @@ -35,6 +36,33 @@ void sim_start(int argc, char** argv) { // Save these for the serial_init code g_argc = argc; g_argv = argv; + + recorder_init("datalog.out"); + + // Record pin names in datalog +#define NAME_PIN(x) add_trace_var(#x , x); + NAME_PIN(X_STEP_PIN); + NAME_PIN(X_DIR_PIN); + NAME_PIN(X_MIN_PIN); + NAME_PIN(X_ENABLE_PIN); + NAME_PIN(Y_STEP_PIN); + NAME_PIN(Y_DIR_PIN); + NAME_PIN(Y_MIN_PIN); + NAME_PIN(Y_ENABLE_PIN); + NAME_PIN(Z_STEP_PIN); + NAME_PIN(Z_DIR_PIN); + NAME_PIN(Z_MIN_PIN); + NAME_PIN(Z_ENABLE_PIN); + NAME_PIN(E_STEP_PIN); + NAME_PIN(E_DIR_PIN); + NAME_PIN(E_ENABLE_PIN); + + NAME_PIN(STEPPER_ENABLE_PIN); + + NAME_PIN(SCK); + NAME_PIN(MOSI); + NAME_PIN(MISO); + NAME_PIN(SS); } /* -- debugging ------------------------------------------------------------ */ @@ -129,6 +157,8 @@ void WRITE(pin_t pin, bool s) { } if (old_state != s) { + uint32_t useconds = 0; // TODO + record_pin(pin, s, useconds); #ifdef TRACE_ALL_PINS fgreen(); for (int i = 0; i < PIN_NB; i++) {