diff --git a/examples/Esp32_NoteOnOffEverySec/Esp32_NoteOnOffEverySec.ino b/examples/Esp32_NoteOnOffEverySec/Esp32_NoteOnOffEverySec.ino index 7192f76..7a51d3a 100644 --- a/examples/Esp32_NoteOnOffEverySec/Esp32_NoteOnOffEverySec.ino +++ b/examples/Esp32_NoteOnOffEverySec/Esp32_NoteOnOffEverySec.ino @@ -1,6 +1,9 @@ +// Select your device +#define ESP32 + #include "BleMidi.h" -BLEMIDI_CREATE_INSTANCE(BLEMIDI); +BLEMIDI_CREATE_INSTANCE(bm); // ----------------------------------------------------------------------------- // @@ -13,6 +16,11 @@ void setup() ; // wait for serial port to connect. Needed for Leonardo only } + bm.begin("hehe"); + + bm.onConnected(OnBleMidiConnected); + bm.onDisconnected(OnBleMidiDisconnected); + Serial.print(F("Getting IP address...")); } @@ -23,3 +31,21 @@ void loop() { } + +// ==================================================================================== +// Event handlers for incoming MIDI messages +// ==================================================================================== + +// ----------------------------------------------------------------------------- +// rtpMIDI session. Device connected +// ----------------------------------------------------------------------------- +void OnBleMidiConnected() { + Serial.print(F("Connected")); +} + +// ----------------------------------------------------------------------------- +// rtpMIDI session. Device disconnected +// ----------------------------------------------------------------------------- +void OnBleMidiDisconnected() { + Serial.println(F("Disconnected")); +} diff --git a/src/BleMidi.h b/src/BleMidi.h index aa8bfd9..8418f7a 100644 --- a/src/BleMidi.h +++ b/src/BleMidi.h @@ -4,24 +4,9 @@ #pragma once +#include "utility/BleMidi_Settings.h" #include "utility/BleMidi_Defs.h" -BEGIN_BLEMIDI_NAMESPACE - -/*! \brief The main class for AppleMidiInterface handling.\n - See member descriptions to know how to use it, - or check out the examples supplied with the library. - */ -class BleMidiInterface -{ -public: - // Constructor and Destructor - inline BleMidiInterface(); - inline ~BleMidiInterface(); -}; - -END_BLEMIDI_NAMESPACE - -// ----------------------------------------------------------------------------- - -#include "BleMidi.hpp" +#if defined(ESP32) +#include "Ble_esp32.h" +#endif diff --git a/src/BleMidi.hpp b/src/BleMidi.hpp deleted file mode 100644 index 9327dcb..0000000 --- a/src/BleMidi.hpp +++ /dev/null @@ -1,16 +0,0 @@ -BEGIN_BLEMIDI_NAMESPACE - -/*! \brief Default constructor for MIDI_Class. */ -inline BleMidiInterface::BleMidiInterface() -{ -} - -/*! \brief Default destructor for MIDI_Class. - - This is not really useful for the Arduino, as it is never called... - */ -inline BleMidiInterface::~BleMidiInterface() -{ -} - -END_BLEMIDI_NAMESPACE diff --git a/src/Ble_esp32.h b/src/Ble_esp32.h new file mode 100644 index 0000000..a6d3a68 --- /dev/null +++ b/src/Ble_esp32.h @@ -0,0 +1,101 @@ +#pragma once + +#include +#include +#include + +#define SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700" +#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3" + +BEGIN_BLEMIDI_NAMESPACE + +class BleMidiInterface; + +class MyServerCallbacks: public BLEServerCallbacks { +public: + MyServerCallbacks(BleMidiInterface* bleMidiInterface) { + _bleMidiInterface = bleMidiInterface; + } +protected: + BleMidiInterface* _bleMidiInterface; + + void onConnect(BLEServer* pServer) { + // _bleMidiInterface->mConnectedCallback(); + }; + + void onDisconnect(BLEServer* pServer) { + // _bleMidiInterface->mDisconnectedCallback(); + } +}; + +class MyCallbacks: public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic *pCharacteristic) { + } +}; + +class BleMidiInterface +{ +protected: + // ESP32 + BLEServer *pServer; + BLEAdvertising *pAdvertising; + BLECharacteristic *pCharacteristic; + + void(*mConnectedCallback)(); + void(*mDisconnectedCallback)(); + +public: + inline BleMidiInterface() + { + mConnectedCallback = NULL; + mDisconnectedCallback = NULL; + } + + inline ~BleMidiInterface() + { + } + + inline bool begin(const char* deviceName) + { + BLEDevice::init(deviceName); + + pServer = BLEDevice::createServer(); + pServer->setCallbacks(new MyServerCallbacks(this)); + + // Create the BLE Service + BLEService* pService = pServer->createService(BLEUUID(SERVICE_UUID)); + + // Create a BLE Characteristic + pCharacteristic = pService->createCharacteristic( + BLEUUID(CHARACTERISTIC_UUID), + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE | + BLECharacteristic::PROPERTY_NOTIFY | + BLECharacteristic::PROPERTY_WRITE_NR + ); + pCharacteristic->setCallbacks(new MyCallbacks()); + // Start the service + pService->start(); + + BLEAdvertisementData advertisementData = BLEAdvertisementData(); + advertisementData.setFlags(0x04); + advertisementData.setCompleteServices(BLEUUID(SERVICE_UUID)); + advertisementData.setName(deviceName); + + // Start advertising + pAdvertising = pServer->getAdvertising(); + pAdvertising->setAdvertisementData(advertisementData); + pAdvertising->start(); + + return true; + } + + inline void onConnected(void(*fptr)()) { + mConnectedCallback = fptr; + } + inline void onDisconnected(void(*fptr)()) { + mDisconnectedCallback = fptr; + } +}; + +END_BLEMIDI_NAMESPACE diff --git a/src/utility/.DS_Store b/src/utility/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/src/utility/.DS_Store differ diff --git a/src/utility/BleMidi_Defs.h b/src/utility/BleMidi_Defs.h index 5433b5c..405e0a3 100644 --- a/src/utility/BleMidi_Defs.h +++ b/src/utility/BleMidi_Defs.h @@ -19,9 +19,9 @@ BEGIN_BLEMIDI_NAMESPACE BLEMIDI_NAMESPACE::BleMidiInterface Name; -/*! \brief Create an instance of the library with EnternetUDP. +/*! \brief */ #define BLEMIDI_CREATE_DEFAULT_INSTANCE() \ - BLEMIDI_CREATE_INSTANCE(BleMIDI); + BLEMIDI_CREATE_INSTANCE(bm); END_BLEMIDI_NAMESPACE