From ebc5cce602b2a9c9168e59e7f01f156963b1286f Mon Sep 17 00:00:00 2001 From: lathoub Date: Wed, 24 Oct 2018 00:13:43 +0200 Subject: [PATCH] added first device: esp32 --- .../Esp32_NoteOnOffEverySec.ino | 28 ++++- src/BleMidi.h | 23 +--- src/BleMidi.hpp | 16 --- src/Ble_esp32.h | 101 ++++++++++++++++++ src/utility/.DS_Store | Bin 0 -> 6148 bytes src/utility/BleMidi_Defs.h | 4 +- 6 files changed, 134 insertions(+), 38 deletions(-) delete mode 100644 src/BleMidi.hpp create mode 100644 src/Ble_esp32.h create mode 100644 src/utility/.DS_Store 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 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0