|
| 1 | +/** |
| 2 | + * -------------------------------------------------------- |
| 3 | + * This example shows how to use client MidiBLE |
| 4 | + * Client BLEMIDI works im a similar way Server (Common) BLEMIDI, but with some exception. |
| 5 | + * |
| 6 | + * The most importart exception is read() method. This function works as usual, but |
| 7 | + * now it manages machine-states BLE connection too. The |
| 8 | + * read() function must be called several times continuously in order to scan BLE device |
| 9 | + * and connect with the server. In this example, read() is called in a "multitask function of |
| 10 | + * FreeRTOS", but it can be called in loop() function as usual. |
| 11 | + * |
| 12 | + * Some BLEMIDI_CREATE_INSTANCE() are added in MidiBLE-Client to be able to choose a specific server to connect |
| 13 | + * or to connect to the first server which has the MIDI characteristic. You can choose the server by typing in the name field |
| 14 | + * the name of the server or the BLE address of the server. If you want to connect |
| 15 | + * to the first MIDI server BLE found by the device, you just have to set the name field empty (""). |
| 16 | + * |
| 17 | + * FOR ADVANCED USERS: Other advanced BLE configurations can be changed in hardware/BLEMIDI_Client_ESP32.h |
| 18 | + * #defines in the head of the file (IMPORTANT: Only the first user defines must be modified). These configurations |
| 19 | + * are related to security (password, pairing and securityCallback()), communication params, the device name |
| 20 | + * and other stuffs. Modify defines at your own risk. |
| 21 | + * |
| 22 | + * |
| 23 | + * |
| 24 | + * @auth RobertoHE |
| 25 | + * -------------------------------------------------------- |
| 26 | + */ |
| 27 | + |
| 28 | +#include <Arduino.h> |
| 29 | +#include <BLEMIDI_Transport.h> |
| 30 | + |
| 31 | +#include <hardware/BLEMIDI_Client_ESP32.h> |
| 32 | + |
| 33 | +//#include <hardware/BLEMIDI_ESP32_NimBLE.h> |
| 34 | +//#include <hardware/BLEMIDI_ESP32.h> |
| 35 | +//#include <hardware/BLEMIDI_nRF52.h> |
| 36 | +//#include <hardware/BLEMIDI_ArduinoBLE.h> |
| 37 | + |
| 38 | +BLEMIDI_CREATE_DEFAULT_INSTANCE(); //Connect to first server found |
| 39 | + |
| 40 | +//BLEMIDI_CREATE_INSTANCE("",MIDI) //Connect to the first server found |
| 41 | +//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server |
| 42 | +//BLEMIDI_CREATE_INSTANCE("MyBLEserver",MIDI) //Connect to a specific name server |
| 43 | + |
| 44 | +#ifndef LED_BUILTIN |
| 45 | +#define LED_BUILTIN 2 //modify for match with yout board |
| 46 | +#endif |
| 47 | + |
| 48 | +void ReadCB(void *parameter); //Continuos Read function (See FreeRTOS multitasks) |
| 49 | + |
| 50 | +unsigned long t0 = millis(); |
| 51 | +bool isConnected = false; |
| 52 | + |
| 53 | +/** |
| 54 | + * ----------------------------------------------------------------------------- |
| 55 | + * When BLE is connected, LED will turn on (indicating that connection was successful) |
| 56 | + * When receiving a NoteOn, LED will go out, on NoteOff, light comes back on. |
| 57 | + * This is an easy and conveniant way to show that the connection is alive and working. |
| 58 | + * ----------------------------------------------------------------------------- |
| 59 | +*/ |
| 60 | +void setup() |
| 61 | +{ |
| 62 | + Serial.begin(115200); |
| 63 | + MIDI.begin(MIDI_CHANNEL_OMNI); |
| 64 | + |
| 65 | + BLEMIDI.setHandleConnected([]() |
| 66 | + { |
| 67 | + Serial.println("---------CONNECTED---------"); |
| 68 | + isConnected = true; |
| 69 | + digitalWrite(LED_BUILTIN, HIGH); |
| 70 | + }); |
| 71 | + |
| 72 | + BLEMIDI.setHandleDisconnected([]() |
| 73 | + { |
| 74 | + Serial.println("---------NOT CONNECTED---------"); |
| 75 | + isConnected = false; |
| 76 | + digitalWrite(LED_BUILTIN, LOW); |
| 77 | + }); |
| 78 | + |
| 79 | + MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) |
| 80 | + { |
| 81 | + Serial.print("NoteON: CH: "); |
| 82 | + Serial.print(channel); |
| 83 | + Serial.print(" | "); |
| 84 | + Serial.print(note); |
| 85 | + Serial.print(", "); |
| 86 | + Serial.println(velocity); |
| 87 | + digitalWrite(LED_BUILTIN, LOW); |
| 88 | + }); |
| 89 | + MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) |
| 90 | + { |
| 91 | + digitalWrite(LED_BUILTIN, HIGH); |
| 92 | + }); |
| 93 | + |
| 94 | + xTaskCreatePinnedToCore(ReadCB, //See FreeRTOS for more multitask info |
| 95 | + "MIDI-READ", |
| 96 | + 3000, |
| 97 | + NULL, |
| 98 | + 1, |
| 99 | + NULL, |
| 100 | + 1); //Core0 or Core1 |
| 101 | + |
| 102 | + pinMode(LED_BUILTIN, OUTPUT); |
| 103 | + digitalWrite(LED_BUILTIN, LOW); |
| 104 | +} |
| 105 | + |
| 106 | +// ----------------------------------------------------------------------------- |
| 107 | +// |
| 108 | +// ----------------------------------------------------------------------------- |
| 109 | +void loop() |
| 110 | +{ |
| 111 | + //MIDI.read(); // This function is called in the other task |
| 112 | + |
| 113 | + if (isConnected && (millis() - t0) > 1000) |
| 114 | + { |
| 115 | + t0 = millis(); |
| 116 | + |
| 117 | + MIDI.sendNoteOn(60, 100, 1); // note 60, velocity 100 on channel 1 |
| 118 | + vTaskDelay(250/portTICK_PERIOD_MS); |
| 119 | + MIDI.sendNoteOff(60, 0, 1); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * This function is called by xTaskCreatePinnedToCore() to perform a multitask execution. |
| 125 | + * In this task, read() is called every millisecond (approx.). |
| 126 | + * read() function performs connection, reconnection and scan-BLE functions. |
| 127 | + * Call read() method repeatedly to perform a successfull connection with the server |
| 128 | + * in case connection is lost. |
| 129 | +*/ |
| 130 | +void ReadCB(void *parameter) |
| 131 | +{ |
| 132 | +// Serial.print("READ Task is started on core: "); |
| 133 | +// Serial.println(xPortGetCoreID()); |
| 134 | + for (;;) |
| 135 | + { |
| 136 | + MIDI.read(); |
| 137 | + vTaskDelay(1 / portTICK_PERIOD_MS); //Feed the watchdog of FreeRTOS. |
| 138 | + //Serial.println(uxTaskGetStackHighWaterMark(NULL)); //Only for debug. You can see the watermark of the free resources assigned by the xTaskCreatePinnedToCore() function. |
| 139 | + } |
| 140 | + vTaskDelay(1); |
| 141 | +} |
0 commit comments