#include <Wire.h>
#include <Adafruit_ADS1015.h>

#include <MozziGuts.h>
#include <Oscil.h>
#include <tables/square_no_alias_2048_int8.h>
//#include <ReverbTank.h> /*Only required for reverb*/
//#include <SensorToButton.h>
//ReverbTank reverb;

//Multiplex adresses
Adafruit_ADS1115 adsa (0x48);
Adafruit_ADS1115 adsb (0x49);
Adafruit_ADS1115 adsc (0x4A);
Adafruit_ADS1115 adsd (0x4B);

#define CONTROL_RATE 64 // powers of 2 please

Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> tone_1(SQUARE_NO_ALIAS_2048_DATA);
Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> tone_2(SQUARE_NO_ALIAS_2048_DATA);
Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> tone_3(SQUARE_NO_ALIAS_2048_DATA);

int count= 0;
boolean note1 = false;
boolean note2 = false;
boolean note3 = false;
boolean codeswitch = false;


void setup(){

  adsa.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  adsb.setGain(GAIN_ONE); 
  adsc.setGain(GAIN_ONE); 
  adsd.setGain(GAIN_ONE); 
  
  //start the multiplexing
  adsa.begin();
  adsb.begin();
  adsc.begin();
  adsd.begin();
  
  startMozzi(CONTROL_RATE);
  tone_1.setFreq(262);
  tone_1.setPhase(120);
  tone_2.setFreq(330);
  tone_2.setPhase(240 );
  tone_3.setFreq(392);
}
void updateControl(){
    //Log number of notes playing
    if (note1){
        count++;
      }
    if (note2){
        count++;
      }
    if (note3){
        count++;
      }
}


int updateAudio(){
    int result = 0;

    //Play notes and mix down (according to notes playing
    if (note1) {
        result += tone_1.next() * (1/count);
    }
    if (note2) {
        result += tone_2.next() * (1/count);
    }
    if (note3) {
        result += tone_3.next() * (1/count);
    }
  //int arev = reverb.next(result); /*Initialize reverb*/

  //return result + (arev>>3);    /*Reverb: Mix signal*/
  //return arev;                  /*Reverb: Wet signal*/
  //return result;                /*Reverb: Dry signal*/

    //if (!codeswitch){
    //polysynth
    return result;
    //}
    //else if (codeswitch) {
    //controlvoltage
    //return CV;
    //}

    
}

void loop(){
  //Messure LDR multiplex inputs
  int16_t adc0a, adc1a, adc2a, adc3a, adc0b, adc1b, adc2b, adc3b, adc0c, adc1c, adc2c, adc3c, adc0d, adc1d, adc2d, adc3d;

  adc0a = adsa.readADC_SingleEnded(0);
  adc1a = adsa.readADC_SingleEnded(1);
  adc2a = adsa.readADC_SingleEnded(2);
  adc3a = adsa.readADC_SingleEnded(3);
//  Serial.print("AIN0A: "); Serial.println(adc0a);
//  Serial.print("AIN1A: "); Serial.println(adc1a);
//  Serial.print("AIN2A: "); Serial.println(adc2a);
//  Serial.print("AIN3A: "); Serial.println(adc3a);
//  Serial.println(" ");

  adc0b = adsb.readADC_SingleEnded(0);
  adc1b = adsb.readADC_SingleEnded(1);
  adc2b = adsb.readADC_SingleEnded(2);
  adc3b = adsb.readADC_SingleEnded(3);
//  Serial.print("AIN0B: "); Serial.println(adc0b);
//  Serial.print("AIN1B: "); Serial.println(adc1b);
//  Serial.print("AIN2B: "); Serial.println(adc2b);
//  Serial.print("AIN3B: "); Serial.println(adc3b);
//  Serial.println(" ");

  adc0c = adsc.readADC_SingleEnded(0);
  adc1c = adsc.readADC_SingleEnded(1);
  adc2c = adsc.readADC_SingleEnded(2);
  adc3c = adsc.readADC_SingleEnded(3);
//  Serial.print("AIN0C: "); Serial.println(adc0c);
//  Serial.print("AIN1C: "); Serial.println(adc1c);
//  Serial.print("AIN2C: "); Serial.println(adc2c);
//  Serial.print("AIN3C: "); Serial.println(adc3c);
//  Serial.println(" ");

  adc0d = adsd.readADC_SingleEnded(0);
  adc1d = adsd.readADC_SingleEnded(1);
  adc2d = adsd.readADC_SingleEnded(2);
  adc3d = adsd.readADC_SingleEnded(3);
//  Serial.print("AIN0D: "); Serial.println(adc0d);
//  Serial.print("AIN1D: "); Serial.println(adc1d);
//  Serial.print("AIN2D: "); Serial.println(adc2d);
//  Serial.print("AIN3D: "); Serial.println(adc3d);
//  Serial.println(" ");
  
//  delay(1000);
  audioHook();


//Switch between auditive output and CV out
//    if (digitalRead (8) == LOW){
//        codeswitch = false;
//    }
//    if (digitalRead (8) == HIGH){
//        codeswitch = true;
//    }
    
    
}
