Interface_3x3 sample question

Status
Not open for further replies.

mark63

Active member
The sample code Interface_3x3 is a good sample.
But there is some i dont understand.
There are 3 midi instances created
Code:
// Create the Serial MIDI ports
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI2);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, MIDI3);

then we get :
Code:
 if (MIDI1.read()) {
    // get a MIDI IN1 (Serial) message
    byte type = MIDI1.getType();
    byte channel = MIDI1.getChannel();
    byte data1 = MIDI1.getData1();
    byte data2 = MIDI1.getData2();

    // forward the message to USB MIDI virtual cable #0
    if (type != midi::SystemExclusive) {
      // Normal messages, simply give the data to the usbMIDI.send()
      usbMIDI.send(type, data1, data2, channel, 0);
    } else {
      // SysEx messages are special.  The message length is given in data1 & data2
      unsigned int SysExLength = data1 + data2 * 256;
      usbMIDI.sendSysEx(SysExLength, MIDI1.getSysExArray(), true, 0);
    }
    activity = true;
  }

  if (MIDI2.read()) {
etc.

Now if i have 8 or 16 instances i would need to copy that code.
So i wonder why i can not pass this instance to a procedure.
Coming from pascal i have little knowledge about arduino. I am used to create an object and pass it(or a pointer).
I tried some things but all failed with cryptic error messages.

The Interface_16x16 sample shows an attempt : //midi::MidiInterface &SerialMidiList[6] = {MIDI1, MIDI2, MIDI3, MIDI4, MIDI5, MIDI6};
but it is remarked and not used. So it is either not simple, or not working.
Anyone an idea to get it work using a loop with a procedure/function?
 
The MIDI instances are macroexpanded from a set of template classes from what I can see, which means
the type is parameterized, as in midi::MidiInterface<midi::SerialMidi<HardwareSerial>>

Arrays can't contain references since references are immutable in C, so pointers are required.
So perhaps
Code:
midi::MidiInterface<midi::SerialMIDI<HardwareSerial>> * SerialMidiList[] = {&MIDI1, &MIDI2, &MIDI3, &MIDI4, &MIDI5, &MIDI6};
 
Now if i have 8 or 16 instances i would need to copy that code.
So i wonder why i can not pass this instance to a procedure.

Yes, it can be done with pointers. But if you're not already proficient with C pointer syntax, try to keep in mind whether it's turning into a lot more work than just making lots of copies and editing each slightly. You might also search for old threads (google with "site:forum.pjrc.com" if this forum's less-than-google-capable search doesn't help much). This exact question has come up many times from people building complicated MIDI projects. Some of those threads were pretty clear cases where just making many copies of the code would have been a lot less trouble! (though maybe worthwhile if your goal is also to learn C pointers.....)

For the examples to demonstrate how to use the library, generally we try to avoid complicated C / C++ syntax so you can focus on learning how to use the library.
 
@PaulStoffregen : i understand and appreciate the simple samples. but an example with complex syntax is no problem as long as there are some remarks to the code. And even without remarks, it is just a matter of using/changing. So i do not see a real problem there. I used many programming languages and i usually learn by checking examples.
And a pointer is just a pointer in any language right? it points to an address in memory of a variable or object. a pity that my simple wish seems so hard to accomplish with this lib. i will do some research/learning.
 
Status
Not open for further replies.
Back
Top