#include "main.h"

void note_on_tx(byte channel, byte pitch, byte velocity)
{
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void note_off_tx(byte channel, byte pitch, byte velocity)
{
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void note_on_rx(byte channel, byte pitch, byte velocity)
{
  trellis.pixels.setPixelColor(map(pitch, 36, 51, 1, 16),
                               Wheel(map(velocity, 0, 127, 0, 255)));
  trellis.pixels.show();
}

void note_off_rx(byte channel, byte pitch, byte velocity)
{
  trellis.pixels.setPixelColor(map(pitch, 36, 51, 1, 16), 0);
  trellis.pixels.show();
}

void midi_header(midiEventPacket_t rx)
{
  switch (rx.header)
  {
  case 0:
    break; // No pending events

  case 0x9:
    note_on_rx(
        rx.byte1 & 0xF, // channel
        rx.byte2,       // pitch
        rx.byte3        // velocity
    );
    break;

  case 0x8:
    note_off_rx(
        rx.byte1 & 0xF, // channel
        rx.byte2,       // pitch
        rx.byte3        // velocity
    );
    break;

  default:
    Serial.print("Unhandled MIDI message: ");
    Serial.print(rx.header, HEX);
    Serial.print("-");
    Serial.print(rx.byte1, HEX);
    Serial.print("-");
    Serial.print(rx.byte2, HEX);
    Serial.print("-");
    Serial.println(rx.byte3, HEX);
  }
}