#ifndef _EC_H_
#define _EC_H_

#include <Arduino.h>

extern bool eccalibrationoutput;
/*
mqttValueTiming timing_ec_adc;
mqttValueTiming timing_ec_calibadc;
mqttValueTiming timing_ec_adcadjusted;
mqttValueTiming timing_ec_ec;
mqttValueTiming timing_ec_sc;
*/



bool ec_flag_measurement_available=false;

#define EC_ADC_UNAVAILABLE 0
#define EC_UNAVAILABLE -1


//#define EC_PIN_RELAY_PROBE 27 //moved to platformio.ini


//#define EC_PIN_ADC 4
#define EC_ADS_CHANNEL 0
//#define EC_PIN_FREQ 5 //move to platformio.ini
#define EC_PWM_CH 0
#define EC_RESOLUTION 8
#define EC_FREQUENCY 5000

#define EC_CALIB_ARRAY_SIZE 128
uint16_t ec_calib_array[EC_CALIB_ARRAY_SIZE];
uint16_t ec_calib_array_pos=0;
#define EC_CALIB_READ_INTERVAL 250 //interval of reading adc value inside a measurement

#define EC_ARRAY_SIZE 64
uint16_t ec_array[EC_ARRAY_SIZE];
uint16_t ec_array_pos=EC_ARRAY_SIZE;
unsigned long last_measurement_ec=0;
#define EC_MEASUREMENT_INTERVAL 10*60*1000 //complete filtered measurement every x ms  
  //One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
#define EC_READ_INTERVAL 50 //interval of reading adc value inside a measurement. one reading takes about 9-10ms

#define EC_RELAY_SWITCH_SETTLETIME 500  //time until voltage of ec circuit has settled

//const uint16_t ec_centerADCvalue=9026; //adc value when probe resistance is equal to the range resistor (mean of both)
//Range Resistor is two parallel 1k2 = 600 Ohm

unsigned long ec_last_change_relay=0; //millis of last relay change

enum ECState{IDLE,MEASURE};



ECState ecstate=IDLE;

float ec_calib_adc;

float ec_adc;
float ec_adc_adjusted; //adjusted for reference resistor
float ec; //ec value after adjustment for reference (at current temperature)
float ec25; //ec value but temperature adjusted for 25 degC

float ec_tempadjust_alpa=0.02;
float ec_reference_adc=EC_REFERENCE_ADC; //adc reference value for the calibration resistor measurement.

//EC short circuit adc value: 17497, 17861.4 (for connection resistance testing)
//EC open circuit adc value: 738, 730, 737.27

//x^0*p[0] + ... + x^n*p[n]
//float ec_calibration_polynom[]={691.5992624638029,-1.4015367296761692,0.0008513503472324141,-2.2140576823179093e-07,2.8962580780180067e-11,-1.8577565383307114e-15,4.7162479484903865e-20};
//float ec_calibration_polynom[]={1033.928052655456,-3.8909104921922895,0.005627541436014758,-4.103988840997024e-06,1.7231981870816133e-09,-4.433707707721975e-13,7.203892111369395e-17,-7.406549810844244e-21,4.667420606439905e-25,-1.6439457516812463e-29,2.477292190335455e-34}; //20220505
//float ec_calibration_polynom[]={-323.68589929771457,0.5836096440900665,-0.000279737392438965,5.98673062873e-08,-5.4460235093798435e-12,1.8535134644431135e-16}; //20230509
//float ec_calibration_polynom[]={212.6826331524675,-0.6043878865263305,0.000571551634082491,-1.827897106718841e-07,2.682337041246909e-11,-1.8368511021965982e-15,4.8269168538877025e-20}; //20230509 manuell
//float ec_calibration_polynom[]={8.718380956513695,-0.026463423062356713,3.425216464107108e-05,-4.069826379094172e-09,2.478900495960682e-13}; //20240423, graphite electrodes

// 20240423, graphite electrodes DB {8.718380956513695,-0.026463423062356713,3.425216464107108e-05,-4.069826379094172e-09,2.478900495960682e-13}
// 20240423, graphite electrodes NFT {18.785904241636743,-0.04069178351449846,3.528797358514823e-05,-4.214254847500995e-09,2.543662736303669e-13} 
float ec_calibration_polynom[]=EC_CALIBRATION_POLYNOM;

