//#define DEBUG
/*
 *  <s> Buffer wird mit vier mal ADCread single gefüllt
 *  <vx> Gain wird mit x gesetzt
 * 
 */
#include <Wire.h>
#include <Adafruit_ADS1015.h>
  // Descomentar el que interese
  // ads.setGain(GAIN_TWOTHIRDS);  +/- 6.144V  1 bit = 0.1875mV (default)
  // ads.setGain(GAIN_ONE);        +/- 4.096V  1 bit = 0.125mV
  // ads.setGain(GAIN_TWO);        +/- 2.048V  1 bit = 0.0625mV
  // ads.setGain(GAIN_FOUR);       +/- 1.024V  1 bit = 0.03125mV
  // ads.setGain(GAIN_EIGHT);      +/- 0.512V  1 bit = 0.015625mV
  // ads.setGain(GAIN_SIXTEEN);    +/- 0.256V  1 bit = 0.0078125mV 
  
Adafruit_ADS1115 ads;
float multiplier = 0.1875F;
int aktgain = 0;


union dual{
             char      charBuff[14];
             int         intBuff[7];
          };

dual myData;

byte RxCmd [4] = {0,0,0,0};

const int buttonPin = 2;     // select the input pin for the potentiometer
int ValueA0 = 0;        // variable to store the value coming from the sensor
int buttonState = 0; 
int count = 0;
boolean now =false;
unsigned long times=0;

void setup(void) 
{
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  ads.begin();

}

void readCounter(){
if (buttonState == HIGH) {
    if (now==false){
      if (times ==0){
         times = millis();
         count += +1;
         #ifdef DEBUG
         Serial.println(count);
         #endif
      } else {
        unsigned long delta;
        delta = millis() -times;
        if (delta >1000){
        count += +1;
         #ifdef DEBUG
         Serial.println(count);
         #endif 
        }
      }     
      now=true;
    }
    //digitalWrite(ledPin, LOW);
  } else {
    // turn LED off:
    now= false;
    times = 0;   
    //digitalWrite(ledPin, HIGH);
  } 
}

void readADCinBuffer(){ 
  myData.intBuff[0] = ads.readADC_SingleEnded(0);
  myData.intBuff[1] = ads.readADC_SingleEnded(1);
  myData.intBuff[2] = ads.readADC_SingleEnded(2);
  myData.intBuff[3] = ads.readADC_SingleEnded(3);
  myData.intBuff[4] = count;
  myData.intBuff[5] = aktgain;
  myData.intBuff[6] = ValueA0;
}

void setgain(char i){
 switch (i){
    case '1' :  ads.setGain(GAIN_ONE); 
              multiplier = 0.125F;
              aktgain = 1;
    break;
    case '2' :  ads.setGain(GAIN_TWO); 
              multiplier = 0.0625F;
              aktgain = 2;
    break;
    case '3' :  ads.setGain(GAIN_FOUR); 
              multiplier = 0.03125F;
              aktgain = 3;
    break;
    case '4' :  ads.setGain(GAIN_EIGHT); 
              multiplier = 0.015625F;
              aktgain = 4;
    break;
    case '5' :  ads.setGain(GAIN_SIXTEEN); 
              multiplier = 0.0078125F;
              aktgain = 5;
    break;
 default :
   ads.setGain(GAIN_TWOTHIRDS);
   multiplier = 0.1875F;
   aktgain = 0;
 } 
 #ifdef DEBUG
 Serial.print("multiplier "); 
 Serial.println( multiplier);
 #endif
}

#ifdef DEBUG
void readADC(){
int16_t adc0, adc1, adc2, adc3; 
  adc0 = ads.readADC_SingleEnded(0);
  adc1 = ads.readADC_SingleEnded(1);
  adc2 = ads.readADC_SingleEnded(2);
  adc3 = ads.readADC_SingleEnded(3);
  Serial.print("AIN0: "); Serial.println(adc0 * multiplier);
  Serial.print("AIN1: "); Serial.println(adc1 * multiplier);
  Serial.print("AIN2: "); Serial.println(adc2 * multiplier);
  Serial.print("AIN3: "); Serial.println(adc3 * multiplier);
  Serial.println(multiplier);
} 
#endif


void loop(void) 
{
  #ifdef DEBUG
    readADC();
  #endif
  for (int i = 0 ; i < 4 ; i++) {
    RxCmd[i] = 0;
  }
  char inc;
  readADCinBuffer();
  buttonState = digitalRead(buttonPin);
  ValueA0 = analogRead(A0);
  readCounter();
 if (Serial.available() > 0) {
   delay(2);
    RxCmd[0] = Serial.read(); 
    if (RxCmd[0] == '<') {
        int i =1;
      while(Serial.available()) {
        delay(1);
        RxCmd[i] = Serial.read();
        //if (RxCmd[i]>127 || i>7)      break;                                    //Communication error
        if (RxCmd[i]== '>') {
          break;                                                                  //Read all data 
        }
        i++;
        
      }
    }
  }
  if ( RxCmd[1] == 's' ){
    Serial.write(myData.charBuff,14);
  }
  if (RxCmd[1] == 'v'){
    setgain((int)RxCmd[2]);
    //Serial.println( aktgain );
  }
}
