/* Codigo por Roy Oroza */

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

const int numBotones = 4;
const int calibracion = 130;
const int numPotenciometros = 1;

byte ccs[] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35};
byte pinesBotones[] = {12, 13, 14, 15};
byte pinesLeds[] = {16, 17, 18, 19};
byte pinesPotenciometros[] = {34};

boolean notaApagada[numBotones];
int contador[numBotones];
boolean estadoLed[numBotones];
int lecturas[numPotenciometros]; 
int lecturasAnteriores[numPotenciometros];

BluetoothSerial SerialBT;

const int numPaginas = 4;
int paginaActual = 1;
int contadorBoton1 = 0;
int contadorBoton4 = 0;

void setup() {
    Serial.begin(115200);
    SerialBT.begin("FS-MIDI-Bluetooth");
    Serial.println("The device started, now you can pair it with bluetooth!");  
  
    for (int i=0; i<numBotones; i++) {
        pinMode(pinesBotones[i], INPUT_PULLUP);
        pinMode(pinesLeds[i], OUTPUT);
    }
    analogReadResolution(10);
}

void midi(unsigned char command, unsigned char note,unsigned char vel) {
    SerialBT.write(command);
    SerialBT.write(note);
    SerialBT.write(vel);
}

void loop() {
    if (digitalRead(pinesBotones[0]) == LOW) {
        contadorBoton1++;
        if (contadorBoton1 >= 2000) {
            paginaActual--;
            if (paginaActual < 1) {
                paginaActual = numPaginas;
            }
            contadorBoton1 = 0;
        }
    } else {
        contadorBoton1 = 0;
    }

    if (digitalRead(pinesBotones[3]) == LOW) {
        contadorBoton4++;
        if (contadorBoton4 >= 2000) {
            paginaActual++;
            if (paginaActual > numPaginas) {
                paginaActual = 1;
            }
            contadorBoton4 = 0;
        }
    } else {
        contadorBoton4 = 0;
    }

    for (int i=0; i<numBotones; i++) {
        if (digitalRead(pinesBotones[i]) == LOW) {
            if (contador[i]==0) {
                byte cc = ccs[i] + (paginaActual - 1) * numBotones;
                if (notaApagada[i]== 1) {
                    contador[i]=calibracion;
                    midi(176, cc, 0);
                    notaApagada[i] = 0;
                    estadoLed[i] = !estadoLed[i];
                    digitalWrite(pinesLeds[i], estadoLed[i]);
                } else {
                    contador[i]=calibracion;
                    midi(176, cc, 127);
                    notaApagada[i] = 1;
                    estadoLed[i] = !estadoLed[i];
                    digitalWrite(pinesLeds[i], estadoLed[i]);
                }
            }
        } else {
            if (contador[i]>0) contador[i]--;
        }
    }

    for (int k=0; k<numPotenciometros; k++) {
        int lec = analogRead(pinesPotenciometros[k]);
        lecturas[k] = map(lec,0,1023,0,255);
    }

    for (int k=0; k<numPotenciometros; k++) {
        if (lecturas[k] > (lecturasAnteriores[k]+1) || lecturas[k] < (lecturasAnteriores[k]-1)) {                   
            midi(176,k+50,lecturas[k]/2);
            lecturasAnteriores[k] = lecturas[k];
        } 
    }
    delay(1);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
led4:A
led4:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
pot1:GND
pot1:SIG
pot1:VCC