Skip to content

Instantly share code, notes, and snippets.

@joebowbeer
Created April 29, 2023 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebowbeer/e445a27984d16d11b1b5995b71dd8179 to your computer and use it in GitHub Desktop.
Save joebowbeer/e445a27984d16d11b1b5995b71dd8179 to your computer and use it in GitHub Desktop.
Receive BLE MIDI events on Puck.js and other Espruino devices
/// Receives MIDI events
function recv(evt) {
var data = evt.data;
var cmd = data[2] & 0xF0; // command
var chn = data[2] & 0x0F; // channel
var num = data[3]; // note or controller number
var val = data[4]; // velocity or controller value
switch(cmd) {
case 0x80: // noteOff
case 0x90: // noteOn
if (val) LED1.set(); else LED1.reset();
break;
case 0xB0: // CC
if (val) LED2.set(); else LED2.reset();
break;
default:
if (val) LED3.set(); else LED3.reset();
}
}
/// Turns the device into a MIDI controller
function init(name) {
NRF.setServices({
"03B80E5A-EDE8-4B33-A751-6CE34EC4C700": { // MIDI
"7772E5DB-3868-4112-A1A9-F2669D106BF3": {
readable: true,
writable: true,
notify: true,
value: [0x80, 0x80, 0x00, 0x00, 0x00],
onWrite: recv
}
}
});
NRF.setAdvertising([
// Flags: LE Limited Discoverable Mode, BR/EDR Not Supported
0x02, 0x01, 0x05,
// Complete Local Name: PuckCC
0x07, 0x09,
name.charCodeAt(0),
name.charCodeAt(1),
name.charCodeAt(2),
name.charCodeAt(3),
name.charCodeAt(4),
name.charCodeAt(5),
// MIDI
0x11, 0x06, 0x00, 0xC7, 0xC4, 0x4E, 0xE3, 0x6C, 0x51,
0xA7, 0x33, 0x4B, 0xE8, 0xED, 0x5A, 0x0E, 0xB8, 0x03
]);
}
init("PuckCC");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment