Skip to content

Commit 862d211

Browse files
committed
clean up add connect/disconnect callback
1 parent 0317b59 commit 862d211

File tree

4 files changed

+166
-3
lines changed

4 files changed

+166
-3
lines changed

libraries/Bluefruit52Lib/examples/blemidi/blemidi.ino

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ void setupAdv(void)
6363

6464
void loop()
6565
{
66-
digitalToggle(LED_BUILTIN);
67-
delay(1000);
66+
if ( Bluefruit.connected() && blemidi.configured() )
67+
{
68+
static int current_note = 60;
69+
70+
// send note on
71+
blemidi.send(0x90, current_note, 0x64);
72+
delay(500);
73+
74+
// send note off
75+
blemidi.send(0x80, current_note, 0x64);
76+
delay(500);
77+
78+
// increment note pitch
79+
current_note++;
80+
81+
// only do one octave
82+
if(current_note > 72)
83+
{
84+
current_note = 60;
85+
}
86+
}
87+
}
88+
89+
void connect_callback(void)
90+
{
91+
Serial.println("Connected");
92+
}
93+
94+
void disconnect_callback(uint8_t reason)
95+
{
96+
(void) reason;
97+
98+
Serial.println("Disconnected");
99+
Serial.println("Bluefruit will auto start advertising (default)");
68100
}

libraries/Bluefruit52Lib/examples/bleuart/bleuart.ino

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BLEBas blebas;
2020
#define STATUS_LED (17)
2121
#define BLINKY_MS (2000)
2222

23-
int blinkyms;
23+
uint32_t blinkyms;
2424

2525
void setup()
2626
{
@@ -39,6 +39,8 @@ void setup()
3939

4040
Bluefruit.begin();
4141
Bluefruit.setName("Bluefruit52");
42+
Bluefruit.setConnectCallback(connect_callback);
43+
Bluefruit.setDisconnectCallback(disconnect_callback);
4244

4345
// Configure and Start Device Information Service
4446
bledis.setManufacturer("Adafruit Industries");
@@ -102,3 +104,16 @@ void loop()
102104
Serial.write(ch);
103105
}
104106
}
107+
108+
void connect_callback(void)
109+
{
110+
Serial.println("Connected");
111+
}
112+
113+
void disconnect_callback(uint8_t reason)
114+
{
115+
(void) reason;
116+
117+
Serial.println("Disconnected");
118+
Serial.println("Bluefruit will auto start advertising (default)");
119+
}

libraries/Bluefruit52Lib/src/services/BLEMidi.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,68 @@ const uint8_t BLEMIDI_UUID_CHR_IO[] =
5252
0x12, 0x41, 0x68, 0x38, 0xD8, 0xE5, 0x72, 0x77
5353
};
5454

55+
/*------------------------------------------------------------------*/
56+
/* MIDI Data Type
57+
*------------------------------------------------------------------*/
58+
typedef union ATTR_PACKED
59+
{
60+
struct {
61+
uint8_t timestamp_hi : 6;
62+
uint8_t : 1;
63+
uint8_t start_bit : 1;
64+
};
65+
66+
uint8_t byte;
67+
} midi_header_t;
68+
69+
VERIFY_STATIC ( sizeof(midi_header_t) == 1 );
70+
71+
typedef union ATTR_PACKED
72+
{
73+
struct {
74+
uint8_t timestamp_low : 7;
75+
uint8_t start_bit : 1;
76+
};
77+
78+
uint8_t byte;
79+
} midi_timestamp_t;
80+
81+
VERIFY_STATIC ( sizeof(midi_timestamp_t) == 1 );
82+
83+
typedef union ATTR_PACKED
84+
{
85+
struct {
86+
uint8_t channel : 4;
87+
uint8_t type : 4;
88+
};
89+
90+
uint8_t byte;
91+
} midi_status_t;
92+
93+
VERIFY_STATIC ( sizeof(midi_status_t) == 1 );
94+
95+
typedef struct ATTR_PACKED
96+
{
97+
midi_header_t header;
98+
midi_timestamp_t timestamp;
99+
uint8_t data[3];
100+
} midi_event_packet_t;
101+
102+
VERIFY_STATIC ( sizeof(midi_event_packet_t) == 5 );
103+
104+
typedef struct ATTR_PACKED
105+
{
106+
midi_header_t header;
107+
midi_timestamp_t timestamp;
108+
midi_status_t status;
109+
uint8_t data[16];
110+
} midi_n_event_packet_t;
111+
112+
VERIFY_STATIC ( sizeof(midi_n_event_packet_t) == 19 );
113+
114+
/*------------------------------------------------------------------*/
115+
/* IMPLEMENTATION
116+
*------------------------------------------------------------------*/
55117
BLEMidi::BLEMidi(void)
56118
: BLEService(BLEMIDI_UUID_SERVICE), _io(BLEMIDI_UUID_CHR_IO)
57119
{
@@ -63,6 +125,41 @@ void blemidi_write_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request)
63125

64126
}
65127

128+
bool BLEMidi::configured()
129+
{
130+
return _io.notifyEnabled();
131+
}
132+
133+
err_t BLEMidi::send(uint8_t data[])
134+
{
135+
uint32_t tstamp = millis();
136+
137+
midi_event_packet_t event =
138+
{
139+
.header = {{
140+
.timestamp_hi = (uint8_t) ((tstamp & 0x1F80UL) >> 7),
141+
.start_bit = 1
142+
}},
143+
144+
.timestamp = {{
145+
.timestamp_low = (uint8_t) (tstamp & 0x7FUL),
146+
.start_bit = 1
147+
}}
148+
};
149+
150+
memcpy(event.data, data, 3);
151+
152+
VERIFY_STATUS( _io.notify(data, 3) );
153+
154+
return ERROR_NONE;
155+
}
156+
157+
err_t BLEMidi::send(uint8_t status, uint8_t byte1, uint8_t byte2)
158+
{
159+
uint8_t data[] = { status, byte1, byte2 };
160+
return send(data);
161+
}
162+
66163
err_t BLEMidi::start(void)
67164
{
68165
VERIFY_STATUS( this->addToGatt() );

libraries/Bluefruit52Lib/src/services/BLEMidi.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,36 @@
4545
extern const uint8_t BLEMIDI_UUID_SERVICE[];
4646
extern const uint8_t BLEMIDI_UUID_CHR_IO[];
4747

48+
enum
49+
{
50+
MIDI_TYPE_NOTE_OFF = 0x8,
51+
MIDI_TYPE_NOTE_ON = 0x9,
52+
MIDI_TYPE_AFTER_TOUCH = 0xA,
53+
MIDI_TYPE_CONTROL_CHANGE = 0xB,
54+
MIDI_TYPE_PROGRAM_CHANGE = 0xC,
55+
MIDI_TYPE_CHANNEL_PRESSURE = 0xD,
56+
MIDI_TYPE_PITCH_WHEEL = 0xE,
57+
};
58+
4859
class BLEMidi: public BLEService
4960
{
5061
public:
5162
BLEMidi(void);
5263

5364
virtual err_t start(void);
5465

66+
bool configured();
67+
68+
69+
err_t send(uint8_t data[]);
70+
err_t send(uint8_t status, uint8_t byte1, uint8_t byte2);
71+
5572
private:
5673
BLECharacteristic _io;
5774

5875
friend void blemidi_write_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request);
5976
};
6077

78+
79+
6180
#endif /* BLEMIDI_H_ */

0 commit comments

Comments
 (0)