1// Bluetooth lab B7 Music. uv run pio run -d labs/ble-lab-b7 -e ble-lab-b7 -t upload2// The board becomes a Bluetooth MIDI instrument. MIDI carries notes, not sound: GarageBand on a3// Mac/iPhone/iPad, any synth app on Android, or the ble-app Music tab in Chrome makes the sound4// (and can send it on to your JBL speaker). Play with the button, five touch wires, serial5// commands, a drum machine; notes played on the other side light the board's piano and LED.6//7// Wiring: the screen from lesson 03. Optional: the push button on GPIO 5 (does what BOOT does),8// and jumper wires on GPIO 1, 2, 6, 13, 14 as five touch keys (`touch on`). 13 and 14 are the9// RC522's RST/IRQ wires from the RFID track: unplug the reader first, or use the other three.10//11// Views: keys · sequencer · roll · link · log12// keys, sequencer, roll: the button plays. click = next note (sequencer: next beat),13// double-click = chord (sequencer: start/stop), hold = all notes off, hold 2 s = next view.14// link, log: click = next view.15// Serial: help lists the commands. Shared code: kit/lib/ble-kit (midi_ble.h = the packet format).16#include "b7_views.h"17#include "ble_lab.h"18#include "midi_ble.h"19#include "midi_service.h"20#include "music.h"21#include "server_link.h"22#include "touch_pads.h"23 24namespace {25 26ble::ServerLink links;27b7::MidiService midiOut;28b7::Scheduler sched;29b7::Screen screen;30constexpr uint8_t kPadPins[5] = {1, 2, 6, 13, 14};31TouchPads<5> pads(kPadPins);32uint8_t padNote[5] = {};33int walk = 0; // the button's position in the scale34uint32_t nextStepAt = 0; // the drum machine's next sixteenth35uint32_t tuneEndsAt = 0, tuneStartedAt = 0;36uint32_t lastInAt = 0; // last note from the synth side (LED)37uint32_t connectedAt = 0; // to notice a new connection38bool warnedDeaf = false;39uint32_t holdSince = 0;40uint8_t holdPin = 0;41 42constexpr int kSeqView = 1, kLinkView = 3;43 44uint32_t beatMs() { return 60000u / uint32_t(screen.bpm); }45 46void note(uint8_t n, uint8_t vel, uint32_t lengthMs, uint32_t startIn = 0, int channel = -1) {47 const uint8_t ch = uint8_t(channel < 0 ? screen.channel : channel);48 const uint32_t at = millis() + startIn;49 sched.at(at, midi::noteOn(ch, n, vel, 0));50 sched.at(at + lengthMs, midi::noteOff(ch, n, 0));51}52 53void panic() {54 sched.clear();55 screen.seqOn = false;56 screen.tune = nullptr;57 const uint16_t ts = millis() & 0x1FFF;58 const midi::Msg off[2] = {midi::allNotesOff(uint8_t(screen.channel), ts), midi::allNotesOff(b7::kDrumChannel, ts)};59 midiOut.send(off, 2);60 memset(screen.outBits, 0, sizeof(screen.outBits));61 for (b7::RollNote &r : screen.roll)62 if (!r.incoming && !r.off) r.off = millis();63 BLE_NOTE(Info, "all notes off");64}65 66void chord() {67 const int count = b7::scaleInfo(screen.scale).count;68 const int root = walk % count;69 for (int k = 0; k < 3; k++) note(b7::degreeNote(screen.scale, screen.octave, root + k * 2), 96, 600);70 BLE_NOTE(Tx, "chord on degree %d", root + 1);71}72 73void stepNote() {74 const int count = b7::scaleInfo(screen.scale).count;75 walk = (walk + 1) % (count * 2); // up two octaves, then start again76 note(b7::degreeNote(screen.scale, screen.octave, walk), 100, 300);77}78 79// Send everything that's due in one go: a run of notes leaves in as few packets as fit.80void flush() {81 midi::Msg due[24];82 const int n = sched.due(millis(), due, 24);83 if (!n) return;84 for (int i = 0; i < n; i++) {85 const midi::Msg &m = due[i];86 if (midi::isNoteOn(m)) screen.setNote(m.d1, false, true);87 else if (midi::isNoteOff(m)) screen.setNote(m.d1, false, false);88 }89 if (!midiOut.send(due, size_t(n)) && !warnedDeaf) {90 warnedDeaf = true;91 BLE_NOTE(Warn, "no synth listening: notes stay on the screen");92 }93}94 95// ---- Views ------------------------------------------------------------------------------------96 97void drawKeys(BleLab &, FrameBuffer &fb, float t) { b7v::keys(fb, screen, t); }98void drawSeq(BleLab &, FrameBuffer &fb, float t) { b7v::sequencer(fb, screen, t); }99void drawRoll(BleLab &, FrameBuffer &fb, float t) { b7v::roll(fb, screen, t); }100void drawLink(BleLab &lab, FrameBuffer &fb, float t) { b7v::link(fb, lab.state(), screen, t); }101void drawLog(BleLab &, FrameBuffer &fb, float) { bv::log(fb); }102 103const BleView kViews[] = {104 {"keys", drawKeys, "chord"},105 {"sequencer", drawSeq, "seq"},106 {"roll", drawRoll, "chord"},107 {"link", drawLink, nullptr},108 {"log", drawLog, nullptr},109};110 111// ---- Commands ---------------------------------------------------------------------------------112 113void cmdPlay(BleLab &lab, const String &arg, const String &) {114 if (arg.isEmpty()) return lab.warn("play <notes>: play C4 E4 G4 · a chord: C4+E4+G4 · a rest: -");115 uint32_t at = 0;116 int from = 0, count = 0;117 while (from < int(arg.length())) {118 int sp = arg.indexOf(' ', from);119 if (sp < 0) sp = arg.length();120 const String token = arg.substring(from, sp);121 from = sp + 1;122 if (token.isEmpty()) continue;123 if (token != "-") {124 int a = 0;125 while (a < int(token.length())) {126 int plus = token.indexOf('+', a);127 if (plus < 0) plus = token.length();128 const int n = midi::parseNote(token.substring(a, plus).c_str());129 if (n < 0) return lab.warn("'%s' is not a note: C4, F#3, Bb2 (middle C = C4)", token.substring(a, plus).c_str());130 note(uint8_t(n), 100, beatMs() * 9 / 10, at);131 count++;132 a = plus + 1;133 }134 }135 at += beatMs();136 }137 BLE_NOTE(Info, "playing %d notes at %d bpm", count, screen.bpm);138}139 140void cmdTune(BleLab &lab, const String &, const String &lower) {141 const b7::Tune *tune = b7::findTune(lower);142 if (!tune) return lab.warn("tune twinkle | ode | frere");143 const uint32_t eighth = beatMs() / 2;144 uint32_t at = 0;145 const uint8_t c = uint8_t((screen.octave + 1) * 12);146 for (int i = 0; i < tune->count; i++) {147 const b7::Step &st = tune->steps[i];148 if (st.note >= 0) note(uint8_t(c + st.note), 96, st.eighths * eighth * 9 / 10, at);149 at += st.eighths * eighth;150 }151 screen.tune = tune->name;152 tuneStartedAt = millis();153 tuneEndsAt = millis() + at;154 screen.tuneLen = tune->count;155 BLE_NOTE(Info, "tune %s: %d notes, %.1f s", tune->name, tune->count, at / 1000.0f);156}157 158void cmdSeq(BleLab &lab, const String &, const String &lower) {159 if (!lower.isEmpty() && lower != "on" && lower != "off") return lab.warn("seq | seq on | seq off");160 screen.seqOn = lower.isEmpty() ? !screen.seqOn : lower == "on";161 if (screen.seqOn && !screen.beat) screen.beat = b7::findBeat("rock");162 if (screen.seqOn) {163 screen.step = 0;164 nextStepAt = millis();165 } else {166 sched.clear(b7::kDrumChannel);167 }168 BLE_NOTE(Info, "drums %s: %s at %d bpm", screen.seqOn ? "on" : "off", screen.beat ? screen.beat->name : "no beat", screen.bpm);169}170 171void cmdBpm(BleLab &lab, const String &arg, const String &) {172 long n = 0;173 if (!BleLab::number(arg, 40, 220, n)) return lab.warn("bpm <40-220> (now %d)", screen.bpm);174 screen.bpm = int(n);175 BLE_NOTE(Info, "%d bpm: a sixteenth is %lu ms", screen.bpm, static_cast<unsigned long>(15000 / n));176}177 178void cmdBeat(BleLab &lab, const String &, const String &lower) {179 const b7::Beat *b = b7::findBeat(lower);180 if (!b) return lab.warn("beat rock | funk | house | boom");181 screen.beat = b;182 lab.showView("sequencer");183 BLE_NOTE(Info, "beat %s%s", b->name, screen.seqOn ? "" : " (seq on to start)");184}185 186void cmdOctave(BleLab &lab, const String &arg, const String &) {187 long n = 0;188 if (!BleLab::number(arg, 1, 7, n)) return lab.warn("octave <1-7> (now %d; 4 = middle C)", screen.octave);189 screen.octave = int(n);190}191 192void cmdChannel(BleLab &lab, const String &arg, const String &) {193 long n = 0;194 if (!BleLab::number(arg, 1, 16, n)) return lab.warn("channel <1-16> (now %d; 10 = drums)", screen.channel + 1);195 screen.channel = int(n - 1);196}197 198void cmdScale(BleLab &lab, const String &, const String &lower) {199 if (lower == "pent" || lower == "pentatonic") screen.scale = b7::Scale::Pentatonic;200 else if (lower == "minor") screen.scale = b7::Scale::Minor;201 else if (lower == "major") screen.scale = b7::Scale::Major;202 else if (lower == "blues") screen.scale = b7::Scale::Blues;203 else return lab.warn("scale pent | minor | major | blues (the button walks it)");204 walk = 0;205 BLE_NOTE(Info, "scale %s", b7::scaleInfo(screen.scale).name);206}207 208void cmdChord(BleLab &, const String &, const String &) { chord(); }209void cmdStop(BleLab &, const String &, const String &) { panic(); }210 211void cmdTouch(BleLab &lab, const String &, const String &lower) {212 if (lower != "on" && lower != "off") return lab.warn("touch on | touch off (wires on GPIO 1 2 6 13 14, hands off while it measures)");213 pads.enable(lower == "on");214 screen.touchOn = pads.on();215 if (pads.on()) BLE_NOTE(Good, "touch on: 5 keys, pentatonic from C%d", screen.octave);216 else BLE_NOTE(Info, "touch off");217}218 219const BleCommand kCommands[] = {220⋯ 11 lines of data folded away. Show them231};232 233// ---- Hooks ------------------------------------------------------------------------------------234 235void setup(BleLab &lab) {236 // Macs and iPhones may ask to pair a MIDI device: allow it, Just Works with Secure Connections.237 NimBLEDevice::setSecurityAuth(true, false, true);238 NimBLEServer *server = NimBLEDevice::createServer();239 server->setCallbacks(&links, false);240 midiOut.begin(server);241 server->start();242 midiOut.advertise(lab.spec().deviceName);243}244 245void tick(BleLab &lab) {246 const uint32_t now = millis();247 const ble::LinkState ls = ble::link().get();248 // A new connection: ask for a short interval, 7.5-15 ms. MIDI wants low latency.249 if (ls.radio == ble::Radio::Connected && ls.connectedAt != connectedAt) {250 connectedAt = ls.connectedAt;251 warnedDeaf = false;252 NimBLEDevice::getServer()->updateConnParams(ls.peer.handle, 6, 12, 0, 200);253 BLE_NOTE(Tx, "asked for a 7.5-15 ms interval (low latency)");254 }255 256 // The drum machine: one sixteenth per step.257 if (screen.seqOn && screen.beat && int32_t(now - nextStepAt) >= 0) {258 for (int r = 0; r < 4; r++)259 if ((screen.beat->rows[r] >> screen.step) & 1)260 note(b7::kDrumNotes[r], r == 2 ? 70 : 110, 60, 0, b7::kDrumChannel);261 screen.step = (screen.step + 1) % 16;262 nextStepAt += 15000u / uint32_t(screen.bpm);263 if (int32_t(now - nextStepAt) > 200) nextStepAt = now; // fell behind: don't burst264 }265 if (screen.tune && int32_t(now - tuneEndsAt) > 0) screen.tune = nullptr;266 267 // Touch keys: down = note on, up = note off. A firmer touch raises the reading more: velocity.268 uint32_t released = 0;269 const uint32_t pressed = pads.poll(released);270 for (int i = 0; i < 5; i++) {271 screen.padDown[i] = pads.on() && pads.down(i);272 screen.padRise[i] = pads.rise(i);273 if ((pressed >> i) & 1) {274 padNote[i] = b7::degreeNote(b7::Scale::Pentatonic, screen.octave, i);275 const uint8_t vel = uint8_t(constrain(50 + pads.rise(i), 50, 127));276 sched.at(now, midi::noteOn(uint8_t(screen.channel), padNote[i], vel, 0));277 }278 if ((released >> i) & 1) sched.at(now, midi::noteOff(uint8_t(screen.channel), padNote[i], 0));279 }280 281 flush();282 283 // What the other side plays: the piano strip, the roll, and the LED in the note's colour.284 midi::Msg in[16];285 const int n = midiOut.drain(in, 16);286 for (int i = 0; i < n; i++) {287 const midi::Msg &m = in[i];288 if (midi::isNoteOn(m)) {289 screen.setNote(m.d1, true, true);290 lab.claimLed(true);291 const uint16_t c = hsv(uint8_t((m.d1 % 12) * 21), 255, 255);292 const uint8_t level = uint8_t(m.d2 * 2);293 rgbLedWrite(PIN_RGB, uint8_t(((c >> 11) << 3) * level / 255), uint8_t((((c >> 5) & 63) << 2) * level / 255),294 uint8_t(((c & 31) << 3) * level / 255));295 lastInAt = now;296 char nm[6];297 midi::noteName(m.d1, nm, sizeof(nm));298 BLE_NOTE(Rx, "note %s vel %u ch %u", nm, m.d2, (m.status & 15) + 1);299 } else if (midi::isNoteOff(m)) {300 screen.setNote(m.d1, true, false);301 } else if ((m.status & 0xF0) == 0xB0 && m.d1 == 123) {302 memset(screen.inBits, 0, sizeof(screen.inBits));303 }304 }305 if (lastInAt && now - lastInAt > 1500) {306 lastInAt = 0;307 lab.claimLed(false);308 }309 screen.stats = midiOut.stats();310 311 // A hold in a playing view: released before 2 s = all notes off, still held = next view.312 if (holdSince) {313 if (digitalRead(holdPin) == HIGH) {314 holdSince = 0;315 panic();316 } else if (now - holdSince >= 1400) {317 holdSince = 0;318 lab.setView(lab.view() + 1);319 }320 }321}322 323bool gesture(BleLab &lab, Gesture g, bool external) {324 const int v = lab.view();325 if (v >= kLinkView) return false; // link and log: the usual click = next view326 switch (g) {327 case Gesture::Click:328 if (v == kSeqView) {329 static const char *const order[] = {"rock", "funk", "house", "boom"};330 int i = 0;331 while (i < 4 && screen.beat && strcmp(screen.beat->name, order[i]) != 0) i++;332 screen.beat = b7::findBeat(order[(i + 1) % 4]);333 BLE_NOTE(Info, "beat %s", screen.beat->name);334 } else {335 stepNote();336 }337 return true;338 case Gesture::LongPress:339 holdSince = millis();340 holdPin = external ? PIN_BUTTON : PIN_BOOT_BTN;341 return true;342 case Gesture::DoubleClick: return false; // the view's action: chord, or seq on/off343 }344 return false;345}346 347const BleLabSpec kSpec{348 "ble-lab-b7", "B7", "Music", "Bench B7", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,349 kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, tick, gesture, nullptr,350};351 352BleLab lab(kSpec);353 354} // namespace355 356void setup() { lab.begin(); }357 358void loop() { lab.loop(); }
// Bluetooth lab B7 Music. uv run pio run -d labs/ble-lab-b7 -e ble-lab-b7 -t upload
// The board becomes a Bluetooth MIDI instrument. MIDI carries notes, not sound: GarageBand on a
// Mac/iPhone/iPad, any synth app on Android, or the ble-app Music tab in Chrome makes the sound
// (and can send it on to your JBL speaker). Play with the button, five touch wires, serial
// commands, a drum machine; notes played on the other side light the board's piano and LED.
//
// Wiring: the screen from lesson 03. Optional: the push button on GPIO 5 (does what BOOT does),
// and jumper wires on GPIO 1, 2, 6, 13, 14 as five touch keys (`touch on`). 13 and 14 are the
// RC522's RST/IRQ wires from the RFID track: unplug the reader first, or use the other three.
// Views: keys · sequencer · roll · link · log
// keys, sequencer, roll: the button plays. click = next note (sequencer: next beat),
// double-click = chord (sequencer: start/stop), hold = all notes off, hold 2 s = next view.
// link, log: click = next view.
// Serial: help lists the commands. Shared code: kit/lib/ble-kit (midi_ble.h = the packet format).
#include "b7_views.h"
#include "ble_lab.h"
#include "midi_ble.h"
#include "midi_service.h"
#include "music.h"
#include "server_link.h"
#include "touch_pads.h"
namespace {
ble::ServerLink links;
b7::MidiService midiOut;
b7::Scheduler sched;
b7::Screen screen;
constexpr uint8_t kPadPins[5] = {1, 2, 6, 13, 14};
TouchPads<5> pads(kPadPins);
uint8_t padNote[5] = {};
int walk = 0; // the button's position in the scale
uint32_t nextStepAt = 0; // the drum machine's next sixteenth
uint32_t tuneEndsAt = 0, tuneStartedAt = 0;
uint32_t lastInAt = 0; // last note from the synth side (LED)
uint32_t connectedAt = 0; // to notice a new connection
bool warnedDeaf = false;
uint32_t holdSince = 0;
uint8_t holdPin = 0;
constexpr int kSeqView = 1, kLinkView = 3;
uint32_t beatMs() { return 60000u / uint32_t(screen.bpm); }
void note(uint8_t n, uint8_t vel, uint32_t lengthMs, uint32_t startIn = 0, int channel = -1) {
const uint8_t ch = uint8_t(channel < 0 ? screen.channel : channel);
const uint32_t at = millis() + startIn;
sched.at(at, midi::noteOn(ch, n, vel, 0));
sched.at(at + lengthMs, midi::noteOff(ch, n, 0));
}
void panic() {
sched.clear();
screen.seqOn = false;
screen.tune = nullptr;
const uint16_t ts = millis() & 0x1FFF;
const midi::Msg off[2] = {midi::allNotesOff(uint8_t(screen.channel), ts), midi::allNotesOff(b7::kDrumChannel, ts)};
midiOut.send(off, 2);
memset(screen.outBits, 0, sizeof(screen.outBits));
for (b7::RollNote &r : screen.roll)
if (!r.incoming && !r.off) r.off = millis();
BLE_NOTE(Info, "all notes off");
void chord() {
const int count = b7::scaleInfo(screen.scale).count;
const int root = walk % count;
for (int k = 0; k < 3; k++) note(b7::degreeNote(screen.scale, screen.octave, root + k * 2), 96, 600);
BLE_NOTE(Tx, "chord on degree %d", root + 1);
void stepNote() {
walk = (walk + 1) % (count * 2); // up two octaves, then start again
note(b7::degreeNote(screen.scale, screen.octave, walk), 100, 300);
// Send everything that's due in one go: a run of notes leaves in as few packets as fit.
void flush() {
midi::Msg due[24];
const int n = sched.due(millis(), due, 24);
if (!n) return;
for (int i = 0; i < n; i++) {
const midi::Msg &m = due[i];
if (midi::isNoteOn(m)) screen.setNote(m.d1, false, true);
else if (midi::isNoteOff(m)) screen.setNote(m.d1, false, false);
if (!midiOut.send(due, size_t(n)) && !warnedDeaf) {
warnedDeaf = true;
BLE_NOTE(Warn, "no synth listening: notes stay on the screen");
// ---- Views ------------------------------------------------------------------------------------
void drawKeys(BleLab &, FrameBuffer &fb, float t) { b7v::keys(fb, screen, t); }
void drawSeq(BleLab &, FrameBuffer &fb, float t) { b7v::sequencer(fb, screen, t); }
void drawRoll(BleLab &, FrameBuffer &fb, float t) { b7v::roll(fb, screen, t); }
void drawLink(BleLab &lab, FrameBuffer &fb, float t) { b7v::link(fb, lab.state(), screen, t); }
void drawLog(BleLab &, FrameBuffer &fb, float) { bv::log(fb); }
const BleView kViews[] = {
{"keys", drawKeys, "chord"},
{"sequencer", drawSeq, "seq"},
{"roll", drawRoll, "chord"},
{"link", drawLink, nullptr},
{"log", drawLog, nullptr},
};
// ---- Commands ---------------------------------------------------------------------------------
void cmdPlay(BleLab &lab, const String &arg, const String &) {
if (arg.isEmpty()) return lab.warn("play <notes>: play C4 E4 G4 · a chord: C4+E4+G4 · a rest: -");
uint32_t at = 0;
int from = 0, count = 0;
while (from < int(arg.length())) {
int sp = arg.indexOf(' ', from);
if (sp < 0) sp = arg.length();
const String token = arg.substring(from, sp);
from = sp + 1;
if (token.isEmpty()) continue;
if (token != "-") {
int a = 0;
while (a < int(token.length())) {
int plus = token.indexOf('+', a);
if (plus < 0) plus = token.length();
const int n = midi::parseNote(token.substring(a, plus).c_str());
if (n < 0) return lab.warn("'%s' is not a note: C4, F#3, Bb2 (middle C = C4)", token.substring(a, plus).c_str());
note(uint8_t(n), 100, beatMs() * 9 / 10, at);
count++;
a = plus + 1;
at += beatMs();
BLE_NOTE(Info, "playing %d notes at %d bpm", count, screen.bpm);
void cmdTune(BleLab &lab, const String &, const String &lower) {
const b7::Tune *tune = b7::findTune(lower);
if (!tune) return lab.warn("tune twinkle | ode | frere");
const uint32_t eighth = beatMs() / 2;
const uint8_t c = uint8_t((screen.octave + 1) * 12);
for (int i = 0; i < tune->count; i++) {
const b7::Step &st = tune->steps[i];
if (st.note >= 0) note(uint8_t(c + st.note), 96, st.eighths * eighth * 9 / 10, at);
at += st.eighths * eighth;
screen.tune = tune->name;
tuneStartedAt = millis();
tuneEndsAt = millis() + at;
screen.tuneLen = tune->count;
BLE_NOTE(Info, "tune %s: %d notes, %.1f s", tune->name, tune->count, at / 1000.0f);
void cmdSeq(BleLab &lab, const String &, const String &lower) {
if (!lower.isEmpty() && lower != "on" && lower != "off") return lab.warn("seq | seq on | seq off");
screen.seqOn = lower.isEmpty() ? !screen.seqOn : lower == "on";
if (screen.seqOn && !screen.beat) screen.beat = b7::findBeat("rock");
if (screen.seqOn) {
screen.step = 0;
nextStepAt = millis();
} else {
sched.clear(b7::kDrumChannel);
BLE_NOTE(Info, "drums %s: %s at %d bpm", screen.seqOn ? "on" : "off", screen.beat ? screen.beat->name : "no beat", screen.bpm);
void cmdBpm(BleLab &lab, const String &arg, const String &) {
long n = 0;
if (!BleLab::number(arg, 40, 220, n)) return lab.warn("bpm <40-220> (now %d)", screen.bpm);
screen.bpm = int(n);
BLE_NOTE(Info, "%d bpm: a sixteenth is %lu ms", screen.bpm, static_cast<unsigned long>(15000 / n));
void cmdBeat(BleLab &lab, const String &, const String &lower) {
const b7::Beat *b = b7::findBeat(lower);
if (!b) return lab.warn("beat rock | funk | house | boom");
screen.beat = b;
lab.showView("sequencer");
BLE_NOTE(Info, "beat %s%s", b->name, screen.seqOn ? "" : " (seq on to start)");
void cmdOctave(BleLab &lab, const String &arg, const String &) {
if (!BleLab::number(arg, 1, 7, n)) return lab.warn("octave <1-7> (now %d; 4 = middle C)", screen.octave);
screen.octave = int(n);
void cmdChannel(BleLab &lab, const String &arg, const String &) {
if (!BleLab::number(arg, 1, 16, n)) return lab.warn("channel <1-16> (now %d; 10 = drums)", screen.channel + 1);
screen.channel = int(n - 1);
void cmdScale(BleLab &lab, const String &, const String &lower) {
if (lower == "pent" || lower == "pentatonic") screen.scale = b7::Scale::Pentatonic;
else if (lower == "minor") screen.scale = b7::Scale::Minor;
else if (lower == "major") screen.scale = b7::Scale::Major;
else if (lower == "blues") screen.scale = b7::Scale::Blues;
else return lab.warn("scale pent | minor | major | blues (the button walks it)");
walk = 0;
BLE_NOTE(Info, "scale %s", b7::scaleInfo(screen.scale).name);
void cmdChord(BleLab &, const String &, const String &) { chord(); }
void cmdStop(BleLab &, const String &, const String &) { panic(); }
void cmdTouch(BleLab &lab, const String &, const String &lower) {
if (lower != "on" && lower != "off") return lab.warn("touch on | touch off (wires on GPIO 1 2 6 13 14, hands off while it measures)");
pads.enable(lower == "on");
screen.touchOn = pads.on();
if (pads.on()) BLE_NOTE(Good, "touch on: 5 keys, pentatonic from C%d", screen.octave);
else BLE_NOTE(Info, "touch off");
const BleCommand kCommands[] = {
// ---- Hooks ------------------------------------------------------------------------------------
void setup(BleLab &lab) {
// Macs and iPhones may ask to pair a MIDI device: allow it, Just Works with Secure Connections.
NimBLEDevice::setSecurityAuth(true, false, true);
NimBLEServer *server = NimBLEDevice::createServer();
server->setCallbacks(&links, false);
midiOut.begin(server);
server->start();
midiOut.advertise(lab.spec().deviceName);
void tick(BleLab &lab) {
const uint32_t now = millis();
const ble::LinkState ls = ble::link().get();
// A new connection: ask for a short interval, 7.5-15 ms. MIDI wants low latency.
if (ls.radio == ble::Radio::Connected && ls.connectedAt != connectedAt) {
connectedAt = ls.connectedAt;
warnedDeaf = false;
NimBLEDevice::getServer()->updateConnParams(ls.peer.handle, 6, 12, 0, 200);
BLE_NOTE(Tx, "asked for a 7.5-15 ms interval (low latency)");
// The drum machine: one sixteenth per step.
if (screen.seqOn && screen.beat && int32_t(now - nextStepAt) >= 0) {
for (int r = 0; r < 4; r++)
if ((screen.beat->rows[r] >> screen.step) & 1)
note(b7::kDrumNotes[r], r == 2 ? 70 : 110, 60, 0, b7::kDrumChannel);
screen.step = (screen.step + 1) % 16;
nextStepAt += 15000u / uint32_t(screen.bpm);
if (int32_t(now - nextStepAt) > 200) nextStepAt = now; // fell behind: don't burst
if (screen.tune && int32_t(now - tuneEndsAt) > 0) screen.tune = nullptr;
// Touch keys: down = note on, up = note off. A firmer touch raises the reading more: velocity.
uint32_t released = 0;
const uint32_t pressed = pads.poll(released);
for (int i = 0; i < 5; i++) {
screen.padDown[i] = pads.on() && pads.down(i);
screen.padRise[i] = pads.rise(i);
if ((pressed >> i) & 1) {
padNote[i] = b7::degreeNote(b7::Scale::Pentatonic, screen.octave, i);
const uint8_t vel = uint8_t(constrain(50 + pads.rise(i), 50, 127));
sched.at(now, midi::noteOn(uint8_t(screen.channel), padNote[i], vel, 0));
if ((released >> i) & 1) sched.at(now, midi::noteOff(uint8_t(screen.channel), padNote[i], 0));
flush();
// What the other side plays: the piano strip, the roll, and the LED in the note's colour.
midi::Msg in[16];
const int n = midiOut.drain(in, 16);
const midi::Msg &m = in[i];
if (midi::isNoteOn(m)) {
screen.setNote(m.d1, true, true);
lab.claimLed(true);
const uint16_t c = hsv(uint8_t((m.d1 % 12) * 21), 255, 255);
const uint8_t level = uint8_t(m.d2 * 2);
rgbLedWrite(PIN_RGB, uint8_t(((c >> 11) << 3) * level / 255), uint8_t((((c >> 5) & 63) << 2) * level / 255),
uint8_t(((c & 31) << 3) * level / 255));
lastInAt = now;
char nm[6];
midi::noteName(m.d1, nm, sizeof(nm));
BLE_NOTE(Rx, "note %s vel %u ch %u", nm, m.d2, (m.status & 15) + 1);
} else if (midi::isNoteOff(m)) {
screen.setNote(m.d1, true, false);
} else if ((m.status & 0xF0) == 0xB0 && m.d1 == 123) {
memset(screen.inBits, 0, sizeof(screen.inBits));
if (lastInAt && now - lastInAt > 1500) {
lastInAt = 0;
lab.claimLed(false);
screen.stats = midiOut.stats();
// A hold in a playing view: released before 2 s = all notes off, still held = next view.
if (holdSince) {
if (digitalRead(holdPin) == HIGH) {
holdSince = 0;
panic();
} else if (now - holdSince >= 1400) {
lab.setView(lab.view() + 1);
bool gesture(BleLab &lab, Gesture g, bool external) {
const int v = lab.view();
if (v >= kLinkView) return false; // link and log: the usual click = next view
switch (g) {
case Gesture::Click:
if (v == kSeqView) {
static const char *const order[] = {"rock", "funk", "house", "boom"};
int i = 0;
while (i < 4 && screen.beat && strcmp(screen.beat->name, order[i]) != 0) i++;
screen.beat = b7::findBeat(order[(i + 1) % 4]);
BLE_NOTE(Info, "beat %s", screen.beat->name);
stepNote();
return true;
case Gesture::LongPress:
holdSince = millis();
holdPin = external ? PIN_BUTTON : PIN_BOOT_BTN;
case Gesture::DoubleClick: return false; // the view's action: chord, or seq on/off
return false;
const BleLabSpec kSpec{
"ble-lab-b7", "B7", "Music", "Bench B7", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,
kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, tick, gesture, nullptr,
BleLab lab(kSpec);
} // namespace
void setup() { lab.begin(); }
void loop() { lab.loop(); }
New word #include
#include
Pastes another file in here, so this file can use what it declares.
PlatformIO environment ble-lab-b7 includes C++ source files covering MIDI messages, BLE-MIDI packets, timestamps, running status, connection interval = latency, capacitive touch, General MIDI drums.