    MAIN loop, mostly reading MIDI messages
 */
 void loop() {
-  // Reading MIDI messages, 
-  
+  // Reading MIDI messages,
+
   MIDI.read();
   
-
+  byte data = MIDI.getData1();
+  
+  if(MIDI.getType() == 0xD0){ //metering?
+    if(data != 0){
+      handleMetering(data);
+    }   
+  }
+  
 }
 
 

#endif

//MIDI buffer
byte buffer[10];


/**
   Board Setup
*/
void setup() {

   MIDI.begin();
   MIDI.turnThruOff();
   MIDI.setHandleSystemExclusive(handleSysEx);

  // MIDI callback system for handling input events.
  //MIDI.setHandleNoteOn(HandleNoteOn);

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

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

}


/**
   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();
}


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









