//Cosmic Pi histogram program
//this compiles histograms from the ADC for different HV Bias levels
//it is used to profile the detection of muons and noise

unsigned long start_time;
unsigned long stop_time;
unsigned int values[2000];
unsigned long histograma[4096];
unsigned long histogramb[4096];
const int SS_pin = 42; //tbc
const int SCK_pin = 44;
const int MISO_pin = 22;
const int MOSI_pin = 43;

String intextbuf;
String extextbuf;
byte sendValue = 0xFF;   // Value we are going to send
byte returnValue = 0;  // Where we will store the value sent by the slave
int cyclecounter = 0;
int hvset = 0;

void setup() {

  digitalWrite(SS, HIGH);  // Start with SS high
  pinMode(SS_pin, OUTPUT);
  pinMode(SCK_pin, OUTPUT);
  pinMode(MISO_pin, INPUT); //this is the avalanche pin, not implemented yet
  pinMode(MOSI_pin, OUTPUT);

  Serial.begin(115200);

  REG_ADC_MR = 0x10380080;  // Free run as fast as you can
  REG_ADC_CHER = 3;   // Channels 0 and 1
  REG_ADC_CR = 2;     // Start
  Serial.print("Alive");
  sendValue = 0x60;
  Serial.println("Histogram for channels A and B, voltage scan");
  Serial.println("1000 samples per round");
  Serial.println("100,000 sets of samples");
  Serial.println("100,000,000 samples per output");
  returnValue = bitBang(sendValue); // Transmit data
  for (int i = 0; i < 4096; i++) {
    histograma[i] = 0;
    histogramb[i] = 0;
  }

  Serial.print("hvset");
  Serial.print("; ");
  Serial.print("sample");
  Serial.print("; ");
  Serial.print("channel a");
  Serial.print("; ");
  Serial.println("channel b");


}

void loop() {
  //reads in on channel 0

  for (hvset = 0x70; hvset > 0x50; hvset--) {
    returnValue = bitBang(hvset); // Transmit data

    for (int j = 0; j < 100000; j++) {

      for (int i = 0; i < 1000; i++) {
        while ((ADC->ADC_ISR & 0x01) == 0);; // wait for conversion
        values[i] = ADC->ADC_CDR[0]; //reads ch 0 only
        while ((ADC->ADC_ISR & 0x02) == 0);
        values[i + 1000] = ADC->ADC_CDR[1]; //get values
      }

      for (int i = 0; i < 1000; i++) {

        //Serial.println(i);
        //Serial.print(values[i]);
        histograma[int(values[i])]++;
        //Serial.print(" ");
        //Serial.println(values[i+1000]);
        histogramb[int(values[i + 1000])]++;
      }
    }
    for (int i = 0; i < 4096; i++) {

      Serial.print(hvset);
      Serial.print("; ");
      Serial.print(i);
      Serial.print("; ");
      Serial.print(histograma[i]);
      Serial.print("; ");
      Serial.println(histogramb[i]);
    }
    for (int i = 0; i < 4096; i++) {
      histograma[i] = 0;
      histogramb[i] = 0;
    }
    //Serial.println(cyclecounter);

    //  delay(2000);
    //  cyclecounter++;
    //  for(int i=0;i<4096;i++) {
    //  Serial.print(i);
    //  Serial.print(" ");
    //  Serial.print(histograma[i]);
    //  Serial.print(" ");
    //  Serial.println(histogramb[i]);
    //histogramb[i]=0;
  }
}



byte bitBang(byte _send)  // This function is what bitbangs the data
{

  //reception isn't implemented in this version.
  byte _receive = 0;
  digitalWrite(SS_pin, LOW);        // SS low
  for (int i = 0; i < 8; i++) // There are 8 bits in a byte
  {
    digitalWrite(MOSI_pin, bitRead(_send, 7 - i));  // Set MOSI
    //delay(1);
    digitalWrite(SCK_pin, HIGH);                  // SCK high
    //bitWrite(_receive, i, digitalRead(MISO_pin)); // Capture MISO
    digitalWrite(SCK_pin, LOW);                   // SCK low
    //digitalWrite(MOSI_pin, LOW);    // Set MOSI

  }
  digitalWrite(SS_pin, HIGH);       // SS high again

  //return _receive;        // Return the received data
}