I have several projects that use usbMIDI.read() and I see both versions in code online, reading out messages in an if statement and in a while loop.. for example:
when is it "better" to change the while to an if statement, or is the while always preferred since it makes sure the buffer gets empty before proceeding? My gut feeling tells me this could stall the rest of my code, so an if (usbMIDI.read()) { //insert code here...} might be preferred if i need the rest of my code to execute every loop on a regular basis. I would be glad for any insights on this..
also is the same true when reading from usb clients in a usbhost situation on the teensy or is while or if preferred in that scenario? for example:
Code:
while(usbMIDI.read()) {
uint8_t type = usbMIDI.getType();
uint8_t data1 = usbMIDI.getData1();
uint8_t data2 = usbMIDI.getData2();
uint8_t channel = usbMIDI.getChannel();
uint8_t cable = usbMIDI.getCable();
if (midibass >= 0){
midilist[midibass]->send(type, data1, data2, channel, cable);
}
/* if (tooro >= 0) {
midilist[tooro]->send(type, data1, data2, channel, cable);
} */
if (type == 0xB0) {
int dmxChannel = (channel -1)*128 + data1;
led_channel(dmxChannel, data2*2);
//update dmx--------------------------------------
// leds_update();
}
}
when is it "better" to change the while to an if statement, or is the while always preferred since it makes sure the buffer gets empty before proceeding? My gut feeling tells me this could stall the rest of my code, so an if (usbMIDI.read()) { //insert code here...} might be preferred if i need the rest of my code to execute every loop on a regular basis. I would be glad for any insights on this..
also is the same true when reading from usb clients in a usbhost situation on the teensy or is while or if preferred in that scenario? for example:
Code:
while (midilist[midibass]->read()) {
uint8_t type = midilist[midibass]->getType();
uint8_t data1 = midilist[midibass]->getData1();
uint8_t data2 = midilist[midibass]->getData2();
uint8_t channel = midilist[midibass]->getChannel();
uint8_t cable = midilist[midibass]->getCable();
//do something here