From 255b1896051bc9ba1c56035d56a03a090108a99c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 8 Mar 2017 15:19:11 +0700 Subject: [PATCH] implement BLEMidi as Serial to use with Arduino MIDI library --- cores/nRF5/debug.cpp | 64 --------- .../examples/blemidi/blemidi.ino | 42 +++--- .../Bluefruit52Lib/src/services/BLEMidi.cpp | 131 ++++++++++++------ .../Bluefruit52Lib/src/services/BLEMidi.h | 25 +++- 4 files changed, 133 insertions(+), 129 deletions(-) diff --git a/cores/nRF5/debug.cpp b/cores/nRF5/debug.cpp index 7ee6ed81..04cdc229 100644 --- a/cores/nRF5/debug.cpp +++ b/cores/nRF5/debug.cpp @@ -90,69 +90,6 @@ int dbgHeapUsed(void) return (mallinfo()).uordblks; } -#if 0 -static void printCenter(const char* str, int width) -{ - int lpad = (width-strlen(str))/2; - int rpad = width - strlen(str) - lpad; - - Serial.printf("|%*c%s%*c|\n", lpad, ' ', str, rpad, ' '); -} - -static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint32_t used) -{ - const int WIDTH = 21; - - Serial.printf(" _____________________ 0x%08lX\n", top); - Serial.printf("| |\n"); - - printCenter(name, WIDTH); - - char buffer[30]; - if ( used ) - { - sprintf(buffer, "%lu / %lu (%02lu%%)", used, top-bottom, (used*100)/ (top-bottom)); - }else - { - sprintf(buffer, "%lu", top-bottom); - } - printCenter(buffer, WIDTH); - - - Serial.printf("|_____________________| 0x%08lX\n", bottom); -} - -void dbgMemInfo(void) -{ - Serial.println(" Memory Map"); - - // Pritn SRAM used for Stack executed by S132 and ISR - printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), 0); - - // Print Heap usage overall (including memory malloced to tasks) - printMemRegion("Heap", ((uint32_t) __HeapLimit), ((uint32_t) __HeapBase), dbgHeapUsed()); - - // DATA + BSS - printMemRegion("Data & Bss", ((uint32_t) __bss_end__), ((uint32_t) __data_start__), 0); - - // Print SRAM Used by SoftDevice - printMemRegion("S132", (uint32_t) __data_start__, 0x20000000, 0); - - Serial.println(); - - // Print Task list - uint32_t tasknum = uxTaskGetNumberOfTasks(); - char* buf = (char*) rtos_malloc(tasknum*40); // 40 bytes per task - - vTaskList(buf); - - Serial.println("Task State Prio Stack Num"); - Serial.println("-----------------------------------"); - Serial.println(buf); - rtos_free(buf); -} -#else - static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint32_t used) { char buffer[30]; @@ -198,7 +135,6 @@ void dbgMemInfo(void) Serial.println(buf); rtos_free(buf); } -#endif void dbgPrintVersion(void) { diff --git a/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino b/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino index a098b4fe..6f968fe3 100644 --- a/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino +++ b/libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino @@ -12,6 +12,7 @@ any redistribution *********************************************************************/ #include +#include // To test: // - Run this sketch and open the Serial Monitor @@ -24,19 +25,25 @@ 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.autoConnLed(true); - Bluefruit.begin(); - Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + // Configure and Start Device Information Service bledis.setManufacturer("Adafruit Industries"); @@ -59,6 +66,9 @@ void setup() */ /* Bluefruit.setConnInterval(9, 12); */ + // Initialize MIDI on channel 4 + MIDI.begin(4); + // Set up Advertising Packet setupAdv(); @@ -91,25 +101,13 @@ void loop() { if ( Bluefruit.connected() && blemidi.notifyEnabled() ) { - static int current_note = 60; - - // send note on - blemidi.send(0x90, current_note, 0x64); - delay(500); - - ledToggle(LED_BUILTIN); - - // send note off - blemidi.send(0x80, current_note, 0x64); - delay(500); - - // increment note pitch - current_note++; - - // only do one octave - if(current_note > 72) + //if (MIDI.read()) // If we have received a message { - current_note = 60; + //ledOn(LED_BUILTIN); + 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 + //ledOff(LED_BUILTIN); } } } diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp index afa8aaae..ae94b03a 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -114,8 +114,8 @@ VERIFY_STATIC ( sizeof(midi_n_event_packet_t) == 19 ); /*------------------------------------------------------------------*/ /* IMPLEMENTATION *------------------------------------------------------------------*/ -BLEMidi::BLEMidi(void) - : BLEService(BLEMIDI_UUID_SERVICE), _io(BLEMIDI_UUID_CHR_IO) +BLEMidi::BLEMidi(uint16_t fifo_depth) + : BLEService(BLEMIDI_UUID_SERVICE), _io(BLEMIDI_UUID_CHR_IO), _rxd_fifo(fifo_depth, 1) { _write_cb = NULL; } @@ -125,62 +125,68 @@ bool BLEMidi::notifyEnabled(void) return Bluefruit.connBonded() && _io.notifyEnabled(); } + void blemidi_write_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset) { (void) offset; - if ( len < 3 ) return; BLEMidi& midi_svc = (BLEMidi&) chr.parentService(); - if ( midi_svc._write_cb ) + midi_svc._write_handler(data, len); +} + +void BLEMidi::_write_handler(uint8_t* data, uint16_t len) +{ + // First 3 bytes is always : Header + Timestamp + Status + midi_header_t header; + uint16_t tstamp = 0; + uint8_t status = 0; + + header.byte = *data++; + len--; + + while (len) { - // First 3 bytes is always : Header + Timestamp + Status - midi_header_t header; - uint16_t tstamp = 0; - uint8_t status = 0; + /* event : 0x00 - 0x7F + status: 0x80 - 0xEF + sysex : 0xF0 - 0xFF + */ - header.byte = *data++; - len--; - - while (len) + if ( bitRead(data[0], 7) ) { - /* event : 0x00 - 0x7F - status: 0x80 - 0xEF - sysex : 0xF0 - 0xFF - */ + // Start of new full event + midi_timestamp_t timestamp; - if ( bitRead(data[0], 7) ) - { - // Start of new full event - midi_timestamp_t timestamp; + timestamp.byte = *data++; + len--; - timestamp.byte = *data++; - len--; + tstamp = (header.timestamp_hi << 7) | timestamp.timestamp_low; - tstamp = (header.timestamp_hi << 7) | timestamp.timestamp_low; + status = *data++; + len--; - status = *data++; - len--; + // Status must have 7th-bit set, otherwise something is wrong !!! + if ( !bitRead(status, 7) ) return; - // Status must have 7th-bit set, otherwise something is wrong !!! - if ( !bitRead(status, 7) ) return; + uint8_t tempbuf[3] = { status, data[0], data[1] }; - uint8_t tempbuf[3] = { status, data[0], data[1] }; - midi_svc._write_cb( tstamp, tempbuf); + _rxd_fifo.write(tempbuf, 3); + if ( _write_cb ) _write_cb( tstamp, tempbuf); - len -= 2; - data += 2; - } - else - { - // Running event - uint8_t tempbuf[3] = { status, data[0], data[1] }; - midi_svc._write_cb( tstamp, tempbuf); + len -= 2; + data += 2; + } + else + { + // Running event + uint8_t tempbuf[3] = { status, data[0], data[1] }; - len -= 2; - data += 2; - } + _rxd_fifo.write(tempbuf, 3); + if ( _write_cb ) _write_cb( tstamp, tempbuf); + + len -= 2; + data += 2; } } } @@ -207,6 +213,51 @@ err_t BLEMidi::start(void) return ERROR_NONE; } +/*------------------------------------------------------------------*/ +/* Stream API + *------------------------------------------------------------------*/ +int BLEMidi::read ( void ) +{ + uint8_t ch; + return _rxd_fifo.read(&ch) ? (int) ch : EOF; +} + +size_t BLEMidi::write ( uint8_t b ) +{ + // MIDI Library will write event byte by byte. Locally buffered + // Until we gather all 3 bytes + static uint8_t count = 0; + static uint8_t buf[3] = { 0 }; + + // Not support SysEx now + buf[count++] = b; + + if ( count == 3 ) + { + count = 0; + + send(buf); + } + + return 1; +} + +int BLEMidi::available ( void ) +{ + return _rxd_fifo.count(); +} + +int BLEMidi::peek ( void ) +{ + uint8_t ch; + return _rxd_fifo.peek(&ch) ? (int) ch : EOF; +} + +void BLEMidi::flush ( void ) +{ + _rxd_fifo.clear(); +} + /*------------------------------------------------------------------*/ /* Send Event *------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.h b/libraries/Bluefruit52Lib/src/services/BLEMidi.h index aa01940c..c01201fc 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.h +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.h @@ -42,6 +42,10 @@ #include "BLECharacteristic.h" #include "BLEService.h" +#define MIDI_CREATE_BLE_INSTANCE(midiService) MIDI_CREATE_INSTANCE(BLEMidi, midiService, MIDI) + +#define BLE_MIDI_DEFAULT_FIFO_DEPTH 64 + extern const uint8_t BLEMIDI_UUID_SERVICE[]; extern const uint8_t BLEMIDI_UUID_CHR_IO[]; @@ -56,15 +60,18 @@ enum MIDI_TYPE_PITCH_WHEEL = 0xE, }; -class BLEMidi: public BLEService +class BLEMidi: public BLEService, public Stream { public: typedef void (*midi_write_cb_t) (uint32_t tstamp, uint8_t data[]); - BLEMidi(void); + BLEMidi(uint16_t fifo_depth = BLE_MIDI_DEFAULT_FIFO_DEPTH); virtual err_t start(void); + // MidiInterface + void begin(int baudrate) { (void) baudrate; } + bool notifyEnabled(void); err_t send(uint8_t data[]); @@ -72,10 +79,22 @@ class BLEMidi: public BLEService void setWriteCallback(midi_write_cb_t fp); + // Stream API for MIDI Interface + virtual int read ( void ); + virtual size_t write ( uint8_t b ); + virtual int available ( void ); + virtual int peek ( void ); + virtual void flush ( void ); + + using Print::write; // pull in write(str) and write(buf, size) from Print + private: BLECharacteristic _io; - midi_write_cb_t _write_cb; + Adafruit_FIFO _rxd_fifo; + midi_write_cb_t _write_cb; + + void _write_handler(uint8_t* data, uint16_t len); friend void blemidi_write_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset); };