91 lines
2.5 KiB
C++
91 lines
2.5 KiB
C++
#include <BLE-MIDI.h>
|
|
//#include <hardware/MIDI_ESP32_NimBLE.h>
|
|
#include <hardware/MIDI_ESP32.h>
|
|
//#include <hardware/MIDI_nRF52.h>
|
|
//#include <hardware/MIDI_ArduinoBLE.h>
|
|
|
|
BLEMIDI_CREATE_DEFAULT_INSTANCE()
|
|
|
|
unsigned long t0 = millis();
|
|
unsigned long t1 = millis();
|
|
bool isConnected = false;
|
|
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// -----------------------------------------------------------------------------
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
while (!Serial);
|
|
|
|
MIDI.begin();
|
|
|
|
Serial.println("booting!");
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
BLEMIDI.setHandleConnected(OnConnected);
|
|
BLEMIDI.setHandleDisconnected(OnDisconnected);
|
|
|
|
MIDI.setHandleNoteOn(OnNoteOn);
|
|
MIDI.setHandleNoteOff(OnNoteOff);
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// -----------------------------------------------------------------------------
|
|
void loop()
|
|
{
|
|
MIDI.read();
|
|
|
|
if (isConnected && (millis() - t0) > 1000)
|
|
{
|
|
t0 = millis();
|
|
|
|
MIDI.sendNoteOn (60, 100, 1); // note 60, velocity 127 on channel 1
|
|
}
|
|
if (isConnected && (millis() - t1) > 1250)
|
|
{
|
|
t1 = millis();
|
|
|
|
MIDI.sendNoteOff(60, 0, 1);
|
|
}
|
|
}
|
|
|
|
// ====================================================================================
|
|
// Event handlers for incoming MIDI messages
|
|
// ====================================================================================
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Device connected
|
|
// -----------------------------------------------------------------------------
|
|
void OnConnected() {
|
|
isConnected = true;
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
Serial.println("connected!");
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Device disconnected
|
|
// -----------------------------------------------------------------------------
|
|
void OnDisconnected() {
|
|
isConnected = false;
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
Serial.println("disconnected!");
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Received note on
|
|
// -----------------------------------------------------------------------------
|
|
void OnNoteOn(byte channel, byte note, byte velocity) {
|
|
Serial.println("note on");
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Received note off
|
|
// -----------------------------------------------------------------------------
|
|
void OnNoteOff(byte channel, byte note, byte velocity) {
|
|
Serial.println("note off");
|
|
}
|