#ifndef _EC_H_
#define _EC_H_

#include <Arduino.h>

#define EC_PIN_RELAY_PROBE 27
#define EC_PIN_RELAY_CALIBRATION 26
#define EC_PIN_RELAY_RANGE 25


#define EC_PIN_ADC 4
#define EC_PIN_FREQ 5
#define EC_PWM_CH 0
#define EC_RESOLUTION 8
#define EC_FREQUENCY 5000

#define EC_ARRAY_SIZE 128
uint16_t ec_array_rangeLow[EC_ARRAY_SIZE];
uint16_t ec_array_rangeHigh[EC_ARRAY_SIZE];
uint16_t ec_array_pos=EC_ARRAY_SIZE*2;
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
  //One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*2
#define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement



float ec_calib_rangeLow_Rlow=0; //adc value for low value resistor on low resistor value range
float ec_calib_rangeLow_Rhigh=0; //adc value for high value resistor on low resistor value range
float ec_calib_rangeHigh_Rlow=0; //adc value for low value resistor on high resistor value range
float ec_calib_rangeHigh_Rhigh=0; //adc value for high value resistor on high resistor value range
const float ec_calibresistor_low=990; //value of low value calibration resistor. Low is Relay NO
const float ec_calibresistor_high=9943; //value of high value calibration resistor. HIGH is Relay NC

unsigned long ec_last_calibration=0; //millis of last calibration
#define EC_CALIBRATION_VALID_TIME 120000 //time in ms a calibration is valid for
#define EC_RELAY_SWITCH_SETTLETIME 500  //time until voltage of ec circuit has settled

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

enum ECState{IDLE,CALIBRATELOW,CALIBRATEHIGH,MEASURE};

ECState ecstate=CALIBRATELOW;


bool ec_measurementReady();
void ec_startMeasurement();
void ec_setRange(bool);
void ec_connectProbe(bool);
void ec_setCalibration(bool calib);
void ec_releaseRelay();
void ec_startCalibration();
void ec_checkIfSettleTimeOK();
float ec_getResistance(float adc,float caliblow,float resistorlow,float calibhigh,float resistorhigh);

void ec_setup() {
  pinMode(EC_PIN_ADC,INPUT);

  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
  pinMode(EC_PIN_RELAY_CALIBRATION,OUTPUT); //LOW=NC Calibration Resistor, HIGH=NO Calib. Res.
  pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor
  ec_releaseRelay();


  ec_startCalibration();
}

void ec_loop(unsigned long loopmillis) {
  static unsigned long last_measurement_ec=0;
  static unsigned long last_read_ec=0;

  switch (ecstate) {
    case IDLE:

      if (loopmillis>ec_last_calibration+EC_CALIBRATION_VALID_TIME) { //calibration needed
        ec_last_calibration=loopmillis;
        ecstate=CALIBRATELOW;
        
        ec_startCalibration();
      }

      
      if (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL && ecstate==IDLE) { //start measurement if idle
        last_measurement_ec=loopmillis;
        ec_startMeasurement();
        ec_connectProbe(true);
        ecstate=MEASURE;
        Serial.println("EC Take Measurement");
      }
      break;


    case CALIBRATELOW:
      if (ec_measurementReady()) {
        //Serial.println("EC CALIBRATELOW measurement ready");
        //save measurement
        ec_calib_rangeLow_Rlow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
        ec_calib_rangeHigh_Rlow=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);

        //ec_checkIfSettleTimeOK();

        //Switch to High calibration
        ecstate=CALIBRATEHIGH;
        
        ec_setCalibration(HIGH);
        ec_setRange(LOW);
        ec_startMeasurement();
        //Serial.println("EC Start calibration high");
      }
      break;

    case CALIBRATEHIGH:
      if (ec_measurementReady()) {
        //Serial.println("EC CALIBRATEHIGH measurement ready");
        //save measurement
        ec_calib_rangeLow_Rhigh=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
        ec_calib_rangeHigh_Rhigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);

        //ec_checkIfSettleTimeOK();

        //Serial.println("EC Release Relay");
        ec_releaseRelay();

        ecstate=IDLE;

        /*
        Serial.println("EC Calibration done");
        Serial.print("ec_calib_rangeLow_Rlow="); Serial.println(ec_calib_rangeLow_Rlow);
        Serial.print("ec_calib_rangeHigh_Rlow="); Serial.println(ec_calib_rangeHigh_Rlow);
        Serial.print("ec_calib_rangeLow_Rhigh="); Serial.println(ec_calib_rangeLow_Rhigh);
        Serial.print("ec_calib_rangeHigh_Rhigh="); Serial.println(ec_calib_rangeHigh_Rhigh);
        */
       Serial.println("EC Calibration Result: ");
       Serial.print(ec_calib_rangeLow_Rlow);
       Serial.print(", "); Serial.print(ec_calib_rangeHigh_Rlow);
       Serial.print(", "); Serial.print(ec_calib_rangeLow_Rhigh);
       Serial.print(", "); Serial.println(ec_calib_rangeHigh_Rhigh);
        
        
      }
      break;

    case MEASURE:
      if (ec_measurementReady()) {
        ec_releaseRelay();
        float adc_rangelow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
        float adc_rangehigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);

        Serial.println();
        float resistance_rangelow=ec_getResistance(adc_rangelow,ec_calib_rangeLow_Rlow,ec_calibresistor_low,ec_calib_rangeLow_Rhigh,ec_calibresistor_high);
        Serial.print("Range Low: ADC="); Serial.print(adc_rangelow); Serial.print(", resistance="); Serial.println(resistance_rangelow);
        Serial.println();
        float resistance_rangehigh=ec_getResistance(adc_rangehigh,ec_calib_rangeHigh_Rlow,ec_calibresistor_low,ec_calib_rangeHigh_Rhigh,ec_calibresistor_high);
        Serial.print("Range High: ADC="); Serial.print(adc_rangehigh); Serial.print(", resistance="); Serial.println(resistance_rangehigh);
        ecstate=IDLE;
      }
      
      break;

  }



  if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/2<EC_ARRAY_SIZE) { //take reading into array if measurement running
    last_read_ec=loopmillis;

    //flag_print= ec_array_pos==EC_ARRAY_SIZE;
    //ec_array_pos%=EC_ARRAY_SIZE;

    if (ec_array_pos<EC_ARRAY_SIZE){ //low range
      ec_setRange(LOW);
    }else{ //high range
      ec_setRange(HIGH);
    }

    if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
      uint16_t value=analogRead(EC_PIN_ADC);
      if (ec_array_pos<EC_ARRAY_SIZE){ //low range
        ec_array_rangeLow[ec_array_pos%EC_ARRAY_SIZE]=value;
        
      }else{ //high range
        ec_array_rangeHigh[ec_array_pos%EC_ARRAY_SIZE]=value;
        
      }

      
      /*
      if (ec_array_pos==0) {
        Serial.println(""); Serial.print("Lowrange:");
      }
      if (ec_array_pos==EC_ARRAY_SIZE) {
        Serial.println(""); Serial.print("Highrange:");
      }
      Serial.print(value); Serial.print(" ");
      if (ec_array_pos==EC_ARRAY_SIZE*2-1) {
        Serial.println("");
      }
      */
      
      
      ec_array_pos++;
    }
    
  }  
  
}



