Arduino-BLE-MIDI/examples/MidiBle/MidiBle.ino

90 lines
2.7 KiB
C++

#include <BLE-MIDI.h>
#include <hardware/BLE-MIDI_ESP32.h>
BLEMIDI_CREATE_DEFAULT_ESP32_INSTANCE()
unsigned long t0 = millis();
bool isConnected = false;
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("Booting");
MIDI.begin(1);
BLEMIDI.onConnected(OnBleMidiConnected);
BLEMIDI.onDisconnected(OnBleMidiDisconnected);
MIDI.setHandleNoteOn(OnBleMidiNoteOn);
MIDI.setHandleNoteOff(OnBleMidiNoteOff);
Serial.println(F("Ready"));
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void loop()
{
MIDI.read();
if (isConnected && (millis() - t0) > 1000)
{
t0 = millis();
MIDI.sendNoteOn(60, 127, 1); // note 60, velocity 127 on channel 1
MIDI.sendNoteOff(60, 0, 1);
}
}
// ====================================================================================
// Event handlers for incoming MIDI messages
// ====================================================================================
// -----------------------------------------------------------------------------
// rtpMIDI session. Device connected
// -----------------------------------------------------------------------------
void OnBleMidiConnected() {
Serial.println(F("Connected"));
isConnected = true;
}
// -----------------------------------------------------------------------------
// rtpMIDI session. Device disconnected
// -----------------------------------------------------------------------------
void OnBleMidiDisconnected() {
Serial.println(F("Disconnected"));
isConnected = false;
}
// -----------------------------------------------------------------------------
// received note on
// -----------------------------------------------------------------------------
void OnBleMidiNoteOn(byte channel, byte note, byte velocity) {
Serial.print(F("Incoming NoteOn from channel:"));
Serial.print(channel);
Serial.print(F(" note:"));
Serial.print(note);
Serial.print(F(" velocity:"));
Serial.print(velocity);
Serial.println();
}
// -----------------------------------------------------------------------------
// received note off
// -----------------------------------------------------------------------------
void OnBleMidiNoteOff(byte channel, byte note, byte velocity) {
Serial.print(F("Incoming NoteOff from channel:"));
Serial.print(channel);
Serial.print(F(" note:"));
Serial.print(note);
Serial.print(F(" velocity:"));
Serial.print(velocity);
Serial.println();
}