Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
app.cpp
Go to the documentation of this file.
1#ifdef ESP32
2#include <sdkconfig.h>
3#if CONFIG_BT_BLE_ENABLED
4
5#include "advertising.h"
6#include "app.h"
7#include "midi.h"
8
9namespace cs::midi_ble_bluedroid {
10namespace {
11MIDIBLEInstance *midi_ble_bluedroid_instance = nullptr;
12}
13} // namespace cs::midi_ble_bluedroid
14
15using namespace cs;
16using namespace cs::midi_ble_bluedroid;
17
18void midi_ble_instance_handle_connect(uint16_t conn_handle) {
19 if (midi_ble_bluedroid_instance)
20 midi_ble_bluedroid_instance->handleConnect(
21 BLEConnectionHandle {conn_handle});
22}
23void midi_ble_instance_handle_disconnect(uint16_t conn_handle) {
24 if (midi_ble_bluedroid_instance)
25 midi_ble_bluedroid_instance->handleDisconnect(
26 BLEConnectionHandle {conn_handle});
27}
28void midi_ble_instance_handle_mtu(uint16_t conn_handle, uint16_t mtu) {
29 if (midi_ble_bluedroid_instance)
30 midi_ble_bluedroid_instance->handleMTU(
31 BLEConnectionHandle {conn_handle}, mtu);
32}
33void midi_ble_instance_handle_subscribe(uint16_t conn_handle,
34 uint16_t char_handle, bool notify) {
35 if (midi_ble_bluedroid_instance)
36 midi_ble_bluedroid_instance->handleSubscribe(
37 BLEConnectionHandle {conn_handle},
38 BLECharacteristicHandle {char_handle}, notify);
39}
40void midi_ble_instance_handle_data(uint16_t conn_handle, const uint8_t *data,
41 uint16_t length) {
42 if (midi_ble_bluedroid_instance) {
43 BLEDataView view {data, length};
44 auto data_gen = [view {view}]() mutable {
45#if __cplusplus >= 201402
46 return std::exchange(view, {});
47#else
48 auto ret = view;
49 view = {};
50 return ret;
51#endif
52 };
53 midi_ble_bluedroid_instance->handleData(
54 BLEConnectionHandle {conn_handle},
55 BLEDataGenerator {compat::in_place, std::move(data_gen)},
57 }
58}
59
60namespace cs::midi_ble_bluedroid {
61
62bool init(MIDIBLEInstance &instance, BLESettings settings) {
63 midi_ble_bluedroid_instance = &instance;
68 return midi_init();
69}
70
71bool notify(BLEConnectionHandle conn_handle,
72 BLECharacteristicHandle char_handle, BLEDataView data) {
73 return midi_notify(conn_handle.conn, char_handle.characteristic, data.data,
74 data.length);
75}
76
77} // namespace cs::midi_ble_bluedroid
78
79#endif
80#endif
@ ConsumeImmediately
Buffer is valid only during the callback. Do not keep any pointers to it.
Advertising the MIDI service for Bluetooth Low Energy.
void advertising_set_connection_interval(uint16_t itvl_min, uint16_t itvl_max)
Set the connection interval range in the advertising data.
void midi_ble_instance_handle_data(uint16_t conn_handle, const uint8_t *data, uint16_t length)
void midi_ble_instance_handle_disconnect(uint16_t conn_handle)
void midi_ble_instance_handle_subscribe(uint16_t conn_handle, uint16_t char_handle, bool notify)
void midi_ble_instance_handle_connect(uint16_t conn_handle)
void midi_ble_instance_handle_mtu(uint16_t conn_handle, uint16_t mtu)
Callable that returns the next chunk of data from a BLE packet when called.
Definition BLEAPI.hpp:66
Defines the interface for callback functions registered by the low-level BLE code.
Definition BLEAPI.hpp:127
Public MIDI over Bluetooth Low Energy API.
bool midi_init()
Initialize the Bluetooth stack and register the MIDI BLE application with the Bluedroid driver.
void set_security_initiate_encryption(bool security_initiate_encryption_)
Configure whether the Arduino should initiate the bonding/secure connection.
void set_midi_ble_name(const char *name)
Set the name of the BLE device. Must be set before calling midi_init().
bool midi_notify(uint16_t conn_handle, uint16_t char_handle, const uint8_t *data, size_t len)
Send a MIDI BLE notification to the client.
void notify(BLEDataView data)
bool init(MIDIBLEInstance &instance, BLESettings ble_settings)
static in_place_t in_place
Definition compat.hpp:27
Represents a handle to a local GATT characteristic.
Definition BLEAPI.hpp:30
Represents a handle to the connection to another device.
Definition BLEAPI.hpp:19
Non-owning, std::span-style read-only view of BLE data.
Definition BLEAPI.hpp:42
uint16_t length
Definition BLEAPI.hpp:44
const uint8_t * data
Definition BLEAPI.hpp:43
Configuration options for the low-level BLE code.
Definition BLEAPI.hpp:150
struct BLESettings::@4 connection_interval
Connection intervals as multiples of 1.25 milliseconds (e.g.0x000C = 15 ms).
uint16_t maximum
Definition BLEAPI.hpp:157
const char * device_name
Device name (used for advertising)
Definition BLEAPI.hpp:152
uint16_t minimum
Definition BLEAPI.hpp:156
bool initiate_security
Set to true if you want the Arduino to always initiate the Bluetooth bonding or secure connection.
Definition BLEAPI.hpp:163