Skip to content

Commit d4d7435

Browse files
committed
BLE and USB adapter examples
1 parent 3487775 commit d4d7435

File tree

5 files changed

+198
-2
lines changed

5 files changed

+198
-2
lines changed

doxygen/pages/MIDI.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,15 @@ USBDebugMIDI_Interface midi_dbg;
804804
BidirectionalMIDI_Pipe pipes;
805805

806806
void setup() {
807-
// Manually route MIDI output from Control_Surface to the MIDI interface,
808-
// and the MIDI output from the MIDI interface to Control_Surface
807+
// Manually route MIDI input from the debug interface to the USB interface,
808+
// and the MIDI input from the USB interface to the debug interface
809809
midi_dbg | pipes | midi_usb;
810810
// Initialize the MIDI interfaces
811811
MIDI_Interface::beginAll();
812812
}
813813

814814
void loop() {
815+
// Continuously poll all interfaces and route the traffic between them
815816
MIDI_Interface::updateAll();
816817
}
817818
```
@@ -882,3 +883,5 @@ functions. See @ref MIDI_Pipes-Filter.ino for more details.
882883
- @ref Dual-MIDI-Interface.ino
883884
- @ref MIDI-Monitor.ino
884885
- @ref MIDI_Pipes-Filter.ino
886+
- @ref USBMIDI-Adapter.ino
887+
- @ref BLEMIDI-Adapter.ino
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Turns an Arduino into a Bluetooth Low Energy (BLE) to 5-pin DIN MIDI adapter.
3+
*
4+
* @boards Nano 33 IoT, Nano 33 BLE, ESP32, ESP32-S3
5+
*
6+
* Connections
7+
* -----------
8+
*
9+
* - TXD: connected to a MIDI 5-pin DIN output connector
10+
* (with series resistor, possibly through a buffer)
11+
* - RXD: connected to a MIDI 5-pin DIN input connector
12+
* (with an optocoupler)
13+
*
14+
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
15+
* for the schematic, optocoupler models and resistor values.
16+
*
17+
* Behavior
18+
* --------
19+
*
20+
* - The Arduino will advertise itself as a Bluetooth Low Energy MIDI device
21+
* with the name `MIDI Adapter`.
22+
* - When you connect to the Arduino using your phone or computer, the built-in
23+
* LED turns on to indicate that the connection was successful.
24+
* - Any MIDI messages sent to the Arduino over BLE are sent out to the 5-pin
25+
* DIN output connector.
26+
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
27+
* are sent over BLE.
28+
*
29+
* @see @ref md_pages_MIDI-over-BLE
30+
* @see @ref midi-tutorial
31+
*
32+
* Written by PieterP, 2024-01-21
33+
* https://github.com/tttapa/Control-Surface
34+
*/
35+
36+
#include <Control_Surface.h>
37+
38+
// Instantiate a MIDI over BLE interface
39+
BluetoothMIDI_Interface midi_ble;
40+
// Instantiate a 5-pin DIN MIDI interface (on the TX and RX pins of Serial1)
41+
HardwareSerialMIDI_Interface midi_ser {Serial1};
42+
// Instantiate the pipe to connect the two interfaces
43+
BidirectionalMIDI_Pipe pipes;
44+
45+
void setup() {
46+
// Change the name of the BLE device (must be done before initializing it)
47+
midi_ble.setName("MIDI Adapter");
48+
// Manually route MIDI input from the serial interface to the BLE interface,
49+
// and the MIDI input from the BLE interface to the serial interface
50+
midi_ser | pipes | midi_ble;
51+
// Initialize the MIDI interfaces
52+
MIDI_Interface::beginAll();
53+
// Initialize the built-in LED
54+
pinMode(LED_BUILTIN, OUTPUT);
55+
}
56+
57+
void loop() {
58+
// Continuously poll all interfaces and route the traffic between them
59+
MIDI_Interface::updateAll();
60+
// Display the connection status using the built-in LED
61+
digitalWrite(LED_BUILTIN, midi_ble.isConnected() ? HIGH : LOW);
62+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Turns an Arduino into a USB to 5-pin DIN MIDI adapter.
3+
*
4+
* @boards AVR USB, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, ESP32-S3, Teensy 3.x
5+
*
6+
* Connections
7+
* -----------
8+
*
9+
* - TXD: connected to a MIDI 5-pin DIN output connector
10+
* (with series resistor, possibly through a buffer)
11+
* - RXD: connected to a MIDI 5-pin DIN input connector
12+
* (with an optocoupler)
13+
*
14+
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
15+
* for the schematic, optocoupler models and resistor values.
16+
*
17+
* Behavior
18+
* --------
19+
*
20+
* - The Arduino will show up as a USB MIDI device.
21+
* - Any MIDI messages sent to the Arduino over USB are sent out to the 5-pin
22+
* DIN output connector.
23+
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
24+
* are sent over USB.
25+
*
26+
* @see @ref md_pages_MIDI-over-USB
27+
* @see @ref midi-tutorial
28+
*
29+
* Written by PieterP, 2024-01-21
30+
* https://github.com/tttapa/Control-Surface
31+
*/
32+
33+
#include <Control_Surface.h>
34+
35+
// Instantiate a MIDI over USB interface
36+
USBMIDI_Interface midi_usb;
37+
// Instantiate a 5-pin DIN MIDI interface (on the TX and RX pins of Serial1)
38+
HardwareSerialMIDI_Interface midi_ser {Serial1};
39+
// Instantiate the pipe to connect the two interfaces
40+
BidirectionalMIDI_Pipe pipes;
41+
42+
void setup() {
43+
// Manually route MIDI input from the serial interface to the USB interface,
44+
// and the MIDI input from the USB interface to the serial interface
45+
midi_ser | pipes | midi_usb;
46+
// Initialize the MIDI interfaces
47+
MIDI_Interface::beginAll();
48+
}
49+
50+
void loop() {
51+
// Continuously poll all interfaces and route the traffic between them
52+
MIDI_Interface::updateAll();
53+
}

examples/examples.dox

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,83 @@
16471647
* https://github.com/tttapa/Control-Surface
16481648
*/
16491649

1650+
/**
1651+
* @example "USBMIDI-Adapter.ino"
1652+
*
1653+
* USBMIDI-Adapter
1654+
* ===============
1655+
*
1656+
* Turns an Arduino into a USB to 5-pin DIN MIDI adapter.
1657+
*
1658+
* @boards AVR USB, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, ESP32-S3, Teensy 3.x
1659+
*
1660+
* Connections
1661+
* -----------
1662+
*
1663+
* - TXD: connected to a MIDI 5-pin DIN output connector
1664+
* (with series resistor, possibly through a buffer)
1665+
* - RXD: connected to a MIDI 5-pin DIN input connector
1666+
* (with an optocoupler)
1667+
*
1668+
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
1669+
* for the schematic, optocoupler models and resistor values.
1670+
*
1671+
* Behavior
1672+
* --------
1673+
*
1674+
* - The Arduino will show up as a USB MIDI device.
1675+
* - Any MIDI messages sent to the Arduino over USB are sent out to the 5-pin
1676+
* DIN output connector.
1677+
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
1678+
* are sent over USB.
1679+
*
1680+
* @see @ref md_pages_MIDI-over-USB
1681+
* @see @ref midi-tutorial
1682+
*
1683+
* Written by PieterP, 2024-01-21
1684+
* https://github.com/tttapa/Control-Surface
1685+
*/
1686+
1687+
/**
1688+
* @example "BLEMIDI-Adapter.ino"
1689+
*
1690+
* BLEMIDI-Adapter
1691+
* ===============
1692+
*
1693+
* Turns an Arduino into a Bluetooth Low Energy (BLE) to 5-pin DIN MIDI adapter.
1694+
*
1695+
* @boards Nano 33 IoT, Nano 33 BLE, ESP32, ESP32-S3
1696+
*
1697+
* Connections
1698+
* -----------
1699+
*
1700+
* - TXD: connected to a MIDI 5-pin DIN output connector
1701+
* (with series resistor, possibly through a buffer)
1702+
* - RXD: connected to a MIDI 5-pin DIN input connector
1703+
* (with an optocoupler)
1704+
*
1705+
* See https://midi.org/specifications/midi-transports-specifications/5-pin-din-electrical-specs
1706+
* for the schematic, optocoupler models and resistor values.
1707+
*
1708+
* Behavior
1709+
* --------
1710+
*
1711+
* - The Arduino will advertise itself as a Bluetooth Low Energy MIDI device
1712+
* with the name `MIDI Adapter`.
1713+
* - When you connect to the Arduino using your phone or computer, the built-in
1714+
* LED turns on to indicate that the connection was successful.
1715+
* - Any MIDI messages sent to the Arduino over BLE are sent out to the 5-pin
1716+
* DIN output connector.
1717+
* - Any MIDI messages sent to the Arduino through the 5-pin DIN input connector
1718+
* are sent over BLE.
1719+
*
1720+
* @see @ref md_pages_MIDI-over-BLE
1721+
* @see @ref midi-tutorial
1722+
*
1723+
* Written by PieterP, 2024-01-21
1724+
* https://github.com/tttapa/Control-Surface
1725+
*/
1726+
16501727
/**
16511728
* @example "Bank.ino"
16521729
*

test/examples-board-fqbns.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
esp32: esp32:esp32:esp32thing:FlashFreq=80,PartitionScheme=default,UploadSpeed=921600,DebugLevel=none
2+
esp32-s3: esp32:esp32:esp32s3
23
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
34
teensy 3.x: teensy:avr:teensy31:speed=96,usb=serialmidiaudio,opt=o2std,keys=en-us
45
teensy 3.6: teensy:avr:teensy36:speed=180,usb=serialmidiaudio,opt=o2std,keys=en-us

0 commit comments

Comments
 (0)