#include "foc/mc_error.h"
#include "foc/core/PMSM_FOC_Core.h"
#include "libs/logger.h"

static err_record_t err[FOC_CRIT_Err_Max];

void err_add_record(u8 err_code, s16 err_value) {
	if (err_code >= FOC_CRIT_Err_Max) {
		return;
	}
	err[err_code].err_value = err_value;
	err[err_code].err_time = get_tick_ms();
}

int err_get_record(u8 index, int size, err_record_t *err_out) {
	size = min(size, (FOC_CRIT_Err_Max -  index));
	for (int i = index; i < index + size; i++) {
		err_out[i].err_value = err[i].err_value;
		err_out[i].err_time = err[i].err_time;
	}
	return size;
}

void err_code_log(void) {
	for (int i = 0;  i < FOC_CRIT_Err_Max; i++) {
		if (err[i].err_time == 0) {
			continue;
		}
		sys_error("err:%d, time = %d\n", err[i].err_value, err[i].err_time);
	}
}
