Adding delays to serial Midi messages

Status
Not open for further replies.

sixeight

Well-known member
I have quite successfully built and programmed a Midi foot controller for several Roland devices. But at times the device will miss part of a message. To get successfully communication, I have to add delays in between messages to these devices, like this:

Code:
#define GR55_SYSEX_DELAY_LENGTH 10 // time between sysex messages (in msec)
unsigned long GR55sysexDelay = 0;

void GR55_check_sysex_delay() { // Will delay if last message was within GR55_SYSEX_DELAY_LENGTH (10 ms)
  while (millis() - GR55sysexDelay <= GR55_SYSEX_DELAY_LENGTH) {}
  GR55sysexDelay = millis();
}

void write_GR55(uint32_t address, uint8_t value) { // For sending one data byte

  uint8_t *ad = (uint8_t*)&address; //Split the 32-bit address into four bytes: ad[3], ad[2], ad[1] and ad[0]
  uint8_t checksum = MIDI_calc_Roland_checksum(ad[3] + ad[2] + ad[1] + ad[0] + value); // Calculate the Roland checksum
  uint8_t sysexmessage[14] = {0xF0, 0x41, GR55_device_id, 0x00, 0x00, 0x53, 0x12, ad[3], ad[2], ad[1], ad[0], value, checksum, 0xF7};
  GR55_check_sysex_delay();
  if (GR55_MIDI_port == USBMIDI_PORT) usbMIDI.sendSysEx(14, sysexmessage);
  if (GR55_MIDI_port == MIDI1_PORT) MIDI1.sendSysEx(13, sysexmessage);
  if (GR55_MIDI_port == MIDI2_PORT) MIDI2.sendSysEx(13, sysexmessage);
  if (GR55_MIDI_port == MIDI3_PORT) MIDI3.sendSysEx(13, sysexmessage);
  MIDI_debug_sysex(sysexmessage, 14, "out(GR55)");
}

void GR55_unmute() {
  //GR55_select_LED = GR55_PATCH_COLOUR; //Switch the LED on
  write_GR55(GR55_SYNTH1_SW, GR55_synth1_onoff); // Switch synth 1 off
  write_GR55(GR55_SYNTH2_SW, GR55_synth2_onoff); // Switch synth 1 off
  write_GR55(GR55_COSM_GUITAR_SW, GR55_COSM_onoff); // Switch COSM guitar on
  write_GR55(GR55_NORMAL_PU_SW, GR55_nrml_pu_onoff); // Switch normal pu on
}

But this means the processor is tied up waiting if several messages are sent in short succession (like in the GR55_ummute() function), whilst it could be doing other useful things.
Is there a way to buffer or delay the serial transmission (using the USART) without tying up the processor in a wait loop. I hope this will improve performance of my foot controller.

Full code: https://github.com/sixeight7/VController_v2/blob/master/VController_v2/MIDI_GR55.ino#L427
 
Last edited:
Status
Not open for further replies.
Back
Top