From 44b32fa27bbbe3ccfa366dc31180036d140d124a Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Sun, 26 Nov 2023 14:15:11 +0100 Subject: [PATCH] Added MIDI via USBHOST-Serial at 38400 bps. --- config.h | 11 +++++++--- midi_devices.hpp | 54 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/config.h b/config.h index 8f4de26..1ad9efa 100644 --- a/config.h +++ b/config.h @@ -60,7 +60,7 @@ // // Information about memory layout, etc.: https://www.pjrc.com/store/teensy41.html -#define VERSION "1.2.8" +#define VERSION "1.2.9" //************************************************************************************************* //* DEVICE SETTINGS @@ -71,13 +71,18 @@ //************************************************************************************************* #define MIDI_DEVICE_DIN Serial1 #define MIDI_DEVICE_USB 1 -#define MIDI_DEVICE_USB_HOST 1 +//#define MIDI_DEVICE_USB_HOST 1 +#define MIDI_DEVICE_USB_HOST_SERIAL 1 -#ifdef MIDI_DEVICE_USB_HOST +#ifdef MIDI_DEVICE_USB_HOST_SERIAL #define USB_HOST_SERIAL_BAUD 38400 #define USB_HOST_SERIAL_FORMAT USBHOST_SERIAL_8N1 #endif +/*#if defined(MIDI_DEVICE_USB_HOST) && defined(MIDI_DEVICE_USB_HOST_SERIAL) +#error Only MIDI_DEVICE_USB_HOST or MIDI_DEVICE_USB_HOST_SERIAL is possible to enable at the same time! +#endif */ + //************************************************************************************************* //* AUDIO HARDWARE SETTINGS //************************************************************************************************* diff --git a/midi_devices.hpp b/midi_devices.hpp index b038e21..23b878d 100644 --- a/midi_devices.hpp +++ b/midi_devices.hpp @@ -28,13 +28,23 @@ extern config_t configuration; -#ifdef MIDI_DEVICE_USB_HOST +#if defined(MIDI_DEVICE_USB_HOST) || defined(MIDI_DEVICE_USB_HOST_SERIAL) #include #endif // override default sysex size settings struct MicroDexedSettings : public midi::DefaultSettings { - static const unsigned SysExMaxSize = 4104; // Accept SysEx messages up to 1024 bytes long. + static const bool UseRunningStatus = false; + static const bool HandleNullVelocityNoteOnAsNoteOff = true; + static const bool Use1ByteParsing = true; + static const unsigned SysExMaxSize = 4104; + static const bool UseSenderActiveSensing = false; + static const bool UseReceiverActiveSensing = false; + static const uint16_t SenderActiveSensingPeriodicity = 0; +}; + +struct CustomBaudRateSettings : public MIDI_NAMESPACE::DefaultSerialSettings { + static const long BaudRate = 38400; }; #ifdef MIDI_DEVICE_DIN @@ -44,8 +54,17 @@ MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, MIDI_DEVICE_DIN, midi_serial, MicroD #ifdef MIDI_DEVICE_USB_HOST USBHost usb_host; MIDIDevice midi_usb(usb_host); -USBSerial userial(usb_host); -MIDI_CREATE_CUSTOM_INSTANCE(USBSerial, userial, usbhost_midi_serial, MicroDexedSettings); +#endif + +#ifdef MIDI_DEVICE_USB_HOST_SERIAL +USBHost usb_host; +USBHub hub1(usb_host); +USBHub hub2(usb_host); +USBHub hub3(usb_host); +USBHub hub4(usb_host); +USBSerial_BigBuffer userial(usb_host,1); +MIDI_NAMESPACE::SerialMIDI serialMIDI(userial); +MIDI_NAMESPACE::MidiInterface> usbhost_midi_serial((MIDI_NAMESPACE::SerialMIDI&)serialMIDI); #endif void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity); @@ -685,11 +704,9 @@ void handleSystemReset_MIDI_DEVICE_USB_HOST(void) { handleRealTimeSystem_generic(inRealTime, MIDI_USB_HOST); } */ +#endif // MIDI_DEVICE_USB_HOST - - - - +#ifdef MIDI_DEVICE_USB_HOST_SERIAL void handleNoteOn_MIDI_DEVICE_USB_HOST_SERIAL(byte inChannel, byte inNoteNumber, byte inVelocity) { handle_generic(inChannel, inNoteNumber, inVelocity, MIDI_BY_USB_HOST, midi::NoteOn); } @@ -788,7 +805,7 @@ void handleSystemReset_MIDI_DEVICE_USB_HOST_SERIAL(void) { { handleRealTimeSystem_generic(inRealTime, MIDI_USB_HOST); } */ -#endif // MIDI_DEVICE_USB_HOST +#endif // MIDI_DEVICE_USB_HOST_SERIAL /***************************************** MIDI_DEVICE_USB @@ -979,8 +996,14 @@ FLASHMEM void setup_midi_devices(void) { midi_usb.setHandleActiveSensing(handleActiveSensing_MIDI_DEVICE_USB_HOST); midi_usb.setHandleSystemReset(handleSystemReset_MIDI_DEVICE_USB_HOST); //midi_usb.setHandleRealTimeSystem(handleRealTimeSystem_MIDI_DEVICE_USB_HOST); - - userial.begin(USB_HOST_SERIAL_BAUD,USB_HOST_SERIAL_FORMAT); +#ifdef DEBUG + Serial.println(F("MIDI_DEVICE_USB_HOST enabled.")); +#endif +#endif + +#ifdef MIDI_DEVICE_USB_HOST_SERIAL + usb_host.begin(); + userial.begin(USB_HOST_SERIAL_BAUD, USB_HOST_SERIAL_FORMAT); usbhost_midi_serial.setHandleNoteOn(handleNoteOn_MIDI_DEVICE_USB_HOST_SERIAL); usbhost_midi_serial.setHandleNoteOff(handleNoteOff_MIDI_DEVICE_USB_HOST_SERIAL); usbhost_midi_serial.setHandleControlChange(handleControlChange_MIDI_DEVICE_USB_HOST_SERIAL); @@ -999,10 +1022,12 @@ FLASHMEM void setup_midi_devices(void) { usbhost_midi_serial.setHandleStop(handleStop_MIDI_DEVICE_USB_HOST_SERIAL); usbhost_midi_serial.setHandleActiveSensing(handleActiveSensing_MIDI_DEVICE_USB_HOST_SERIAL); usbhost_midi_serial.setHandleSystemReset(handleSystemReset_MIDI_DEVICE_USB_HOST_SERIAL); - //usbhost_midi_serial.setHandleRealTimeSystem(handleRealTimeSystem_MIDI_DEVICE_USB_HOST_SERIAL); +//usbhost_midi_serial.setHandleRealTimeSystem(handleRealTimeSystem_MIDI_DEVICE_USB_HOST_SERIAL); + + usbhost_midi_serial.begin(MIDI_CHANNEL_OMNI); #ifdef DEBUG - Serial.println(F("MIDI_DEVICE_USB_HOST enabled.")); + Serial.println(F("MIDI_DEVICE_USB_HOST_SERIAL enabled.")); #endif #endif @@ -1044,6 +1069,9 @@ FLASHMEM void check_midi_devices(void) { #ifdef MIDI_DEVICE_USB_HOST usb_host.Task(); midi_usb.read(); +#endif +#ifdef MIDI_DEVICE_USB_HOST_SERIAL + usb_host.Task(); usbhost_midi_serial.read(); #endif }