From 8193fc58a7111fa6e52e79173f3921c55f8abb89 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Tue, 28 Feb 2017 17:04:49 -0500 Subject: [PATCH 01/12] add midi tx test --- tools/midi_tests/package.json | 17 ++++ tools/midi_tests/tx.js | 47 +++++++++++ tools/midi_tests/tx_arduino/tx_arduino.ino | 92 ++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 tools/midi_tests/package.json create mode 100644 tools/midi_tests/tx.js create mode 100644 tools/midi_tests/tx_arduino/tx_arduino.ino diff --git a/tools/midi_tests/package.json b/tools/midi_tests/package.json new file mode 100644 index 00000000..d7c3c95a --- /dev/null +++ b/tools/midi_tests/package.json @@ -0,0 +1,17 @@ +{ + "name": "midi-test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "async": "^2.0.0-rc.2", + "inquirer": "^3.0.5", + "midi": "^0.9.4", + "multi-progress": "^2.0.0" + } +} diff --git a/tools/midi_tests/tx.js b/tools/midi_tests/tx.js new file mode 100644 index 00000000..24ad4e52 --- /dev/null +++ b/tools/midi_tests/tx.js @@ -0,0 +1,47 @@ +var midi = require('midi'), + async = require('async'), + inquirer = require('inquirer'), + progress = require('multi-progress'), + input = new midi.input(), + multi = new progress(), + portNames = []; + +for(var i=0; i < input.getPortCount(); i++) { + portNames.push(input.getPortName(i)); +} + +inquirer.prompt([ + { + type: 'list', + name: 'port', + message: 'Select port', + choices: portNames + } +]).then((answers) => { + input.openPort(portNames.indexOf(answers.port)); + startTest(); +}); + +function startTest() { + + var on_bar = multi.newBar('midi on [:bar] :current/:total', { + incomplete: ' ', + total: 128 + }); + + var off_bar = multi.newBar('midi off [:bar] :current/:total', { + incomplete: ' ', + total: 128, + callback: function() { + input.closePort(); + process.exit(); + } + }); + + input.on('message', function(deltaTime, message) { + if(message[0] != 144) return; + if(message[2] == 0) off_bar.tick(); + if(message[2] == 100) on_bar.tick(); + }); + +} diff --git a/tools/midi_tests/tx_arduino/tx_arduino.ino b/tools/midi_tests/tx_arduino/tx_arduino.ino new file mode 100644 index 00000000..ca005ba8 --- /dev/null +++ b/tools/midi_tests/tx_arduino/tx_arduino.ino @@ -0,0 +1,92 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include + +BLEMidi blemidi; + +bool startTest = false; +int on_count = 0; +int off_count = 0; + +unsigned long on_interval = 5; +unsigned long off_interval = on_interval; + +unsigned long prev_on = millis(); +unsigned long prev_off = millis(); + +void setup() +{ + Serial.begin(115200); + + Serial.println("Bluefruit52 BLEMIDI TX Test"); + + Bluefruit.autoConnLed(true); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + blemidi.start(); + + Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.addAdvTxPower(); + Bluefruit.addAdvService(blemidi); + Bluefruit.addScanRespName(); + + Bluefruit.startAdvertising(); + +} + +void loop() +{ + + if (! Bluefruit.connected()) + return; + + if (! blemidi.configured()) + return; + + unsigned long currentMillis = millis(); + + if(! startTest) { + Serial.println(); + Serial.println("Start the node TX test and press a key in the serial monitor to start the test..."); + while(Serial.available() == 0) { } + while(Serial.available()) { Serial.read(); } + startTest = true; + prev_on = currentMillis; + prev_off = currentMillis; + Serial.println("Starting test."); + } + + if(on_count >= 128 && off_count >= 128) { + Serial.println("Test finished. Resetting."); + startTest = false; + on_count = 0; + off_count = 0; + return; + } + + if (currentMillis - prev_on >= on_interval && on_count < 128) { + blemidi.send(0x90, on_count, 0x64); + on_count++; + prev_on = currentMillis; + } + + if (currentMillis - prev_off >= off_interval && off_count < 128) { + blemidi.send(0x90, off_count, 0x00); + off_count++; + prev_off = currentMillis; + } + +} From 8d3cada73f317eee78c6369030a5ac5e62d4c3b5 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Tue, 28 Feb 2017 17:05:12 -0500 Subject: [PATCH 02/12] add midi rx test --- tools/midi_tests/rx.js | 69 ++++++++++++++++++++++ tools/midi_tests/rx_arduino/rx_arduino.ino | 69 ++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 tools/midi_tests/rx.js create mode 100644 tools/midi_tests/rx_arduino/rx_arduino.ino diff --git a/tools/midi_tests/rx.js b/tools/midi_tests/rx.js new file mode 100644 index 00000000..f537bd20 --- /dev/null +++ b/tools/midi_tests/rx.js @@ -0,0 +1,69 @@ +var midi = require('midi'), + async = require('async'), + inquirer = require('inquirer'), + progress = require('multi-progress')(process.stdout), + output = new midi.output(), + speed = 100, + portNames = []; + +for(var i=0; i < output.getPortCount(); i++) { + portNames.push(output.getPortName(i)); +} + +if(! portNames.length) { + console.error('no MIDI ports available'); + process.exit(1); +} + +inquirer.prompt([ + { + type: 'input', + name: 'speed', + message: 'Send interval (ms)', + default: speed + }, + { + type: 'list', + name: 'port', + message: 'Select port', + choices: portNames + } +]).then(answers => { + output.openPort(portNames.indexOf(answers.port)); + speed = parseInt(answers.speed); + startTest(); +}); + +function startTest() { + + var on_bar = progress.newBar('midi on [:bar] :current/:total', { + incomplete: ' ', + total: 128 + }); + + var off_bar = progress.newBar('midi off [:bar] :current/:total', { + incomplete: ' ', + total: 128 + }); + + async.timesSeries(128, function(n, next){ + + setTimeout(function() { + on_bar.tick(); + output.sendMessage([0x90, n, 0x7F]); + next(); + }, speed); + + }, function() {}); + + async.timesSeries(128, function(n, next){ + + setTimeout(function() { + off_bar.tick(); + output.sendMessage([0x90, n, 0x00]); + next(); + }, speed); + + }, function() {}); + +} diff --git a/tools/midi_tests/rx_arduino/rx_arduino.ino b/tools/midi_tests/rx_arduino/rx_arduino.ino new file mode 100644 index 00000000..165deb34 --- /dev/null +++ b/tools/midi_tests/rx_arduino/rx_arduino.ino @@ -0,0 +1,69 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include + +BLEMidi blemidi; + +int on_count = 0; +int off_count = 0; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Bluefruit52 BLEMIDI RX Test"); + + Bluefruit.autoConnLed(true); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + blemidi.setWriteCallback(midi_callback); + blemidi.start(); + + Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.addAdvTxPower(); + Bluefruit.addAdvService(blemidi); + Bluefruit.addScanRespName(); + + Bluefruit.startAdvertising(); +} + +void loop() {} + +void midi_callback(uint32_t timestamp, uint8_t data[]) +{ + Serial.print("[MIDI "); + Serial.print(timestamp); + Serial.print(" ] "); + + Serial.print(data[0], HEX); Serial.print(" "); + Serial.print(data[1] , HEX); Serial.print(" "); + Serial.print(data[2] , HEX); Serial.print(" "); + + if(data[0] == 0x90 && data[2] == 0x00) { + off_count++; + } else if(data[0] == 0x90 && data[2] == 0x7F) { + on_count++; + } + + Serial.println(); + + Serial.print("note on count: "); + Serial.println(on_count, DEC); + Serial.print("note off count: "); + Serial.println(off_count, DEC); + +} + From 50d24e893623be7f80e4bd9f6ffa3032c32bc187 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 9 Mar 2017 13:46:21 -0500 Subject: [PATCH 03/12] init BLE midi from MIDI.begin() --- libraries/Bluefruit52Lib/src/services/BLEMidi.cpp | 6 ++++++ libraries/Bluefruit52Lib/src/services/BLEMidi.h | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp index 27657ca2..50b3a09f 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -201,6 +201,12 @@ void BLEMidi::setWriteCallback(midi_write_cb_t fp) _io.setWriteCallback(blemidi_write_cb); } +void BLEMidi::begin(int baudrate) +{ + (void) baudrate; + begin(); +} + err_t BLEMidi::begin(void) { VERIFY_STATUS( this->addToGatt() ); diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.h b/libraries/Bluefruit52Lib/src/services/BLEMidi.h index b12b12ca..6688e9ee 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.h +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.h @@ -70,7 +70,7 @@ class BLEMidi: public BLEService, public Stream virtual err_t begin(void); // MidiInterface - void begin(int baudrate) { (void) baudrate; } + void begin(int baudrate); bool notifyEnabled(void); From 9a8a04bdd6405bbc29f5911537585c338b495e6a Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 9 Mar 2017 13:47:16 -0500 Subject: [PATCH 04/12] update blemidi example --- .../examples/Peripheral/blemidi/blemidi.ino | 148 +++++++++++------- .../blemidi_callback/blemidi_callback.ino | 148 ------------------ 2 files changed, 88 insertions(+), 208 deletions(-) delete mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/blemidi_callback/blemidi_callback.ino diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino b/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino index be4b558c..09ffabb2 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino @@ -14,100 +14,128 @@ #include #include -// To test: -// - Run this sketch and open the Serial Monitor -// - Open the iGrand Piano Free app -// - Open the midimittr app on your phone and under Clients select "Bluefruit52" -// - When you see the 'Connected' label switch to the Routing panel -// - Set the Destination to 'iGrand Piano' -// - Switch to the iGrand Piano Free app and you should see notes playing one by one - BLEDis bledis; BLEMidi blemidi; -// Create MIDI instance using ble service as physical layer +// Create a new instance of the Arduino MIDI Library, +// and attach BluefruitLE MIDI as the transport. MIDI_CREATE_BLE_INSTANCE(blemidi); +// Variable that holds the current position in the sequence. +int position = 0; + +// Store example melody as an array of note values +byte note_sequence[] = { + 74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78, + 74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61, + 56,61,64,68,74,78,81,86,90,93,98,102 +}; + void setup() { + Serial.begin(115200); - Serial.println("Bluefruit52 BLEMIDI Example"); + Serial.println("Adafruit Bluefruit52 MIDI over Bluetooth LE Example"); Bluefruit.begin(); - Bluefruit.setName("Bluefruit52"); + Bluefruit.setName("Bluefruit52 MIDI"); - // Setup the BLE LED to be enabled on CONNECT - // Note: This is actually the default behaviour, but provided - // here in case you want to control this manually via PIN 19 - Bluefruit.autoConnLed(true); - - Bluefruit.setConnectCallback(connect_callback); - Bluefruit.setDisconnectCallback(disconnect_callback); - + // Setup the on board blue LED to be enabled on CONNECT + Bluefruit.autoConnLed(true); // Configure and Start Device Information Service bledis.setManufacturer("Adafruit Industries"); bledis.setModel("Bluefruit Feather52"); bledis.begin(); - - /* Start BLE MIDI - * Note: Apple requires BLE device must have min connection interval >= 20m - * ( The smaller the connection interval the faster we could send data). - * However for HID and MIDI device, Apple could accept min connection interval - * up to 11.25 ms. Therefore BLEMidi::start() will try to set the min and max - * connection interval to 11.25 ms and 15 ms respectively for best performance. - */ - blemidi.setWriteCallback(midi_write_callback); - blemidi.begin(); - // Initialize MIDI, listen to all channels + // Set BLE write callback + blemidi.setWriteCallback(handleWrite); + + // Initialize MIDI, and listen to all MIDI channels MIDI.begin(MIDI_CHANNEL_OMNI); - // Set up Advertising Packet - setupAdv(); + // Attach the handleNoteOn function to the MIDI Library. It will + // be called whenever the Bluefruit receives MIDI Note On messages. + MIDI.setHandleNoteOn(handleNoteOn); + + // Do the same for MIDI Note Off messages. + MIDI.setHandleNoteOff(handleNoteOff); + + // Set General Discoverable Mode flag + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + + // Advertise TX Power + Bluefruit.Advertising.addTxPower(); + + // Advertise BLE MIDI Service + Bluefruit.Advertising.addService(blemidi); + + // Advertise device name in the Scan Response + Bluefruit.ScanResponse.addName(); // Start Advertising Bluefruit.Advertising.start(); + } -void setupAdv(void) +void handleNoteOn(byte channel, byte pitch, byte velocity) { - //Bluefruit.Advertising.addTxPower(); - Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.Advertising.addTxPower(); - - // Include bleuart 128-bit uuid - Bluefruit.Advertising.addService(blemidi); - - // There is no room for Name in Advertising packet - // Use Scan response for Name - Bluefruit.ScanResponse.addName(); + // Log when a note is pressed. + Serial.printf("Note on: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity); + Serial.println(); } -void midi_write_callback(void) +void handleNoteOff(byte channel, byte pitch, byte velocity) { + // Log when a note is released. + Serial.printf("Note off: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity); + Serial.println(); +} + +void handleWrite() { + // Tell the MIDI instance that we have new data. MIDI.read(); } void loop() { - if ( Bluefruit.connected() && blemidi.notifyEnabled() ) - { - MIDI.sendNoteOn(42, 127, 1); // Send a Note (pitch 42, velo 127 on channel 1) - delay(1000); // Wait for a second - MIDI.sendNoteOff(42, 0, 1); // Stop the note + + // Don't continue if we aren't connected. + if (! Bluefruit.connected()) { + return; } -} -void connect_callback(void) -{ - Serial.println("Connected"); -} + // Don't continue if the connected device isn't ready to receive messages. + if (! blemidi.notifyEnabled()) { + return; + } + + // Setup variables for the current and previous + // positions in the note sequence. + int current = position; + int previous = position - 1; + + // If we currently are at position 0, set the + // previous position to the last note in the sequence. + if (previous < 0) { + previous = sizeof(note_sequence) - 1; + } + + // Send Note On for current position at full velocity (127) on channel 1. + MIDI.sendNoteOn(note_sequence[current], 127, 1); + + // Send Note Off for previous note. + MIDI.sendNoteOff(note_sequence[previous], 0, 1); + + // Increment position + position++; + + // If we are at the end of the sequence, start over. + if (position >= sizeof(note_sequence)) { + position = 0; + } + + // Wait before sending next note + delay(286); -void disconnect_callback(uint8_t reason) -{ - (void) reason; - - Serial.println("Disconnected"); - Serial.println("Bluefruit will auto start advertising (default)"); } diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/blemidi_callback/blemidi_callback.ino b/libraries/Bluefruit52Lib/examples/Peripheral/blemidi_callback/blemidi_callback.ino deleted file mode 100644 index fff280f1..00000000 --- a/libraries/Bluefruit52Lib/examples/Peripheral/blemidi_callback/blemidi_callback.ino +++ /dev/null @@ -1,148 +0,0 @@ -/********************************************************************* - This is an example for our nRF52 based Bluefruit LE modules - - Pick one up today in the adafruit shop! - - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing - products from Adafruit! - - MIT license, check LICENSE for more information - All text above, and the splash screen below must be included in - any redistribution -*********************************************************************/ -#include -#include - -// To test: -// - Run this sketch and open the Serial Monitor -// - Open the iGrand Piano Free app -// - Open the midimittr app on your phone and under Clients select "Bluefruit52" -// - When you see the 'Connected' label switch to the Routing panel -// - Set the Destination to 'iGrand Piano' -// - Switch to the iGrand Piano Free app and you should see notes playing one by one - -BLEDis bledis; -BLEMidi blemidi; - -// Create MIDI instance using ble service as physical layer -MIDI_CREATE_BLE_INSTANCE(blemidi); - -void setup() -{ - Serial.begin(115200); - Serial.println("Bluefruit52 BLEMIDI Example"); - - Bluefruit.begin(); - Bluefruit.setName("Bluefruit52"); - - // Setup the BLE LED to be enabled on CONNECT - // Note: This is actually the default behaviour, but provided - // here in case you want to control this manually via PIN 19 - Bluefruit.autoConnLed(true); - - Bluefruit.setConnectCallback(connect_callback); - Bluefruit.setDisconnectCallback(disconnect_callback); - - - // Configure and Start Device Information Service - bledis.setManufacturer("Adafruit Industries"); - bledis.setModel("Bluefruit Feather52"); - bledis.begin(); - - - - /* Setup callback when data is writtent to MIDI characteristics - * and call MIDI.read() to handle message immediately. Alternately - * MIDI.read() can be placed in loop() (less real-time) - */ - blemidi.setWriteCallback(midi_write_callback); - - /* Start BLE MIDI - * Note: Apple requires BLE device must have min connection interval >= 20m - * ( The smaller the connection interval the faster we could send data). - * However for HID and MIDI device, Apple could accept min connection interval - * up to 11.25 ms. Therefore BLEMidi::start() will try to set the min and max - * connection interval to 11.25 ms and 15 ms respectively for best performance. - */ - blemidi.begin(); - - // Connect the handleNoteOn function to the library, - // so it is called upon reception of a NoteOn. - MIDI.setHandleNoteOn(handleNoteOn); // Put only the name of the function - - // Do the same for NoteOffs - MIDI.setHandleNoteOff(handleNoteOff); - - // Initialize MIDI, listen to all channels - MIDI.begin(MIDI_CHANNEL_OMNI); - - // Set up Advertising Packet - setupAdv(); - - // Start Advertising - Bluefruit.Advertising.start(); -} - -void setupAdv(void) -{ - //Bluefruit.Advertising.addTxPower(); - Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.Advertising.addTxPower(); - - // Include bleuart 128-bit uuid - Bluefruit.Advertising.addService(blemidi); - - // There is no room for Name in Advertising packet - // Use Scan response for Name - Bluefruit.ScanResponse.addName(); -} - -void loop() -{ - digitalToggle(LED_BUILTIN); - delay(1000); // Wait for a second -} - -void midi_write_callback(void) -{ - MIDI.read(); - - // There is no need to check if there are messages incoming - // if they are bound to a Callback function. - // The attached method will be called automatically - // when the corresponding message has been received. -} - -// This function will be automatically called when a NoteOn is received. -// It must be a void-returning function with the correct parameters, -// see documentation here: -// http://arduinomidilib.fortyseveneffects.com/a00022.html - -void handleNoteOn(byte channel, byte pitch, byte velocity) -{ - // Do whatever you want when a note is pressed. - Serial.printf("Note on: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity); - Serial.println(); -} - -void handleNoteOff(byte channel, byte pitch, byte velocity) -{ - // Do something when the note is released. - // Note that NoteOn messages with 0 velocity are interpreted as NoteOffs. - Serial.printf("Note off: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity); - Serial.println(); -} - -void connect_callback(void) -{ - Serial.println("Connected"); -} - -void disconnect_callback(uint8_t reason) -{ - (void) reason; - - Serial.println("Disconnected"); - Serial.println("Bluefruit will auto start advertising (default)"); -} From bfb90a3998e8e8607a47d30e5d8b74e183694daa Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 9 Mar 2017 15:07:39 -0500 Subject: [PATCH 05/12] add midi test readme --- tools/midi_tests/README.md | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tools/midi_tests/README.md diff --git a/tools/midi_tests/README.md b/tools/midi_tests/README.md new file mode 100644 index 00000000..9483861b --- /dev/null +++ b/tools/midi_tests/README.md @@ -0,0 +1,60 @@ +# MIDI Tests + +This folder contains a small number of tests used to verify BLE MIDI features. + +## Setup + +First, you will need the current stable version of [Node.js][node] installed. + +```console +$ node -v +v7.4.0 +``` + +Next, cd to `tools/midi_tests` and run `npm install`. + +```console +$ cd tools/midi_tests +$ npm install +``` + +## RX Test + +1. Upload the `rx_arduino` example to your nRF52 feather. +2. Open the serial monitor, and connect to the feather on OS X using Audio MIDI Setup. +3. Once you are connected, navigate to the `tools/midi_tests` folder and run the `rx.js` test. + +```console +$ cd tools/midi_tests +$ node rx.js +``` + +4. The test will ask how fast you would like to send messages. The default is 100ms. Enter a number and press return. + +``` +? Send interval (ms) 100 +``` + +5. Next, the test will ask you to select a MIDI port. Select the Bluefruit52 from the list. + +``` +? Select port Bluefruit52 Bluetooth +``` + +6. The test will begin sending messages at the specified interval. It will send 128 note on and 128 note off messages. + +``` +midi on [================================================================================================================================] 128/128 +midi off [================================================================================================================================] 128/128 +``` + +7. Check the Arduino Serial monitor and confirm that you have received all 128 note on and 128 note off messages. + +``` +note on count: 128 +note off count: 128 +``` + +If you have, the test passed. + +[node]: https://nodejs.org/ From f79ff182e1890bbf0c5649fefb88dfaa50f75675 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 9 Mar 2017 15:10:06 -0500 Subject: [PATCH 06/12] test README formatting --- tools/midi_tests/README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/midi_tests/README.md b/tools/midi_tests/README.md index 9483861b..b7d205c0 100644 --- a/tools/midi_tests/README.md +++ b/tools/midi_tests/README.md @@ -20,41 +20,41 @@ $ npm install ## RX Test -1. Upload the `rx_arduino` example to your nRF52 feather. -2. Open the serial monitor, and connect to the feather on OS X using Audio MIDI Setup. -3. Once you are connected, navigate to the `tools/midi_tests` folder and run the `rx.js` test. +* Upload the `rx_arduino` example to your nRF52 feather. +* Open the serial monitor, and connect to the feather on OS X using Audio MIDI Setup. +* Once you are connected, navigate to the `tools/midi_tests` folder and run the `rx.js` test. ```console $ cd tools/midi_tests $ node rx.js ``` -4. The test will ask how fast you would like to send messages. The default is 100ms. Enter a number and press return. +The test will ask how fast you would like to send messages. The default is 100ms. Enter a number and press return. ``` ? Send interval (ms) 100 ``` -5. Next, the test will ask you to select a MIDI port. Select the Bluefruit52 from the list. +Next, the test will ask you to select a MIDI port. Select the Bluefruit52 from the list. ``` ? Select port Bluefruit52 Bluetooth ``` -6. The test will begin sending messages at the specified interval. It will send 128 note on and 128 note off messages. +The test will begin sending messages at the specified interval. It will send 128 note on and 128 note off messages. ``` midi on [================================================================================================================================] 128/128 midi off [================================================================================================================================] 128/128 ``` -7. Check the Arduino Serial monitor and confirm that you have received all 128 note on and 128 note off messages. +Check the Arduino Serial monitor and confirm that you have received all 128 note on and 128 note off messages. ``` note on count: 128 note off count: 128 ``` -If you have, the test passed. +If you have 128 of each, the test passed. [node]: https://nodejs.org/ From 3c70953abd8b2448476f31c79ef191f5229a987b Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 9 Mar 2017 15:17:00 -0500 Subject: [PATCH 07/12] add node_modules to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e6b60a17..aa361b66 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ /tools/nrfutil-0.5.2/dist/ /tools/nrfutil-0.5.2/nrfutil.egg-info/ /tools/.idea/ +/tools/midi_tests/node_modules .DS_Store *.swp From e914684140f4e01b12d90fc78b8823d8dec414df Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 9 Mar 2017 15:30:51 -0500 Subject: [PATCH 08/12] add MIDI tx test docs --- tools/midi_tests/README.md | 42 ++++++++++++++++++++++++++++++++++++-- tools/midi_tests/rx.js | 4 ++-- tools/midi_tests/tx.js | 4 ++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/tools/midi_tests/README.md b/tools/midi_tests/README.md index b7d205c0..42f00ffb 100644 --- a/tools/midi_tests/README.md +++ b/tools/midi_tests/README.md @@ -20,6 +20,8 @@ $ npm install ## RX Test +This will test receiving MIDI messages on the nRF52 feather. + * Upload the `rx_arduino` example to your nRF52 feather. * Open the serial monitor, and connect to the feather on OS X using Audio MIDI Setup. * Once you are connected, navigate to the `tools/midi_tests` folder and run the `rx.js` test. @@ -44,8 +46,8 @@ Next, the test will ask you to select a MIDI port. Select the Bluefruit52 from t The test will begin sending messages at the specified interval. It will send 128 note on and 128 note off messages. ``` -midi on [================================================================================================================================] 128/128 -midi off [================================================================================================================================] 128/128 +note on [===============================================================] 128/128 +note off [===============================================================] 128/128 ``` Check the Arduino Serial monitor and confirm that you have received all 128 note on and 128 note off messages. @@ -57,4 +59,40 @@ note off count: 128 If you have 128 of each, the test passed. +## TX Test + +This will test sending MIDI messages from the nRF52 feather. + +* Upload the `tx_arduino` example to your nRF52 feather. +* Open the serial monitor, and connect to the feather on OS X using Audio MIDI Setup. +* Once you are connected, navigate to the `tools/midi_tests` folder and run the `tx.js` test. + +```console +$ cd tools/midi_tests +$ node tx.js +``` + +The test will ask you to select a MIDI port. Select the Bluefruit52 from the list. + +``` +? Select port Bluefruit52 Bluetooth +``` + +In the Arduino Serial Monitor, you will see a message like the one seen below. Press **space** and then **return** to start the test. + +``` +Start the node TX test and press a key in the serial monitor to start the test... +``` + +The nRF52 will now start sending MIDI messages to the test. Check the counters and make sure you receive all 128 note on and 128 note off messages. + +``` +$ node tx.js +? Select port Bluefruit52 Bluetooth +note on [===============================================================] 128/128 +note off [===============================================================] 128/128 +``` + +If you have 128 of each, the test passed. + [node]: https://nodejs.org/ diff --git a/tools/midi_tests/rx.js b/tools/midi_tests/rx.js index f537bd20..4f689db0 100644 --- a/tools/midi_tests/rx.js +++ b/tools/midi_tests/rx.js @@ -36,12 +36,12 @@ inquirer.prompt([ function startTest() { - var on_bar = progress.newBar('midi on [:bar] :current/:total', { + var on_bar = progress.newBar('note on [:bar] :current/:total', { incomplete: ' ', total: 128 }); - var off_bar = progress.newBar('midi off [:bar] :current/:total', { + var off_bar = progress.newBar('note off [:bar] :current/:total', { incomplete: ' ', total: 128 }); diff --git a/tools/midi_tests/tx.js b/tools/midi_tests/tx.js index 29da9933..4c83742d 100644 --- a/tools/midi_tests/tx.js +++ b/tools/midi_tests/tx.js @@ -24,12 +24,12 @@ inquirer.prompt([ function startTest() { - var on_bar = multi.newBar('midi on [:bar] :current/:total', { + var on_bar = multi.newBar('note on [:bar] :current/:total', { incomplete: ' ', total: 128 }); - var off_bar = multi.newBar('midi off [:bar] :current/:total', { + var off_bar = multi.newBar('note off [:bar] :current/:total', { incomplete: ' ', total: 128, callback: function() { From 8beebadb3bd2babbf6b92f330689c881e72a5812 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Fri, 10 Mar 2017 10:57:02 -0500 Subject: [PATCH 09/12] add MIDI rx test reset --- tools/midi_tests/rx_arduino/rx_arduino.ino | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tools/midi_tests/rx_arduino/rx_arduino.ino b/tools/midi_tests/rx_arduino/rx_arduino.ino index 4db065dc..5602f6e3 100644 --- a/tools/midi_tests/rx_arduino/rx_arduino.ino +++ b/tools/midi_tests/rx_arduino/rx_arduino.ino @@ -34,7 +34,6 @@ void setup() Bluefruit.begin(); Bluefruit.setName("Bluefruit52"); - blemidi.setWriteCallback(handleWrite); MIDI.begin(MIDI_CHANNEL_OMNI); MIDI.setHandleNoteOn(handleNoteOn); MIDI.setHandleNoteOff(handleNoteOff); @@ -67,9 +66,14 @@ void handleNoteOff(byte channel, byte pitch, byte velocity) Serial.println(off_count, DEC); } -void handleWrite() { - // Tell the MIDI instance that we have new data. +void loop() { + + if(Serial.available()) { + Serial.read(); + off_count = 0; + on_count = 0; + Serial.println("Test reset."); + } + MIDI.read(); } - -void loop() {MIDI.read();} From b599404933e9c994ee65cb80542565fea6e3d332 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Fri, 10 Mar 2017 11:00:35 -0500 Subject: [PATCH 10/12] bump MIDI FIFO to 256 --- libraries/Bluefruit52Lib/src/services/BLEMidi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.h b/libraries/Bluefruit52Lib/src/services/BLEMidi.h index 6688e9ee..34eef547 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.h +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.h @@ -44,7 +44,7 @@ #define MIDI_CREATE_BLE_INSTANCE(midiService) MIDI_CREATE_INSTANCE(BLEMidi, midiService, MIDI) -#define BLE_MIDI_DEFAULT_FIFO_DEPTH 128 +#define BLE_MIDI_DEFAULT_FIFO_DEPTH 256 extern const uint8_t BLEMIDI_UUID_SERVICE[]; extern const uint8_t BLEMIDI_UUID_CHR_IO[]; From 89d88920c40e1d228f3c59a6c113aae6764e7cca Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Fri, 10 Mar 2017 11:00:51 -0500 Subject: [PATCH 11/12] don't require a MIDI write callback --- libraries/Bluefruit52Lib/src/services/BLEMidi.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp index 50b3a09f..d5d3769e 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -198,7 +198,6 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len) void BLEMidi::setWriteCallback(midi_write_cb_t fp) { _write_cb = fp; - _io.setWriteCallback(blemidi_write_cb); } void BLEMidi::begin(int baudrate) @@ -209,6 +208,8 @@ void BLEMidi::begin(int baudrate) err_t BLEMidi::begin(void) { + _io.setWriteCallback(blemidi_write_cb); + VERIFY_STATUS( this->addToGatt() ); // IO characteristic From 4077e29482fcc3c8942ad0b513d98a2f95d409b7 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Fri, 10 Mar 2017 11:38:56 -0500 Subject: [PATCH 12/12] add non-blocking delay to MIDI example --- .../examples/Peripheral/blemidi/blemidi.ino | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino b/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino index 09ffabb2..14447f05 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino @@ -31,6 +31,9 @@ byte note_sequence[] = { 56,61,64,68,74,78,81,86,90,93,98,102 }; +// Variable to hold the last time we sent a note +unsigned long previousSend = 0; + void setup() { @@ -48,9 +51,6 @@ void setup() bledis.setModel("Bluefruit Feather52"); bledis.begin(); - // Set BLE write callback - blemidi.setWriteCallback(handleWrite); - // Initialize MIDI, and listen to all MIDI channels MIDI.begin(MIDI_CHANNEL_OMNI); @@ -92,11 +92,6 @@ void handleNoteOff(byte channel, byte pitch, byte velocity) Serial.println(); } -void handleWrite() { - // Tell the MIDI instance that we have new data. - MIDI.read(); -} - void loop() { @@ -110,6 +105,17 @@ void loop() return; } + // read any new MIDI messages + MIDI.read(); + + // Store the current time + unsigned long now = millis(); + + // Check if enough time has passed since last send + if (now - previousSend < 286) { + return; + } + // Setup variables for the current and previous // positions in the note sequence. int current = position; @@ -135,7 +141,7 @@ void loop() position = 0; } - // Wait before sending next note - delay(286); + // Log the send time + previousSend = now; }