Daisy Seed Midi

#DSP #eurorack🎛️

First, you need to initialize the MIDI handler in your setup() function:

void setup()
{
    // Initialize Daisy Seed
    hw.Init();

    // Initialize MIDI handler
    MidiUartHandler::Config midi_config;
    midi_config.baudrate = 31250;
    midi.Init(midi_config);
}

In this example, we're using the UART MIDI handler, which is suitable for DIN MIDI connections. We're setting the baud rate to 31250, which is the standard baud rate for MIDI.

Next, you can create a function to send MIDI messages:

void sendMidiMessage(uint8_t status, uint8_t data1, uint8_t data2)
{
    uint8_t message[3];
    message[0] = status;
    message[1] = data1;
    message[2] = data2;
    midi.SendMessage(message, 3);
}

This function takes three arguments: the status byte, the first data byte, and the second data byte. It creates a message array with these three bytes and sends it using the midi.SendMessage() function.

Now, you can use this function to send MIDI messages. For example, to send a note on message for C4 with maximum velocity on channel 1, you can call:

sendMidiMessage(0x90, 60, 127);

This will send the following bytes:

10x90 0x60 0x7F

which corresponds to the status byte for a note on message (0x90), the pitch byte for C4 (0x60), and the velocity byte for maximum velocity (0x7F).

You can also listen for incoming MIDI messages using the Listen() function and the HasEvents() function:

void loop()
{
    // Listen for incoming MIDI messages
    hw.midi.Listen();

    // Handle incoming MIDI messages
    while (hw.midi.HasEvents())
    {
        MidiEvent event = hw.midi.PopEvent();

        // Handle the event
        switch (event.type)
        {
            case MidiEvent::Type::NoteOn:
                // Handle note on message
                break;
            case MidiEvent::Type::NoteOff:
                // Handle note off message
                break;
            case MidiEvent::Type::ControlChange:
                // Handle control change message
                break;
            // Add more cases for other message types
        }
    }

    // Do other things in your loop
}

This code listens for incoming MIDI messages using the Listen() function and handles them using the HasEvents() function. It then pops the next event from the queue using the PopEvent() function and handles it based on its type.

Note that you can also use the MidiEvent class to parse incoming MIDI messages and extract their data. For example, to extract the pitch and velocity from a note on message, you can do:

case MidiEvent::Type::NoteOn:
{
    uint8_t pitch = event.data.note_on.pitch;
    uint8_t velocity = event.data.note_on.velocity;
    // Handle the note on message
    break;
}

This code extracts the pitch and velocity from a note on message and stores them in variables. You can then use these variables to control your synthesizer or other MIDI-controlled device.