float ec_calibration_linearize_below_adc=EC_CALIBRATION_LINEARIZE_BELOW_ADC; //use linear approximation below this adc value. 0=disable
float ec_calibration_linear_lowADC=EC_CALIBRATION_LINEAR_LOWADC; //x0
float ec_calibration_linear_lowEC=EC_CALIBRATION_LINEAR_LOWEC; //y0

/*
float ec_calibration_polynom_B[]={18.785904241636743,-0.04069178351449846,3.528797358514823e-05,-4.214254847500995e-09,2.543662736303669e-13}; //20240423, graphite electrodes
float ec_calibration_linearize_below_adc_B=2000; //use linear approximation below this adc value. 0=disable
float ec_calibration_linear_lowADC_B=728; //x0
float ec_calibration_linear_lowEC_B=0; //y0
*/


bool ec_measurementReady();
void ec_startMeasurement();
void ec_setRange(uint8_t range);
void ec_connectProbe(bool);
void ec_releaseRelay();
float ec_getECfromADC(float adc, float ec_calibration_polynom[], size_t len_ec_calibration_polynom,  float ec_calibration_linearize_below_adc, float ec_calibration_linear_lowADC, float ec_calibration_linear_lowEC);
float ec_calculateEC25(float pEC,float pTemp);
bool ec_measurementRunning();

void ec_setup() {
  /*
  timing_ec_adc.minchange=0.0;
  timing_ec_adc.maxchange=250;
  timing_ec_adc.mintime=10*000;
  timing_ec_adc.maxtime=60*60*1000;

  timing_ec_calibadc.minchange=0.0;
  timing_ec_calibadc.maxchange=250;
  timing_ec_calibadc.mintime=10*000;
  timing_ec_calibadc.maxtime=60*60*1000;

  timing_ec_adcadjusted.minchange=0.0;
  timing_ec_adcadjusted.maxchange=2.0;
  timing_ec_adcadjusted.mintime=10*000;
  timing_ec_adcadjusted.maxtime=30*60*1000;

  timing_ec_ec.minchange=0.0;
  timing_ec_ec.maxchange=50;
  timing_ec_ec.mintime=10*000;
  timing_ec_ec.maxtime=60*60*1000;

  timing_ec_sc.minchange=0.0;
  timing_ec_sc.maxchange=50;
  timing_ec_sc.mintime=10*000;
  timing_ec_sc.maxtime=60*60*1000;
  */

  ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
  ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
  ledcWrite(EC_PWM_CH, 127); //50% duty cycle

  pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected

  //Test Relay
  digitalWrite(EC_PIN_RELAY_PROBE,HIGH);
  delay(500);
  digitalWrite(EC_PIN_RELAY_PROBE,LOW);

  ec_releaseRelay();

}

