/*
  Simple Waveform generator with Arduino Due
  * connect two push buttons to the digital pins 2 and 3
    with a 10 kilohm pulldown resistor to choose the waveform
    to send to the DAC0 and DAC1 channels
  * connect a 10 kilohm potentiometer to A0 to control the
    signal frequency
 */
#include "Waveform.h"
#define oneHzSample 1000000/maxSamplesNum  // sample for the 1Hz signal expressed in microseconds
const int button0 = 12, button1 = 14;
const int ledPin  = 5;  // 5 corresponds to GPIO5
volatile int wave0 = 0, wave1 = 0;
int i = 0;
int sample;
int dacValue;
void setup() {
  Serial.begin(115200);
  attachInterrupt(button0, wave0Select, RISING);  // Interrupt attached to the button connected to pin 2
  attachInterrupt(button1, wave1Select, RISING);  // Interrupt attached to the button connected to pin 3
}
void loop() {
  // Read the the potentiometer and map the value  between the maximum and the minimum sample available
  // 1 Hz is the minimum freq for the complete wave
  // 170 Hz is the maximum freq for the complete wave. Measured considering the loop and the analogRead() time
  sample = map(analogRead(4), 0, 4095, 0, oneHzSample);
  sample = constrain(sample, 0, oneHzSample);
//  Serial.print("Sample: ");
//  Serial.println(sample);
  dacValue = map(waveformsTable[wave0][i], 0, 4095, 0, 255);
  dacWrite(25, dacValue);  // write the selected waveform on DAC0
  Serial.print("waveformsTable[wave0][i]): ");
  Serial.println(dacValue);
  i++;
  if(i == maxSamplesNum)  // Reset the counter to repeat the wave
    i = 0;
  delayMicroseconds(sample);  // Hold the sample value for the sample time
}
// function hooked to the interrupt on digital pin 2
void wave0Select() {
  wave0++;
  if(wave0 >= 4)
    wave0 = 0;
}
// function hooked to the interrupt on digital pin 3
void wave1Select() {
  wave1++;
  if(wave1 >= 4)
    wave1 = 0;
}