MIDI read note best practice in MbedOS 6 NUCLEO-L476RG

Hi
I managed to play MIDI using fun example
https://github.com/ARMmbed/mbed-os-snippet-USBMIDI_Take_Me_Out

My goal is a converter of MIDI notes to CV(Control Voltage)
Hence, I must read and parse MIDI message

I reused and modified the Cookbook code,
https://os.mbed.com/users/simon/code/USBMIDI_DrumExample/docs/040c2c8ca5eb/main_8cpp_source.html

My code looks like this:

// read midi and send CV
#include "mbed.h"
#include "USBMIDI.h"

DigitalOut led(LED1);
USBMIDI midi;
MIDIMessage msg;      

void cv(int state){
    led = state;
}

static void do_message(void) {
     if(midi.read(&msg)){
       switch (msg.type()) {
            case MIDIMessage::NoteOnType:
                cv(1);
                break;
            case MIDIMessage::NoteOffType:
                cv(0);
                break;
        }  
    }
}

int main() {    
    midi.attach(do_message); 
    cv(0);
    while (1);
}

I had to use external variable msg, which is not elegant.
The code works and blinks LED1 when piano key is pressed!!

But, when I tried to midi.attach(do_message) with msg like this do_message(MIDIMessage msg){…} as in Cookbook example, I got multiple error messages. I got rid of them using do_message(void)

How should I call midi.attach() to be compliant with best practices?
Tom

Hello again,

this is because old library is different then the latest one.

the old oneWhen you check
USBMIDI - A library to send and receive MIDI messages over … | Mbed

void attach(void (*fptr)(MIDIMessage));

and in new one
USBMIDI - API references and tutorials | Mbed OS 6 Documentation

void attach(mbed::Callback< void()>callback)	

You probably need to cover this all with your own class.

BTW your first link is broken, it runs import into KeilStudio :slight_smile:

BR, Jan

1 Like

Thanks a lot now I can move on.
I’m planning a class to catch sustain pedal state, multiple note values and velocity etc. in nice structure :slight_smile:
I will keep reporting on my progress.
Tom

2 Likes