Skip to content

Commit

Permalink
BLE and USB adapter examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Jan 23, 2024
1 parent 3487775 commit d4d7435
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 2 deletions.
7 changes: 5 additions & 2 deletions doxygen/pages/MIDI.md
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,15 @@ USBDebugMIDI_Interface midi_dbg;
BidirectionalMIDI_Pipe pipes;

void setup() {
// Manually route MIDI output from Control_Surface to the MIDI interface,
// and the MIDI output from the MIDI interface to Control_Surface
// Manually route MIDI input from the debug interface to the USB interface,
// and the MIDI input from the USB interface to the debug interface
midi_dbg | pipes | midi_usb;
// Initialize the MIDI interfaces
MIDI_Interface::beginAll();
}

void loop() {
// Continuously poll all interfaces and route the traffic between them
MIDI_Interface::updateAll();
}
```
Expand Down Expand Up @@ -882,3 +883,5 @@ functions. See @ref MIDI_Pipes-Filter.ino for more details.
- @ref Dual-MIDI-Interface.ino
- @ref MIDI-Monitor.ino
- @ref MIDI_Pipes-Filter.ino
- @ref USBMIDI-Adapter.ino
- @ref BLEMIDI-Adapter.ino
62 changes: 62 additions & 0 deletions examples/3. MIDI Interfaces/BLEMIDI-Adapter/BLEMIDI-Adapter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Turns an Arduino into a Bluetooth Low Energy (BLE) to 5-pin DIN MIDI adapter.
*
* @boards Nano 33 IoT, Nano 33 BLE, ESP32, ESP32-S3
*
* Connections
* -----------
*
* - TXD: connected to a MIDI 5-pin DIN output connector
* (with series resistor, possibly through a buffer)
* - RXD: connected to a MIDI 5-pin DIN input connector
* (with an optocoupler)
*
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
* for the schematic, optocoupler models and resistor values.
*
* Behavior
* --------
*
* - The Arduino will advertise itself as a Bluetooth Low Energy MIDI device
* with the name `MIDI Adapter`.
* - When you connect to the Arduino using your phone or computer, the built-in
* LED turns on to indicate that the connection was successful.
* - Any MIDI messages sent to the Arduino over BLE are sent out to the 5-pin
* DIN output connector.
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
* are sent over BLE.
*
* @see @ref md_pages_MIDI-over-BLE
* @see @ref midi-tutorial
*
* Written by PieterP, 2024-01-21
* https://github.com/tttapa/Control-Surface
*/

#include <Control_Surface.h>

// Instantiate a MIDI over BLE interface
BluetoothMIDI_Interface midi_ble;
// Instantiate a 5-pin DIN MIDI interface (on the TX and RX pins of Serial1)
HardwareSerialMIDI_Interface midi_ser {Serial1};
// Instantiate the pipe to connect the two interfaces
BidirectionalMIDI_Pipe pipes;

void setup() {
// Change the name of the BLE device (must be done before initializing it)
midi_ble.setName("MIDI Adapter");
// Manually route MIDI input from the serial interface to the BLE interface,
// and the MIDI input from the BLE interface to the serial interface
midi_ser | pipes | midi_ble;
// Initialize the MIDI interfaces
MIDI_Interface::beginAll();
// Initialize the built-in LED
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
// Continuously poll all interfaces and route the traffic between them
MIDI_Interface::updateAll();
// Display the connection status using the built-in LED
digitalWrite(LED_BUILTIN, midi_ble.isConnected() ? HIGH : LOW);
}
53 changes: 53 additions & 0 deletions examples/3. MIDI Interfaces/USBMIDI-Adapter/USBMIDI-Adapter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Turns an Arduino into a USB to 5-pin DIN MIDI adapter.
*
* @boards AVR USB, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, ESP32-S3, Teensy 3.x
*
* Connections
* -----------
*
* - TXD: connected to a MIDI 5-pin DIN output connector
* (with series resistor, possibly through a buffer)
* - RXD: connected to a MIDI 5-pin DIN input connector
* (with an optocoupler)
*
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
* for the schematic, optocoupler models and resistor values.
*
* Behavior
* --------
*
* - The Arduino will show up as a USB MIDI device.
* - Any MIDI messages sent to the Arduino over USB are sent out to the 5-pin
* DIN output connector.
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
* are sent over USB.
*
* @see @ref md_pages_MIDI-over-USB
* @see @ref midi-tutorial
*
* Written by PieterP, 2024-01-21
* https://github.com/tttapa/Control-Surface
*/

#include <Control_Surface.h>

// Instantiate a MIDI over USB interface
USBMIDI_Interface midi_usb;
// Instantiate a 5-pin DIN MIDI interface (on the TX and RX pins of Serial1)
HardwareSerialMIDI_Interface midi_ser {Serial1};
// Instantiate the pipe to connect the two interfaces
BidirectionalMIDI_Pipe pipes;

void setup() {
// Manually route MIDI input from the serial interface to the USB interface,
// and the MIDI input from the USB interface to the serial interface
midi_ser | pipes | midi_usb;
// Initialize the MIDI interfaces
MIDI_Interface::beginAll();
}

void loop() {
// Continuously poll all interfaces and route the traffic between them
MIDI_Interface::updateAll();
}
77 changes: 77 additions & 0 deletions examples/examples.dox
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,83 @@
* https://github.com/tttapa/Control-Surface
*/

/**
* @example "USBMIDI-Adapter.ino"
*
* USBMIDI-Adapter
* ===============
*
* Turns an Arduino into a USB to 5-pin DIN MIDI adapter.
*
* @boards AVR USB, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, ESP32-S3, Teensy 3.x
*
* Connections
* -----------
*
* - TXD: connected to a MIDI 5-pin DIN output connector
* (with series resistor, possibly through a buffer)
* - RXD: connected to a MIDI 5-pin DIN input connector
* (with an optocoupler)
*
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
* for the schematic, optocoupler models and resistor values.
*
* Behavior
* --------
*
* - The Arduino will show up as a USB MIDI device.
* - Any MIDI messages sent to the Arduino over USB are sent out to the 5-pin
* DIN output connector.
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
* are sent over USB.
*
* @see @ref md_pages_MIDI-over-USB
* @see @ref midi-tutorial
*
* Written by PieterP, 2024-01-21
* https://github.com/tttapa/Control-Surface
*/

/**
* @example "BLEMIDI-Adapter.ino"
*
* BLEMIDI-Adapter
* ===============
*
* Turns an Arduino into a Bluetooth Low Energy (BLE) to 5-pin DIN MIDI adapter.
*
* @boards Nano 33 IoT, Nano 33 BLE, ESP32, ESP32-S3
*
* Connections
* -----------
*
* - TXD: connected to a MIDI 5-pin DIN output connector
* (with series resistor, possibly through a buffer)
* - RXD: connected to a MIDI 5-pin DIN input connector
* (with an optocoupler)
*
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
* for the schematic, optocoupler models and resistor values.
*
* Behavior
* --------
*
* - The Arduino will advertise itself as a Bluetooth Low Energy MIDI device
* with the name `MIDI Adapter`.
* - When you connect to the Arduino using your phone or computer, the built-in
* LED turns on to indicate that the connection was successful.
* - Any MIDI messages sent to the Arduino over BLE are sent out to the 5-pin
* DIN output connector.
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
* are sent over BLE.
*
* @see @ref md_pages_MIDI-over-BLE
* @see @ref midi-tutorial
*
* Written by PieterP, 2024-01-21
* https://github.com/tttapa/Control-Surface
*/

/**
* @example "Bank.ino"
*
Expand Down
1 change: 1 addition & 0 deletions test/examples-board-fqbns.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
esp32: esp32:esp32:esp32thing:FlashFreq=80,PartitionScheme=default,UploadSpeed=921600,DebugLevel=none
esp32-s3: esp32:esp32:esp32s3
esp8266: esp8266:esp8266:d1_mini:xtal=80,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=921600
teensy 3.x: teensy:avr:teensy31:speed=96,usb=serialmidiaudio,opt=o2std,keys=en-us
teensy 3.6: teensy:avr:teensy36:speed=180,usb=serialmidiaudio,opt=o2std,keys=en-us
Expand Down

0 comments on commit d4d7435

Please sign in to comment.