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

91 lines
2.6 KiB
C++

#include <BLE-MIDI.h>
#include <hardware/ESP32.h>
//#include <hardware/nRF52.h>
BLEMIDI_CREATE_DEFAULT_INSTANCE()
unsigned long t0 = millis();
bool isConnected = false;
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("Booting");
MIDI.begin();
BLEMIDI.setHandleConnected(OnConnected);
BLEMIDI.setHandleDisconnected(OnDisconnected);
MIDI.setHandleNoteOn(OnNoteOn);
MIDI.setHandleNoteOff(OnNoteOff);
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
// ====================================================================================
// -----------------------------------------------------------------------------
// Device connected
// -----------------------------------------------------------------------------
void OnConnected() {
Serial.println(F("Connected"));
isConnected = true;
}
// -----------------------------------------------------------------------------
// Device disconnected
// -----------------------------------------------------------------------------
void OnDisconnected() {
Serial.println(F("Disconnected"));
isConnected = false;
}
// -----------------------------------------------------------------------------
// Received note on
// -----------------------------------------------------------------------------
void OnNoteOn(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 OnNoteOff(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();
}