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... ![]()
- 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?
- 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!