From 40d1768020db68efbc76cf75a39113753509575f Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 21 Feb 2017 14:27:09 +0700 Subject: [PATCH] adding BLE MIDI support --- .../examples/blemidi/blemidi.ino | 67 ++++++++++++++++ libraries/Bluefruit52Lib/src/bluefruit.h | 2 +- .../Bluefruit52Lib/src/services/BLEMidi.cpp | 78 +++++++++++++++++++ .../Bluefruit52Lib/src/services/BLEMidi.h | 61 +++++++++++++++ 4 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino create mode 100644 libraries/Bluefruit52Lib/src/services/BLEMidi.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEMidi.h diff --git a/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino b/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino new file mode 100644 index 00000000..d4317e0d --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino @@ -0,0 +1,67 @@ +/********************************************************************* + 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 + +BLEDis bledis; +BLEMidi blemidi; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Bluefruit52 BLEMIDI Example"); + + // 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.begin(); + Bluefruit.setName("Bluefruit52"); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.setHardwareRev("Rev E"); + bledis.setFirmwareRev("0.1.0"); + bledis.start(); + + // Configure and Start BLE MIDI Service + blemidi.start(); + + // Set up Advertising Packet + setupAdv(); + + // Start Advertising + Bluefruit.startAdvertising(); +} + +void setupAdv(void) +{ + //Bluefruit.addAdvTxPower(); + Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.addAdvTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.addAdvService(blemidi); + + // There is no room for Name in Advertising packet + // Use Scan response for Name + Bluefruit.addScanRespName(); +} + +void loop() +{ + +} diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 4dc9cc0a..fc629238 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -50,7 +50,7 @@ #include "services/BLEBeacon.h" #include "services/BLEHidGeneric.h" #include "services/BLEHidAdafruit.h" - +#include "services/BLEMidi.h" #define BLE_MAX_CHARS 50 diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp new file mode 100644 index 00000000..5429d6d5 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -0,0 +1,78 @@ +/**************************************************************************/ +/*! + @file BLEMidi.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "bluefruit.h" + +/* MIDI Service: 03B80E5A-EDE8-4B33-A751-6CE34EC4C700 + * MIDI I/O : 7772E5DB-3868-4112-A1A9-F2669D106BF3 + */ + +const uint8_t BLEMIDI_UUID_SERVICE[] = +{ + 0x00, 0xC7, 0xC4, 0x4E, 0xE3, 0x6C, 0x51, 0xA7, + 0x33, 0x4B, 0xE8, 0xED, 0x5A, 0x0E, 0xB8, 0x03 +}; + +const uint8_t BLEMIDI_UUID_CHR_IO[] = +{ + 0xF3, 0x6B, 0x10, 0x9D, 0x66, 0xF2, 0xA9, 0xA1, + 0x12, 0x41, 0x68, 0x38, 0xD8, 0xE5, 0x72, 0x77 +}; + +BLEMidi::BLEMidi(void) + : BLEService(BLEMIDI_UUID_SERVICE), _io(BLEMIDI_UUID_CHR_IO) +{ + +} + +void blemidi_write_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request) +{ + +} + +err_t BLEMidi::start(void) +{ + VERIFY_STATUS( this->addToGatt() ); + + // IO characteristic + _io.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP | CHR_PROPS_NOTIFY); + _io.setPermission(SECMODE_ENC_NO_MITM, SECMODE_ENC_NO_MITM); + _io.setWriteCallback(blemidi_write_cb); + + VERIFY_STATUS( _io.start() ); + + return ERROR_NONE; +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.h b/libraries/Bluefruit52Lib/src/services/BLEMidi.h new file mode 100644 index 00000000..8d2e068b --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.h @@ -0,0 +1,61 @@ +/**************************************************************************/ +/*! + @file BLEMidi.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef BLEMIDI_H_ +#define BLEMIDI_H_ + +#include "bluefruit_common.h" +#include "utility/adafruit_fifo.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +extern const uint8_t BLEMIDI_UUID_SERVICE[]; +extern const uint8_t BLEMIDI_UUID_CHR_IO[]; + +class BLEMidi: public BLEService +{ + public: + BLEMidi(void); + + virtual err_t start(void); + + private: + BLECharacteristic _io; + + friend class blemidi_write_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request); +}; + +#endif /* BLEMIDI_H_ */