void ec_loop(unsigned long loopmillis) {
  if (!adsenabled) {
    return;
  }
  
  static unsigned long last_read_ec=0;


  switch (ecstate) {
    case IDLE:
      
      if ( (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL) || force_ec_measurement) { //start measurement if idle
        //Serial.println("DEBUG: Start measurement");
        last_measurement_ec=loopmillis;
        force_ec_measurement=false;
        ec_startMeasurement();
        ec_connectProbe(true);

        
        ecstate=MEASURE;
      }
      break;

    case MEASURE:
      if (ec_measurementReady()) {
        //Serial.println("DEBUG: Measurement Ready");
        
        ec_releaseRelay();
        ec_adc=getMean(ec_array,EC_ARRAY_SIZE);
        //Serial.print("ec_adc="); Serial.println(ec_adc);
        if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)){
          ec_calib_adc=getMean(ec_calib_array,EC_CALIB_ARRAY_SIZE);
          //Serial.print("ec_calib_adc="); Serial.println(ec_calib_adc);
          ec_adc_adjusted=mapf(ec_adc,0,ec_calib_adc,0,ec_reference_adc);
          //Serial.print("ec_adc_adjusted="); Serial.println(ec_adc_adjusted);
          
          ec=ec_getECfromADC(ec_adc_adjusted, ec_calibration_polynom, sizeof(ec_calibration_polynom), ec_calibration_linearize_below_adc, ec_calibration_linear_lowADC, ec_calibration_linear_lowEC);
          //Serial.print("ec="); Serial.println(ec);
          ec25=ec_calculateEC25(ec,tempC_reservoir);
          
          
          //Serial.println("DEBUG: EC OK");
        }else{
          ec_calib_adc=EC_ADC_UNAVAILABLE;
          ec_adc_adjusted=EC_ADC_UNAVAILABLE;
          ec=EC_UNAVAILABLE;
          ec25=EC_UNAVAILABLE;
          //Serial.println("DEBUG: EC unavailable");
        }
          
        ec_flag_measurement_available=true;
        ecstate=IDLE;
        

      }
      
      break;

  }


  if (ec_measurementRunning()) { //measurement running
    if (loopmillis>last_read_ec+EC_READ_INTERVAL) { //take reading into array
      last_read_ec=loopmillis;

      if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled 
        //Serial.print("Get ADC Reading"); 
        uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
        //Serial.print(". Write to pos ");
        //Serial.print(ec_array_pos);
        //Serial.print(" = ");
        //Serial.println(value);

        
        
        ec_array[ec_array_pos]=value;
        
        ec_array_pos++;
      }
      
    }
  }else{ //measurement not running, then take calibration readings
    if (loopmillis>last_read_ec+EC_CALIB_READ_INTERVAL) { //take reading into arraysdf
      last_read_ec=loopmillis;

      if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled    
        uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
        
        ec_calib_array[ec_calib_array_pos]=value;
        
        ec_calib_array_pos++;
        ec_calib_array_pos%=EC_CALIB_ARRAY_SIZE;

        if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)){
          ec_calib_adc=getMean(ec_calib_array,EC_CALIB_ARRAY_SIZE);
        }
      }
    }
  }
  
}



void ec_startMeasurement() {
  ec_array_pos=0;
}

bool ec_measurementReady(){
  if (ec_array_pos>=EC_ARRAY_SIZE) { //reached end of both arrays
    return true;
  }else{
    return false;
  }
}


void ec_connectProbe(bool relay) {
  bool val=digitalRead(EC_PIN_RELAY_PROBE);
  if (val!=relay) { //write only if different
    digitalWrite(EC_PIN_RELAY_PROBE,relay);
    //Serial.print("DEBUG: Set Relay to "); Serial.println(relay);
    ec_last_change_relay=millis();
  }
}

void ec_releaseRelay() {
  digitalWrite(EC_PIN_RELAY_PROBE,LOW);
  //Serial.println("DEBUG: Released Relays");
  ec_last_change_relay=millis();
}

float ec_getECfromADC(float adc, float ec_calibration_polynom[], size_t len_ec_calibration_polynom,  float ec_calibration_linearize_below_adc, float ec_calibration_linear_lowADC, float ec_calibration_linear_lowEC) {
  //uint8_t polynom_order=sizeof(ec_calibration_polynom) / sizeof(ec_calibration_polynom[0]);
  uint8_t polynom_order=len_ec_calibration_polynom / sizeof(ec_calibration_polynom[0]);
  double _ec=0;
  if (adc>=ec_calibration_linearize_below_adc) { //adc is in range where polynomial approximation fits well
    for (uint8_t i=0;i<polynom_order;i++) {
      _ec+=pow(adc,i)*ec_calibration_polynom[i];
    }
  }else{ //low ec region. linear approximation works better here
    float x1=ec_calibration_linearize_below_adc;
    float y1=0;
    for (uint8_t i=0;i<polynom_order;i++) { //get y1 value from curve
      y1+=pow(x1,i)*ec_calibration_polynom[i];
    }
    float x0=ec_calibration_linear_lowADC;
    float y0=ec_calibration_linear_lowEC;

    _ec=mapf(adc,x0,x1,y0,y1); //linear approximation    
  }
  if (_ec>=0) {
    return _ec;
  }else{
    return 0;
  }
  
}

float ec_calculateEC25(float pEC,float pTemp) 
{
  return pEC/(1.0+ec_tempadjust_alpa*(pTemp-25.0));
}

bool ec_measurementRunning() {
  return (ec_array_pos<EC_ARRAY_SIZE);
}

#endif