How does usbMIDI.read() works?

Status
Not open for further replies.

Dinera

Member
Hi,

I try to use some (lets say 3) rotary encoders with the Teensy3.2 as an midi device.

The REs work fine on my Teensy and I can controll my DAW. Furthermore, I want to send the changed value, which I've alter in the software by mouse, back to the RE.
I've solved for one RE by using
Code:
value = usbMIDI.getData2();
myEnc.write(value);

But how do I use it for more than one value/RE?

In the documentation (https://www.pjrc.com/teensy/td_midi.html) is only the "usbMIDI.getChannel()" but I want to use the 1 with different controllers (e.g. 21,22,23).

1. Basically, which kind of data is comming from getData1 and getData2?
2. What is the differend between both getData?
3. Does usbMIDI.getType() give me also the controller which has the "usbMIDI.ControlChange"? If yes, how do I acces the data? If no, how do I get the number of the controller which has change in the software?
4. Will it be better to use another library like https://github.com/joshnishikawa/MIDIcontroller ?

Many thanks!
 
1) Standard MIDI is three bytes.
The status byte, which tells the type and channel, as well as two data bytes. For CC messages D1 is the CC number and D2 is the value.
2) in general D1 is the identifier and D2 the value. Note D1 / velocity D2 for example.
3) getType returns the constant named usbMIDI.ControlChange when the message being processed by the most recent .read() is a control change.

So you
read()
check that getType()==usbMIDI.ControlChange
... and then you use D1 to see which CC and D2 to get its value just as you have in your code fragment.
 
Hi oddson,

Thank you for your reply!

If I've understood the code shoud be like the following, isn't it?

Code:
while (usbMIDI.read()) {
  if (usbMIDI.getType()==usbMIDI.ControlChange) {
    int cc = usbMIDI.getData1();
    int val = usbMIDI.getData2();
    switch (cc) {
      case 21:
        serial.print("knob 1 feedback: ");
        serial.print(val);
      break;
      case 22:
        serial.print("knob 2 feedback: ");
        serial.print(val);
      break;
      case 23:
        serial.print("knob 3 feedback: ");
        serial.print(val);
      break;
      default:
        serial.print("can't match controller number: ");
        serial.print(cc);
      break;  
    }
    serial.println();
  }
}
 
You also can have a callback in your setup() for every cc message as

Code:
usbMIDI.setHandleControlChange(handle_cc);

Then declare the callback as

Code:
void handle_cc(byte channel, byte control, byte value) {
switch (control) {
      case 21:
        serial.print("knob 1 feedback: ");
        serial.print(value);
      break;
      case 22:
        serial.print("knob 2 feedback: ");
        serial.print(value);
      break;
      case 23:
        serial.print("knob 3 feedback: ");
        serial.print(value);
      break;
      default:
        serial.print("can't match controller number: ");
        serial.print(control);
      break;  
    }
    serial.println();
}

And you'll get the channel, the control and its value
 
Can I understand the "usbMIDI.setHandleControlChange(handle_cc);" as an Interrupt? Does everytime a controll change message is received, the "handle_cc" function will be executed?
 
I presume that the interrupt mechanism is handled behind the scene by the midi library and that handle_cc is not an interrupt routine, but yes every time a CC is received handle_cc is executed
 
But you have to put this statement in your loop() function

Code:
usbMIDI.read();

If you don't, handle_cc will never be called
 
Status
Not open for further replies.
Back
Top