 #include <SPI.h>
 #include <Wire.h>
 #include <stdio.h>
+
+#include <string.h>
+#include <stdlib.h>
 
 
 

   LCD message should be processed here
*/
void handleScribble(byte *sysEx, unsigned buffSize) {
  byte offset = sysEx[6];
  // Start to get the display to update (0->7)
  int displayId = getDisplayId(sysEx);


  if (displayId == 0) {
    display.clearDisplay();
    // Get position on the display
    if (offset >= 0x00 && offset <= 0x37) {
      //first line
      display.setTextSize(2);
      display.setCursor(0, 0);
    }
    else if (offset <= 0x6C) { //38->6C second line
      //second line
      display.setTextSize(2);
      display.setCursor(0, CH_HEI * 2);
      display.setCursor(DP_WID_MID - (3 * CH_WID * 2), display.getCursorY()); // Center text on OLED

    }

    //Display the message
    for (int16_t i = 7; i < buffSize - 1; i ++) {
      display.write(sysEx[i]); //write to convert Byte as char
    }
    display.display();
  }
}


void handleMetering(byte data) {
      int fullScale = DP_WID;

      byte chan = data & 0xf0; // from 0 to 7
      //TODO handle multiple displays
      
      byte level = data & 0x0f; // 0-> 0% , C-> 100%
      int meterVal = (level * fullScale) / 0x0C;
      
      display.clearDisplay();
      display.setCursor(0,0);
      
      for (int16_t i = 0; i < meterVal; i += 1) {
          display.drawLine(display.getCursorX() + i, display.getCursorY(), display.getCursorX() + i, display.getCursorY() + CH_HEI, WHITE);
      }

      // Set Overload
      if(level == 0x0E){
        overload = true;
      }
      // unset Overload
      if(level == 0x0F){
         overload = false;
      }
      if(overload){
        display.setTextSize(1);
        display.println(" "); 
        display.print("OVERLOAD!!!!!"); //set overload
      }
      
      delay(50);  
      display.display();
}



  // Do something here with your data!
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(pitch);
  display.display();
}

/**
   MIDI note off
*/
void HandleNoteOff(byte channel, byte pitch, byte velocity)
{
  // Do something here with your data!
}

/**
   MIDI SysEx
*/
void handleSysEx(byte * sysEx, unsigned buffSize) {  
/*
  //Opening communication
  if (sysEx[5] == 0x00) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Connection Query...");
    display.display();
    //                                        <HDR>                | Host query
    byte sysExConnectionQuery[18] = { 0xF0, 0x00, 0x00, 0x66, 0x00, 0x01, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x01, 0x01, 0x01, 0x01, 0xF7 };
    sysExConnectionQuery[4] = sysEx[4];
    MIDI.sendSysEx(18, sysExConnectionQuery, false);

    delay(500);

  }
  if (sysEx[5] == 0x02) {
    //skip protocol verifications...
    //display.clearDisplay();
    //display.setCursor(0, 0);
    display.println("Confirmed");
    display.display();
    //                                        <HDR>                | Host confirm
    byte sysExConnectionConfirm[] = { 0xf0, 0x00, 0x00, 0x66, 0x14, 0x03, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0xF7 };
    MIDI.sendSysEx(14, sysExConnectionConfirm, false);
  }
*/
  //if(sysEx[0] == 0xF0){
    if (sysEx[5] == 0x12) {
      handleScribble(sysEx, buffSize);
    }
  
    refreshLCD();
  //}
}


