#include <OSCBundle.h>

// SERIAL
#ifdef BOARD_HAS_USB_SERIAL
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
#else
#include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial); // Change to Serial1 or Serial2 etc. for boards with multiple serial ports that don’t have Serial
#endif

OSCBundle bundle;

byte knofi[] = {
  A1,// levo (track)
  A0,// desno (track)
  2, // play / pause
  3, // overdub on/off
  4, // record arm on/off
  5, // 
  6, // dol (clip)
  7, // gor (clip)
  8, // quantize
  9, // duplicate
  10,// delete
  11,// record 4 beats
  12,// record 8 beats
  13,// record on/off
};
byte KNOFI_LEN = 14;
// Za stanje vrednosti
byte knofi_prej[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

// Kako dolgo pocakaj za dvoklik? (v milisekundah)
int dvoklik_interval = 250;

const char* kanali[16] = {
  "/track/previous",
  "/track/next",
  "/play",
  "/overdub",
  "/recarm",
  "",
  "/clip/next",
  "/clip/previous",
  "/quantize",
  "/duplicate",
  "/delete",
  "/clip/create/4",
  "/clip/create/8",
  "/clip/record" // rabi info o trenutno izbranem clipu / tracku
};


byte knofi_dvoklik[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void setup() {
  for(int i = 0; i < KNOFI_LEN; i++) {
    pinMode(knofi[i], INPUT_PULLUP);
  }

  //Serial.begin(9600);
  SLIPSerial.begin(115200);
}

byte vrednost;

void loop() {
  //Serial.println("Keys pressed:");
  for(int i = 0; i < KNOFI_LEN; i++) {
    vrednost = !digitalRead(knofi[i]);
    //Serial.print(vrednost);
    //Serial.print(", ");

    // Je gumb sveze vklopljen?
    if (!knofi_prej[i] && vrednost) {
      bundle.add(kanali[i]);
      //Serial.println(kanali[i]);
    }

    knofi_prej[i] = vrednost;
  }
  //Serial.println("");
  SLIPSerial.beginPacket();
  bundle.send(SLIPSerial);
  SLIPSerial.endPacket();
  //Serial.println("");
  
  bundle.empty();
}
