#ifndef INC_ERRORS_H
#define INC_ERRORS_H

#include <stdint.h>

#define ERROR_SOURCE_VOLTAGES                     (1 << 0)
#define ERROR_SOURCE_TEMPERATURES                 (1 << 1)
#define ERROR_SOURCE_TOO_FEW_WORKING_TEMP_SENSORS (1 << 2)
#define ERROR_SOURCE_OPEN_CELL_CONNECTION         (1 << 3)
#define ERROR_SOURCE_EEPROM                       (1 << 4)
#define ERROR_SOURCE_INTERNAL                     (1 << 5)

#define ERROR_TIME_THRESH 150 // ms

typedef enum {
  SEK_OVERTEMP = 0x0,
  SEK_UNDERTEMP = 0x1,
  SEK_OVERVOLT = 0x2,
  SEK_UNDERVOLT = 0x3,
  SEK_TOO_FEW_TEMPS = 0x4,
  SEK_OPENWIRE = 0x5,
  SEK_EEPROM_ERR = 0x6,
  SEK_INTERNAL_BMS_TIMEOUT = 0x7,
  SEK_INTERNAL_BMS_CHECKSUM_FAIL = 0x8,
  SEK_INTERNAL_BMS_OVERTEMP = 0x9,
  SEK_INTERNAL_BMS_FAULT = 0xA,
} SlaveErrorKind;

typedef struct {
  int error_sources;
  SlaveErrorKind data_kind;
  uint8_t data[4];
  uint32_t errors_since;
} SlaveErrorData;

extern SlaveErrorData error_data;

void set_error_source(int source);
void clear_error_source(int source);

#endif // INC_ERRORS_H