void ec_startCalibration() {
  //Switch to Low calibration
  ec_setCalibration(LOW);
  ec_setRange(LOW);
  ec_startMeasurement();
  Serial.println("EC Started Calibration");
}

void ec_startMeasurement() {
  ec_array_pos=0;
}

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

void ec_setRange(bool range) {
  //range low means low resistor value -> NO -> relay High
  bool val=digitalRead(EC_PIN_RELAY_RANGE);
  if (val!=!range) { //write only if different
    digitalWrite(EC_PIN_RELAY_RANGE,!range);
    ec_last_change_relay=millis();
  }
}

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);
    ec_last_change_relay=millis();
  }
}

void ec_setCalibration(bool calib) {
  //calib low means low resistor value -> NO -> relay high
  ec_connectProbe(false);
  bool val=digitalRead(EC_PIN_RELAY_CALIBRATION);
  if (val!=!calib) { //write only if different
    digitalWrite(EC_PIN_RELAY_CALIBRATION,!calib);
    ec_last_change_relay=millis();
  }
}

void ec_releaseRelay() {
  digitalWrite(EC_PIN_RELAY_PROBE,LOW);
  digitalWrite(EC_PIN_RELAY_CALIBRATION,LOW);
  digitalWrite(EC_PIN_RELAY_RANGE,LOW);
  ec_last_change_relay=millis();
}

void ec_checkIfSettleTimeOK() {
  /*
  Serial.print("ec_array_rangeLow[0]="); Serial.println(ec_array_rangeLow[0]);
  Serial.print("rangeLow min="); Serial.println(getMin(ec_array_rangeLow,EC_ARRAY_SIZE));
  Serial.print("rangeLow max="); Serial.println(getMax(ec_array_rangeLow,EC_ARRAY_SIZE));
  */
  if (ec_array_rangeLow[0]<=getMin(ec_array_rangeLow,EC_ARRAY_SIZE) || ec_array_rangeLow[0]>=getMax(ec_array_rangeLow,EC_ARRAY_SIZE)){
    //is first value the highest or lowest?
    Serial.println("Warning: EC_RELAY_SWITCH_SETTLETIME might be too low! (ec_calib_rangeLow_Rlow)");
  }

  /*
  Serial.print("ec_array_rangeHigh[0]="); Serial.println(ec_array_rangeHigh[0]);
  Serial.print("rangeHigh min="); Serial.println(getMin(ec_array_rangeHigh,EC_ARRAY_SIZE));
  Serial.print("rangeHigh max="); Serial.println(getMax(ec_array_rangeHigh,EC_ARRAY_SIZE));
  */
  if (ec_array_rangeHigh[0]<=getMin(ec_array_rangeHigh,EC_ARRAY_SIZE) || ec_array_rangeHigh[0]>=getMax(ec_array_rangeHigh,EC_ARRAY_SIZE)){
    //is first value the highest or lowest?
    Serial.println("Warning: EC_RELAY_SWITCH_SETTLETIME might be too low! (ec_array_rangeHigh)");
  }
}


float ec_getResistance(float adc,float caliblow,float resistorlow,float calibhigh,float resistorhigh)
{
  //adc = adc reading to calculate resistance for
  //caliblow = adc value from calibration. Low resistance
  //resistorlow = actual resistor value. Low resistance
  //calibhjgh = adc value from calibration. High resistance
  //resistorhigh = actual resistor value. High resistance

  //y=mx+a;
  //resistorlow=m*caliblow+a;
  //resistorhigh=m*calibhigh+a;

  //linear interpolation interpolate
  double m=(resistorhigh-resistorlow)/(calibhigh-caliblow);
  float a=resistorlow-m*caliblow;

  Serial.print("m="); Serial.println(m);
  Serial.print("a="); Serial.println(a);

  return m*adc+a;
}

#endif