/*
    Voice Box Demo Sketch
    para el bootcamp 2019 Facultad de Ingeniería, Universidad Anáhuac
    SparkFun Electronics
  */


  //Soft serial library used to send serial commands on pin 2 instead of regular serial pin.
  #include <SoftwareSerial.h>

  //Define the Pin Numbers for the sketch.
  #define E0  5
  #define E1  6
  #define E2  7
  #define E3  8
  #define E4  9
  #define E5  10
  #define E6  11
  #define E7  12

  #define RDY  13
  #define RES  3
  #define SPK  4
  #define SPKO 22


  #define txPin  2

  //Create a SoftSerial Objet
  SoftwareSerial speakjet = SoftwareSerial(0, txPin);

  //The message array contains the command for sounds to be sent in order to inunciate the words "bienvenidos al bootcamp de la universidad Anahuac".

  const byte interruptPin = SPK;
  volatile byte state = LOW;

  //20 Volumen: 96 -default [0,127]
  //21 Velocidad: 114 - default 0 Más lento, 127 - Más rápido [0,127]
  //22 Pitch- tono- vocalización tonal en Hz: 88 -default [0,255]
  //23 Bend - Distorsión : 5 - default [0,15]

  char inicio[] = {20, 96, 21, 114, 22, 88, 23, 5 };

  //The sounds array contains the commands to send robot sounds to the SpeakJet chip.
  char sounds[] = {200, 201, 202, 203, 220, 221, 222, 0};



  //Diccionario de fonemas
  #define SLOW 8
  #define FAST 7
  #define SPACE 5
  #define _ 5

  #define RELAX 14
  #define STRESS 15

  //Bootcamp
  #define BU 171
  #define UH 138
  #define T 191  //192
  #define C 194
  #define AY 132
  #define M 140
  #define P 198

  #define B 172
  #define V 166
  #define I 128
  #define E 131
  #define N 141
  #define D 175
  #define O 137

  #define A 135
  #define L 145

  #define S 187
  #define U 139

  #define G 178
  #define R 148

  #define A1 133


  //Reproducción de Frases
  char bienvenido[] = { B,I,E,N,V,E,N,I,D,FAST,O,_,RELAX,RELAX,A,RELAX, RELAX, L, _, BU,SLOW,UH,T,C,AY,M,P, _, D,E,_,L,A,_,U,N,I,V,E,R,S,I,D,A,D,_,A,N,SLOW,A,FAST,U,A,C,_,0};

  char si[] = { _,S,I,_,0};
  char no[] = { _,N,O,_,0};
  char gracias[] = { _,G,STRESS,R,A,S,I,A,S,_,0};

   String palabra=""; // Almacena el palabra que se introduzca.


  char silence[] = {20,0};

  void setup()
  {
    Serial.begin(9600);

    //Configure the pins for the SpeakJet module
    pinMode(txPin, OUTPUT);
    pinMode(SPK, INPUT);
    //pinMode(SPK, INPUT_PULLUP);
    pinMode(SPKO, OUTPUT);

    //Set up a serial port to talk from Arduino to the SpeakJet module on pin 3.
    speakjet.begin(9600);

    //Configure the Ready pin as an input
    pinMode(RDY, INPUT);

    //Configure Reset line as an output
    //pinMode(RES, OUTPUT);

    pinMode(RES, INPUT);

    //Configure all of the Event pins as outputs from Arduino, and set them Low.
    for(int i=E0; i<=E7; i++)
    {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);
    }

    //All I/O pins are configured. Reset the SpeakJet module
    digitalWrite(RES, LOW);
    delay(100);
    digitalWrite(RES, HIGH);


    speakjet.print(bienvenido);
    speakjet.print(sounds);

  }

  void loop()
  {
    //int spk = map(analogRead(SPK),0,1024,0,255);
    //analogWrite(SPKO,spk);
    //Serial.println(spk);


    int value=0;




      // Revisamos si la computadora se ha comuncado con nuestro programa.
    while (Serial.available() > 0)
    {
      // Leemos y almacenamos el palabra introducido.
      palabra = Serial.readString();
      limpiapalabra(&palabra);

      speakjet.print(inicio);



      // Revisamos si la palabra coincide con alguna frase.
      // ____________________________________________________________________________________________________________________
      if (palabra.equalsIgnoreCase("si"))
      {
         speakjet.print(si);
      }
      else if (palabra.equalsIgnoreCase("no"))
      {
        speakjet.print(no);
      }
      else if (palabra.equalsIgnoreCase("gracias"))
      {
        speakjet.print(gracias);
      }
      else
      {
        Serial.println("Palabra desconocida");
      }

     Serial.println(palabra);

    }

    //Wait before sending the next string.
    delay(500);



    //speakjet.print(silence);
  }

  void limpiapalabra(String * texto)
  {
    texto->trim();
    texto->replace('\n', '\0');
    texto->replace('\r', '\0');
  }
//Siguiente explicación: Carga (upload) y ejecución del programa en Arduino