#ifndef _SOILMOISTURE_H_
#define _SOILMOISTURE_H_

#define SM1_ADS_CHANNEL 1
#define SM2_ADS_CHANNEL 2
#define SM3_ADS_CHANNEL 3

#define READINTERVAL_SM 100

mqttValueTiming timing_soilmoisture_sm1;
mqttValueTiming timing_soilmoisture_sm2;
mqttValueTiming timing_soilmoisture_sm3;

//Calibration values
//high=adc value for sensor in air
//low=adc value for sensor in NaCl solution at 25°C with 12880 uS/cm
float sm1_low=4555;
float sm1_high=11799;
float sm2_low=3235;
float sm2_high=16050;
float sm3_low=0;
float sm3_high=16000;
//sm1 is underwater reservoir soilmoisture sensor (#2)
//sm2 is soilmoisture sensor (#1)


unsigned long last_read_sm=0;


#define SM_SIZE 16
uint8_t sm_mean_pos=0;
uint16_t sm_mean1array[SM_SIZE];
uint16_t sm_mean2array[SM_SIZE];
uint16_t sm_mean3array[SM_SIZE];


#define SM_DISCONNECTED -1

float sm_mean1=SM_DISCONNECTED;
float sm_mean2=SM_DISCONNECTED;
float sm_mean3=SM_DISCONNECTED;



uint8_t sm_readchannel=0;


void sm_setup() {

  timing_soilmoisture_sm1.minchange=0.0;
  timing_soilmoisture_sm1.maxchange=0.05;
  timing_soilmoisture_sm1.mintime=2*1000;
  timing_soilmoisture_sm1.maxtime=60*60*1000;

  timing_soilmoisture_sm2.minchange=0.0;
  timing_soilmoisture_sm2.maxchange=0.05;
  timing_soilmoisture_sm2.mintime=2*1000;
  timing_soilmoisture_sm2.maxtime=60*60*1000;

  timing_soilmoisture_sm3.minchange=0.0;
  timing_soilmoisture_sm3.maxchange=0.05;
  timing_soilmoisture_sm3.mintime=2*1000;
  timing_soilmoisture_sm3.maxtime=60*60*1000;


  for (uint16_t i=0;i<SM_SIZE;i++) {
    sm_mean1array[i]=SM_DISCONNECTED;
    sm_mean2array[i]=SM_DISCONNECTED;
    sm_mean3array[i]=SM_DISCONNECTED;
  }

}

void sm_loop(unsigned long loopmillis) {
    if (loopmillis>=last_read_sm+READINTERVAL_SM) {
        
        switch (sm_readchannel) {
            case 0:
                sm_mean1array[sm_mean_pos]=ADS.readADC(SM1_ADS_CHANNEL);
                sm_readchannel++;

                break;
            case 1:
                sm_mean2array[sm_mean_pos]=ADS.readADC(SM2_ADS_CHANNEL);
                sm_readchannel++;

                break;

            default: //2 and above
                
                sm_mean3array[sm_mean_pos]=ADS.readADC(SM3_ADS_CHANNEL);
                sm_readchannel=0;

                sm_mean_pos++;
                sm_mean_pos%=SM_SIZE;  
                last_read_sm=loopmillis; 

                

                if (isValueArrayOK(sm_mean1array,SM_SIZE,SM_DISCONNECTED)){
                    float _sm_mean_raw;
                    _sm_mean_raw=getMean(sm_mean1array,SM_SIZE);
                    sm_mean1=mapf(_sm_mean_raw,sm1_low,sm1_high,1.0,0.0);

                    _sm_mean_raw=getMean(sm_mean2array,SM_SIZE);
                    sm_mean2=mapf(_sm_mean_raw,sm2_low,sm2_high,1.0,0.0);

                    _sm_mean_raw=getMean(sm_mean3array,SM_SIZE);
                    sm_mean3=mapf(_sm_mean_raw,sm3_low,sm3_high,1.0,0.0);
                }

                break;
        }

        //Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print("\t "); Serial.println(value);
    }
}


#endif