# 1 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino"
# 2 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 3 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 4 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 5 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 6 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 7 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 8 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 9 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 10 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2
# 11 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 2


void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
  Serial.print("NoteOn ");
  Serial.print("Channel:  ");
  Serial.print(channel);
  Serial.print(", Pitch:  ");
  Serial.print(pitch,16);
  Serial.print(", Velocity:  ");
  Serial.print(velocity);
  Serial.print(", LED number:  ");
  Serial.println(pitch-0x30);

  counter++;

  digitalWrite(myPins[({ typeof (pitch-0x30) _x = (pitch-0x30); typeof (0) _l = (0); typeof (sizeof(myPins)/2 - 1) _h = (sizeof(myPins)/2 - 1); _x < _l ? _l : _x > _h ? _h : _x; })],0x1);

  analogWrite(12, (velocity*2));

  if(Inst == 0) previousMillis = currentMillis;
}

void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
  Serial.print("NoteOff ");
  Serial.print("Channel:  ");
  Serial.print(channel);
  Serial.print(", Pitch:  ");
  Serial.print(pitch,16);
  Serial.print(", Velocity:  ");
  Serial.println(velocity,16);

  counter--;

  digitalWrite(myPins[({ typeof (pitch-0x30) _x = (pitch-0x30); typeof (0) _l = (0); typeof (sizeof(myPins)/2 - 1) _h = (sizeof(myPins)/2 - 1); _x < _l ? _l : _x > _h ? _h : _x; })],0x0);
  if(counter == 0) analogWrite(12, 0);
}

void setup() {
  MyMIDI.begin(MIDIChannel);
  MyMIDI.setHandleNoteOn(MyHandleNoteOn);
  MyMIDI.setHandleNoteOff(MyHandleNoteOff);

  r.begin();

  Serial.begin(115200);

  if(!display.begin(0x02 /*|< Gen. display voltage from 3.3V*/, 0x3C /*|< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32*/)) {
    Serial.println((reinterpret_cast<const __FlashStringHelper *>(
# 58 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 3
                  (__extension__({static const char __c[] __attribute__((__progmem__)) = (
# 58 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino"
                  "SSD1306 allocation failed"
# 58 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino" 3
                  ); &__c[0];}))
# 58 "/Users/martinmusinka/Documents/Skola/Diplomova_Praca/Program/MidiSequencer/MidiSequencer.ino"
                  )));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.display();

  pinMode(3, 0x1);
  digitalWrite(3, 0x1);

  analogWrite(12, 0);

  for (int i = 0; i < sizeof(myPins)/2; i = i + 1) {
    pinMode(myPins[i], 0x1);
  }

  displayPrint("MIDI", 'C', 0, 2, 'W');
  displayPrint("SEQUENCER", 'C', 1, 2, 'W');
  displayPrint("by Martin Musinka", 'C', 6, 1, 'W');
  display.display();

  delay(5000);

  display.clearDisplay();
  displayPrint("LEDs", 'L', 0, 2, 'W');
  displayPrint("On", 'C', 2, 2, 'W');
  display.display();
}

void loop() {
  MyMIDI.read();
  StateMachine();
}
