|
| 1 | +#include <BLEMIDI_Transport.h> |
| 2 | + |
| 3 | +static uint32_t customPasskeyRequest() |
| 4 | +{ |
| 5 | + // FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY |
| 6 | + // FOR EXAMPLE: |
| 7 | + uint32_t passkey = 123456; |
| 8 | + |
| 9 | + // Serial.println("Client Passkey Request"); |
| 10 | + |
| 11 | + /** return the passkey to send to the server */ |
| 12 | + return passkey; |
| 13 | +}; |
| 14 | + |
| 15 | + struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { |
| 16 | + //See all options and them explanation in the library. |
| 17 | + |
| 18 | + /* |
| 19 | + ##### BLE DEVICE NAME ##### |
| 20 | + */ |
| 21 | + //static constexpr char *name = "BleMidiClient"; |
| 22 | + /* |
| 23 | + ###### TX POWER ##### |
| 24 | + */ |
| 25 | + //static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9; |
| 26 | + /* |
| 27 | + ###### SECURITY ##### |
| 28 | + */ |
| 29 | + //static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT; |
| 30 | + //static const bool clientBond = true; |
| 31 | + //static const bool clientMITM = false; |
| 32 | + //static const bool clientPair = true; |
| 33 | + //static constexpr PasskeyRequestCallback userOnPassKeyRequest = customPasskeyRequest; |
| 34 | + /* |
| 35 | + ###### BLE COMMUNICATION PARAMS ###### |
| 36 | + */ |
| 37 | + //static const uint16_t commMinInterval = 6; // 7.5ms |
| 38 | + //static const uint16_t commMaxInterval = 35; // 40ms |
| 39 | + //static const uint16_t commLatency = 0; // |
| 40 | + //static const uint16_t commTimeOut = 200; // 2000ms |
| 41 | + /* |
| 42 | + ###### BLE FORCE NEW CONNECTION ###### |
| 43 | + */ |
| 44 | + //static const bool forceNewConnection = false; |
| 45 | + /* |
| 46 | + ###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ###### |
| 47 | + */ |
| 48 | + //static const bool notification = true; |
| 49 | + //static const bool response = true; |
| 50 | + /* |
| 51 | + ###### AND THE OTHER SETTINGS OF MIDI LIBRARY ###### |
| 52 | + */ |
| 53 | + static const size_t MaxBufferSize = 16; |
| 54 | + |
| 55 | +}; |
| 56 | + |
| 57 | +#include <hardware/BLEMIDI_ESP32_NimBLE.h> |
| 58 | +//#include <hardware/BLEMIDI_ESP32.h> |
| 59 | +//#include <hardware/BLEMIDI_ArduinoBLE.h> |
| 60 | + |
| 61 | +#ifndef LED_BUILTIN |
| 62 | +#define LED_BUILTIN 2 |
| 63 | +#endif |
| 64 | + |
| 65 | +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings); |
| 66 | + |
| 67 | +unsigned long t0 = millis(); |
| 68 | +bool isConnected = false; |
| 69 | + |
| 70 | + |
| 71 | +// ----------------------------------------------------------------------------- |
| 72 | +// When BLE connected, LED will turn on (indication that connection was successful) |
| 73 | +// When receiving a NoteOn, LED will go out, on NoteOff, light comes back on. |
| 74 | +// This is an easy and conveniant way to show that the connection is alive and working. |
| 75 | +// ----------------------------------------------------------------------------- |
| 76 | +void setup() |
| 77 | +{ |
| 78 | + Serial.begin(115200); |
| 79 | + while (!Serial) {} |
| 80 | + Serial.println("booting"); |
| 81 | + |
| 82 | + MIDI.begin(); |
| 83 | + |
| 84 | + pinMode(LED_BUILTIN, OUTPUT); |
| 85 | + digitalWrite(LED_BUILTIN, LOW); |
| 86 | + |
| 87 | + BLEMIDI.setHandleConnected([]() { |
| 88 | + isConnected = true; |
| 89 | + digitalWrite(LED_BUILTIN, HIGH); |
| 90 | + }); |
| 91 | + |
| 92 | + BLEMIDI.setHandleDisconnected([]() { |
| 93 | + isConnected = false; |
| 94 | + digitalWrite(LED_BUILTIN, LOW); |
| 95 | + }); |
| 96 | + |
| 97 | + MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) { |
| 98 | + digitalWrite(LED_BUILTIN, LOW); |
| 99 | + }); |
| 100 | + MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) { |
| 101 | + digitalWrite(LED_BUILTIN, HIGH); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +// ----------------------------------------------------------------------------- |
| 106 | +// |
| 107 | +// ----------------------------------------------------------------------------- |
| 108 | +void loop() |
| 109 | +{ |
| 110 | + MIDI.read(); |
| 111 | + |
| 112 | + if (isConnected && (millis() - t0) > 1000) |
| 113 | + { |
| 114 | + t0 = millis(); |
| 115 | + |
| 116 | + MIDI.sendNoteOn (60, 100, 1); // note 60, velocity 100 on channel 1 |
| 117 | + } |
| 118 | +} |
0 commit comments