#ifndef EIS_H
#define EIS_H

#include "ad5940.h"

#define EIS_MAX_POINTS 100

typedef enum {
    RTIA_200 = 0, RTIA_1K, RTIA_5K, RTIA_10K,
    RTIA_20K, RTIA_40K, RTIA_80K, RTIA_160K,
    RTIA_EXT_DE0,
    RTIA_COUNT
} EISRtia;

typedef enum {
    RCAL_200R = 0,  /* RCAL0 ↔ RCAL1 */
    RCAL_3K,        /* RCAL0 ↔ AIN0  */
    RCAL_COUNT
} EISRcal;

typedef enum {
    ELEC_4WIRE = 0, /* AIN3 drive, AIN0 return, AIN2/AIN1 sense */
    ELEC_3WIRE,     /* CE0 drive, RE0 sense+, SE0 sense-/TIA    */
    ELEC_COUNT
} EISElectrode;

typedef struct {
    float freq_start_hz;
    float freq_stop_hz;
    uint16_t points_per_decade;

    EISRtia rtia;
    EISRcal rcal;
    EISElectrode electrode;
    uint32_t pga;
    uint32_t excit_amp;
} EISConfig;

typedef struct {
    float freq_hz;
    float mag_ohms;
    float phase_deg;
    float z_real;
    float z_imag;
    float rtia_mag_before;
    float rtia_mag_after;
    float rev_mag;
    float rev_phase;
    float pct_err;
} EISPoint;

typedef int (*eis_point_cb_t)(uint16_t idx, const EISPoint *pt);

void eis_default_config(EISConfig *cfg);
void eis_init(const EISConfig *cfg);
void eis_reconfigure(const EISConfig *cfg);
int  eis_measure_point(float freq_hz, EISPoint *out);
int  eis_sweep(EISPoint *out, uint32_t max_points, eis_point_cb_t cb);
uint32_t eis_calc_num_points(const EISConfig *cfg);

#endif
