1// Bluetooth lab B3 Connect & talk. uv run pio run -d labs/ble-lab-b3 -e ble-lab-b3 -t upload2// The board becomes a GATT server: a small database your phone connects to. In nRF Connect or3// LightBlue you set the on-board LED's colour, write a message onto the screen, read the chip's4// temperature and subscribe to the button. Every operation lights its row on the gatt view.5//6// Wiring: the screen from lesson 03. The push button on GPIO 5 is optional (it does what BOOT does).7//8// Views: gatt · message · link · log (click = next view, double-click = the action in the footer)9// Every gesture is also sent to a subscribed phone as a Button notification.10// Serial: help lists the commands. Shared code: kit/lib/ble-kit (bench_service.h is the GATT table).11#include "bench_views.h"12#include "ble_lab.h"13#include "server_link.h"14 15namespace {16 17ble::ServerLink links;18bench::BenchService bench_;19 20void drawGatt(BleLab &, FrameBuffer &fb, float t) { bv::gatt(fb, bench_.state(), t); }21void drawMessage(BleLab &, FrameBuffer &fb, float t) { bv::message(fb, bench_.state(), t); }22void drawLink(BleLab &lab, FrameBuffer &fb, float t) { bv::link(fb, lab.state(), t); }23void drawLog(BleLab &, FrameBuffer &fb, float) { bv::log(fb); }24 25const BleView kViews[] = {26 {"gatt", drawGatt, "press"},27 {"message", drawMessage, "press"},28 {"link", drawLink, "params fast"},29 {"log", drawLog, nullptr},30};31 32// ---- Commands ---------------------------------------------------------------------------------33 34void cmdAdv(BleLab &lab, const String &, const String &lower) {35 if (lower != "on" && lower != "off") return lab.warn("adv on | adv off");36 NimBLEAdvertising *adv = NimBLEDevice::getAdvertising();37 if (lower == "on") return bench::advertise(lab.spec().deviceName);38 adv->stop();39 ble::link().edit([](ble::LinkState &s) {40 s.advertising = false;41 if (s.radio == ble::Radio::Advertising) s.radio = ble::Radio::Off;42 });43 BLE_NOTE(Info, "advertising off: invisible to phones");44}45 46void cmdMessage(BleLab &lab, const String &arg, const String &) {47 if (arg.isEmpty()) return lab.warn("message <text>: sets the Message value from the board's side");48 bench_.setMessage(arg.c_str());49 lab.showView("message");50 BLE_NOTE(Info, "Message set here: a phone that reads it now gets it");51}52 53void cmdPress(BleLab &, const String &, const String &) { bench_.button(1); }54 55void cmdDisconnect(BleLab &lab, const String &, const String &) {56 const ble::LinkState s = ble::link().get();57 if (s.radio != ble::Radio::Connected) return lab.warn("nobody is connected");58 NimBLEDevice::getServer()->disconnect(s.peer.handle);59}60 61// The peripheral may only ASK; the central (the phone) decides. iPhones accept 15 ms and up.62void cmdParams(BleLab &lab, const String &, const String &lower) {63 const ble::LinkState s = ble::link().get();64 if (s.radio != ble::Radio::Connected) return lab.warn("connect a phone first");65 // Units: interval 1.25 ms, timeout 10 ms.66 if (lower == "fast") NimBLEDevice::getServer()->updateConnParams(s.peer.handle, 12, 24, 0, 400); // 15-30 ms67 else if (lower == "slow") NimBLEDevice::getServer()->updateConnParams(s.peer.handle, 80, 160, 4, 600); // 100-200 ms, skip 468 else return lab.warn("params fast | params slow (the phone may say no)");69 BLE_NOTE(Tx, "asked the phone for %s parameters", lower.c_str());70}71 72void cmdPhy(BleLab &lab, const String &, const String &lower) {73 const ble::LinkState s = ble::link().get();74 if (s.radio != ble::Radio::Connected) return lab.warn("connect a phone first");75 uint8_t mask = 0;76 if (lower == "1m") mask = BLE_GAP_LE_PHY_1M_MASK;77 else if (lower == "2m") mask = BLE_GAP_LE_PHY_2M_MASK;78 else return lab.warn("phy 1m | phy 2m (2M = twice the bit rate, a bit less range)");79 NimBLEDevice::getServer()->updatePhy(s.peer.handle, mask, mask, 0);80 BLE_NOTE(Tx, "asked for the %s PHY", lower == "2m" ? "2M" : "1M");81}82 83void cmdLed(BleLab &lab, const String &, const String &lower) {84 if (lower != "auto") return lab.warn("led auto: the LED shows the radio's state again");85 lab.claimLed(false);86 BLE_NOTE(Info, "LED shows the radio again");87}88 89const BleCommand kCommands[] = {90 {"adv", "on|off", "advertise (visible, connectable) or hide", cmdAdv},91 {"message", "<text>", "set the Message value from the board", cmdMessage},92 {"press", "", "send a Button click notification", cmdPress},93 {"params", "fast|slow", "ask the phone for another connection interval", cmdParams},94 {"phy", "1m|2m", "ask for the 1M or 2M radio mode", cmdPhy},95 {"disconnect", "", "hang up on the phone", cmdDisconnect},96 {"led", "auto", "after the phone set the LED: show radio state again", cmdLed},97};98 99// ---- Hooks ------------------------------------------------------------------------------------100 101void setup(BleLab &lab) {102 NimBLEServer *server = NimBLEDevice::createServer();103 server->setCallbacks(&links, false);104 bench_.begin(server, bench::Options{});105 server->start();106 bench::advertise(lab.spec().deviceName);107}108 109void tick(BleLab &lab) {110 bench_.tick();111 uint8_t c[3];112 if (bench_.takeLed(c)) {113 lab.claimLed(true);114 rgbLedWrite(PIN_RGB, c[0], c[1], c[2]);115 }116}117 118// Every gesture goes to the phone too; then the shell does its usual thing (return false).119bool gesture(BleLab &, Gesture g, bool) {120 bench_.button(g == Gesture::Click ? 1 : g == Gesture::DoubleClick ? 2 : 3);121 return false;122}123 124const BleLabSpec kSpec{125 "ble-lab-b3", "B3", "Connect & talk", "Bench B3", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,126 kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, tick, gesture, nullptr,127};128 129BleLab lab(kSpec);130 131} // namespace132 133void setup() { lab.begin(); }134 135void loop() { lab.loop(); }
// Bluetooth lab B3 Connect & talk. uv run pio run -d labs/ble-lab-b3 -e ble-lab-b3 -t upload
// The board becomes a GATT server: a small database your phone connects to. In nRF Connect or
// LightBlue you set the on-board LED's colour, write a message onto the screen, read the chip's
// temperature and subscribe to the button. Every operation lights its row on the gatt view.
//
// Wiring: the screen from lesson 03. The push button on GPIO 5 is optional (it does what BOOT does).
// Views: gatt · message · link · log (click = next view, double-click = the action in the footer)
// Every gesture is also sent to a subscribed phone as a Button notification.
// Serial: help lists the commands. Shared code: kit/lib/ble-kit (bench_service.h is the GATT table).
#include "bench_views.h"
#include "ble_lab.h"
#include "server_link.h"
namespace {
ble::ServerLink links;
bench::BenchService bench_;
void drawGatt(BleLab &, FrameBuffer &fb, float t) { bv::gatt(fb, bench_.state(), t); }
void drawMessage(BleLab &, FrameBuffer &fb, float t) { bv::message(fb, bench_.state(), t); }
void drawLink(BleLab &lab, FrameBuffer &fb, float t) { bv::link(fb, lab.state(), t); }
void drawLog(BleLab &, FrameBuffer &fb, float) { bv::log(fb); }
const BleView kViews[] = {
{"gatt", drawGatt, "press"},
{"message", drawMessage, "press"},
{"link", drawLink, "params fast"},
{"log", drawLog, nullptr},
};
// ---- Commands ---------------------------------------------------------------------------------
void cmdAdv(BleLab &lab, const String &, const String &lower) {
if (lower != "on" && lower != "off") return lab.warn("adv on | adv off");
NimBLEAdvertising *adv = NimBLEDevice::getAdvertising();
if (lower == "on") return bench::advertise(lab.spec().deviceName);
adv->stop();
ble::link().edit([](ble::LinkState &s) {
s.advertising = false;
if (s.radio == ble::Radio::Advertising) s.radio = ble::Radio::Off;
});
BLE_NOTE(Info, "advertising off: invisible to phones");
}
void cmdMessage(BleLab &lab, const String &arg, const String &) {
if (arg.isEmpty()) return lab.warn("message <text>: sets the Message value from the board's side");
bench_.setMessage(arg.c_str());
lab.showView("message");
BLE_NOTE(Info, "Message set here: a phone that reads it now gets it");
void cmdPress(BleLab &, const String &, const String &) { bench_.button(1); }
void cmdDisconnect(BleLab &lab, const String &, const String &) {
const ble::LinkState s = ble::link().get();
if (s.radio != ble::Radio::Connected) return lab.warn("nobody is connected");
NimBLEDevice::getServer()->disconnect(s.peer.handle);
// The peripheral may only ASK; the central (the phone) decides. iPhones accept 15 ms and up.
void cmdParams(BleLab &lab, const String &, const String &lower) {
if (s.radio != ble::Radio::Connected) return lab.warn("connect a phone first");
// Units: interval 1.25 ms, timeout 10 ms.
if (lower == "fast") NimBLEDevice::getServer()->updateConnParams(s.peer.handle, 12, 24, 0, 400); // 15-30 ms
else if (lower == "slow") NimBLEDevice::getServer()->updateConnParams(s.peer.handle, 80, 160, 4, 600); // 100-200 ms, skip 4
else return lab.warn("params fast | params slow (the phone may say no)");
BLE_NOTE(Tx, "asked the phone for %s parameters", lower.c_str());
void cmdPhy(BleLab &lab, const String &, const String &lower) {
uint8_t mask = 0;
if (lower == "1m") mask = BLE_GAP_LE_PHY_1M_MASK;
else if (lower == "2m") mask = BLE_GAP_LE_PHY_2M_MASK;
else return lab.warn("phy 1m | phy 2m (2M = twice the bit rate, a bit less range)");
NimBLEDevice::getServer()->updatePhy(s.peer.handle, mask, mask, 0);
BLE_NOTE(Tx, "asked for the %s PHY", lower == "2m" ? "2M" : "1M");
void cmdLed(BleLab &lab, const String &, const String &lower) {
if (lower != "auto") return lab.warn("led auto: the LED shows the radio's state again");
lab.claimLed(false);
BLE_NOTE(Info, "LED shows the radio again");
const BleCommand kCommands[] = {
{"adv", "on|off", "advertise (visible, connectable) or hide", cmdAdv},
{"message", "<text>", "set the Message value from the board", cmdMessage},
{"press", "", "send a Button click notification", cmdPress},
{"params", "fast|slow", "ask the phone for another connection interval", cmdParams},
{"phy", "1m|2m", "ask for the 1M or 2M radio mode", cmdPhy},
{"disconnect", "", "hang up on the phone", cmdDisconnect},
{"led", "auto", "after the phone set the LED: show radio state again", cmdLed},
// ---- Hooks ------------------------------------------------------------------------------------
void setup(BleLab &lab) {
NimBLEServer *server = NimBLEDevice::createServer();
server->setCallbacks(&links, false);
bench_.begin(server, bench::Options{});
server->start();
bench::advertise(lab.spec().deviceName);
void tick(BleLab &lab) {
bench_.tick();
uint8_t c[3];
if (bench_.takeLed(c)) {
lab.claimLed(true);
rgbLedWrite(PIN_RGB, c[0], c[1], c[2]);
// Every gesture goes to the phone too; then the shell does its usual thing (return false).
bool gesture(BleLab &, Gesture g, bool) {
bench_.button(g == Gesture::Click ? 1 : g == Gesture::DoubleClick ? 2 : 3);
return false;
const BleLabSpec kSpec{
"ble-lab-b3", "B3", "Connect & talk", "Bench B3", 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-b3 includes C++ source files covering central & peripheral, GATT: services & characteristics, UUIDs (16 & 128-bit), read · write · notify, CCCD (subscribing), MTU, connection interval.