Trying to understand 2 lines of Midi_dump_example

Hello,

busy here with trying to control my guitar effect (Nux MG-30) which is working now but on some lines in the code I do not understand the 'why' and what is being 'said'.

So everything is working, no problem, I just like to understand it more so may-be in future I can adjust things if needed.

This is the code, it's an standard example than comes with USB-Host shield 2.0 library and it dumps the midi content being received.

/*
 *******************************************************************************
 * USB-MIDI dump utility
 * Copyright (C) 2013-2021 Yuuichi Akagawa
 *
 * for use with USB Host Shield 2.0 from Circuitsathome.com
 * https://github.com/felis/USB_Host_Shield_2.0
 *
 * This is sample program. Do not expect perfect behavior.
 *******************************************************************************
 */

#include <usbh_midi.h>
#include <usbhub.h>

USB Usb;
USBHub Hub(&Usb);
USBH_MIDI  Midi(&Usb);

void MIDI_poll();

void onInit()
{
  char buf[20];
  uint16_t vid = Midi.idVendor();
  uint16_t pid = Midi.idProduct();
  sprintf(buf, "VID:%04X, PID:%04X", vid, pid);
  Serial.println(buf); 
}

void setup()
{
  Serial.begin(115200);

  if (Usb.Init() == -1) {
    while (1); //halt
  }//if (Usb.Init() == -1...
  delay( 200 );

  // Register onInit() function
  Midi.attachOnInit(onInit);
}

void loop()
{
  Usb.Task();
  if ( Midi ) {
    MIDI_poll();
  }
}

// Poll USB MIDI Controler and send to serial MIDI
void MIDI_poll()
{
  char buf[16];
  uint8_t bufMidi[MIDI_EVENT_PACKET_SIZE];
  uint16_t  rcvd;

  if (Midi.RecvData( &rcvd,  bufMidi) == 0 ) {
    uint32_t time = (uint32_t)millis();
    sprintf(buf, "%04X%04X:%3d:", (uint16_t)(time >> 16), (uint16_t)(time & 0xFFFF), rcvd); // Split variable to prevent warnings on the ESP8266 platform
    Serial.print(buf);

    for (int i = 0; i < MIDI_EVENT_PACKET_SIZE; i++) {
      sprintf(buf, " %02X", bufMidi[i]);
      Serial.print(buf);
    }
    Serial.println("");
  }
}

Now I have 2 questions at the moment I do not understand (well more..but I'll limit it as these are complicated enough I think... :slight_smile:

  1. almost at the top there is: void MIDI_poll();

I dont get it:
either (in my opinion) it is void <function_name> () { }

so with the void command you want to declare a function for later use/call (like void onInit())

or you write the code without the void to call a function but then you would write just MIDI_poll(); to call MIDI_poll....

so why is it written like it is and why there?

  1. now this is really abracadabra to me:

if (Midi.RecvData( &rcvd, bufMidi) == 0 ) {

what I do understand that...if the result of something I do not understand ==0 that's good and you can go continue with the received midi data.

but what is (&rcvd, bufMidi) doing?

I know/read that in the variable rcvd are the numbers of received bytes and bufMidi is an array[] of the received lenght of bytes.

But what is this logical AND doing and why ==0 because we are receiving bytes so I would say it should not be empty because we received some bytes....

Very difficult for me to understand and I could get away with it just thinking ...mm, it's working, why bother, but as said, like to understand more,

Thanks in advance for your answer!

1 Like

This is a function prototype q.v.

void MIDI_poll();

The IDE adds them for you behind the scenes but apparently the author was being thorough.

This:

if (Midi.RecvData( &rcvd, bufMidi) == 0 ) {

Is call to a function that takes a pointer to the number of bytes received and a pointer to the buffer they were put in. The & gets the address of the rcvd variable, for the buffer, it's not needed because bufMidi is an address already.

I'll guess that the function returns zero if everything was ok.

1 Like

This is a forward declaration. It informs the compiler that somewhere in the code there is a function by that name. It prevents any references to the function call that might occur before the function actually appears in the code, from throwing an 'undefined' error during parsing. The Arduino IDE tries to make things easy and takes care of that problem so forward declarations are not required in .ino sketches when using the Arduino IDE, but may be required in other environments such as PlatformIO or Eclipse. The actual function code appears further down in the sketch.

As you have surmised, this is calling a function, and if the result of executing that function equals 0 it will execute the code within the if clause.

In this context, the '&' character is not a logical AND, but denotes 'address of'. At first I thought that rcvd might be a struct of some sort or an array, but its just an int16. Passing its address probably allows the function to write some value back into it. The comparison with zero might be to determine a return state indicating that no errors occurred, however that is only speculation. We would need to see the function code to fully understand the reason for the variable declarations in the function call as well as an explanation of its return value.

1 Like

Thank you for your replies.

With this knowledge I '//' the void MIDI_poll(); and it's working as good so good to know where it's for and that it's not needed.

Pointers..I'll look that up now I know that this is the case in that &rcvd statement

Thanks!

The Arduino "magic" only works in .ino files. If you put this code in a .cpp file (like in a library) you would need that prototype because loop() calls MIDI_poll() but MIDI_poll() is not declared until after loop().

1 Like

Good point (as I just wrote my first library (not needed there but i could pull my hair out why it wouldn't work in that case...))

May-be therefore in the code above the function OnInit is placed where it is: before it's being used in the setup as attach.

I did wonder a little why this function was placed there as normally I put all my function at the end, after the main loop() but didn't question it before because well...where ever you want to put them is fine....but now I know...

thanks :slight_smile: (and I have a lot to learn......)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.