 */
 void setup() {
 
-  MIDI.begin(MIDI_CHANNEL_OMNI);
+   MIDI.begin();
+   MIDI.turnThruOff();
+   MIDI.setHandleSystemExclusive(handleSysEx);
 
   // MIDI callback system for handling input events.
   //MIDI.setHandleNoteOn(HandleNoteOn);
   //MIDI.setHandleControlChange(HandleCC);
   //MIDI.setHandleNoteOff(HandleNoteOff);
-  //MIDI.setHandleSystemExclusive(HandleSysEx);
-
-  Serial.begin(9600);
+
+  //Serial.begin(9600);
 
   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)

#include <SPI.h>
#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MIDI.h>

//Oled related
#define OLED_RESET 4

  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("MIDI CC");
  display.display();
}


/**
   MIDI SysEx
*/
void handleSysEx(byte * sysEx, unsigned buffSize) {
  //header f0 00 00 66 05 00 
  //       
  display.clearDisplay();


  if(sysEx[5] == 0x12){
    handleScribble(sysEx, buffSize);
  }
 

  /*
  if(sysEx[5] == 0x20){
    // <hdr> 20 05 00 <eof>
    handleMetering(sysEx, buffSize);
  }
  */
  


  display.display();
}


/**
 * 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){
      // Get position on the display
      if(offset>=0 && offset <= 37){
          //first line 
          display.setTextSize(2); 
          display.setCursor(0,0);
      }
      else{ //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
      }   
    }
}


void handleMetering(byte *sysEx, unsigned buffSize){
    //TODO
    byte offset = sysEx[6];
    // Start to get the display to update (0->7)
    int displayId = getDisplayId(sysEx);
     display.setTextSize(2); 
     display.setCursor(0,0);
    
     display.write(sysEx[7]); //write to convert Byte as char
     display.write(sysEx[8]); //write to convert Byte as char
}


/**
 * Get Display id
 */
int getDisplayId(byte *sysEx){
    int offset = (sysEx[6] / 7) ; // 7char per display
    return offset % 8 ; // only 8 displays available
}









