Skip to content

Commit a4173c4

Browse files
committed
nrf5/bluetooth: Adding webbluetooth REPL template. Alternating advertisment of eddystone URL and UART BLE service every 500 ms. Adding new config parameter to bluetooth_conf.h to enable webbluetooth repl. Has to be configured in combination with BLE_NUS. Eddystone URL not pointing to a valid WebBluetooth application at the moment, but rather to micropython.org as a placeholder for now.
1 parent 587c627 commit a4173c4

4 files changed

Lines changed: 57 additions & 14 deletions

File tree

nrf5/bluetooth/ble_uart.c

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
#if MICROPY_PY_BLE_NUS
3333

34+
#if BLUETOOTH_WEBBLUETOOTH_REPL
35+
#include "hal_time.h"
36+
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
37+
3438
static ubluepy_uuid_obj_t uuid_obj_service = {
3539
.base.type = &ubluepy_uuid_type,
3640
.type = UBLUEPY_UUID_128_BIT,
@@ -75,13 +79,19 @@ static ubluepy_peripheral_obj_t ble_uart_peripheral = {
7579
};
7680

7781
static volatile bool m_cccd_enabled;
82+
static volatile bool m_connected;
7883

7984
ringBuffer_typedef(uint8_t, ringbuffer_t);
8085

8186
static ringbuffer_t m_rx_ring_buffer;
8287
static ringbuffer_t * mp_rx_ring_buffer = &m_rx_ring_buffer;
8388
static uint8_t m_rx_ring_buffer_data[128];
8489

90+
static ubluepy_advertise_data_t m_adv_data_uart_service;
91+
92+
#if BLUETOOTH_WEBBLUETOOTH_REPL
93+
static ubluepy_advertise_data_t m_adv_data_eddystone_url;
94+
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
8595

8696
int mp_hal_stdin_rx_chr(void) {
8797
while (isBufferEmpty(mp_rx_ring_buffer)) {
@@ -127,8 +137,10 @@ STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn
127137

128138
if (event_id == 16) { // connect event
129139
self->conn_handle = conn_handle;
140+
m_connected = true;
130141
} else if (event_id == 17) { // disconnect event
131142
self->conn_handle = 0xFFFF; // invalid connection handle
143+
m_connected = false;
132144
}
133145
}
134146

@@ -195,15 +207,21 @@ void ble_uart_init0(void) {
195207
mp_uint_t num_services;
196208
mp_obj_get_array(service_list, &num_services, &services);
197209

198-
ubluepy_advertise_data_t adv_data = {
199-
.p_services = services,
200-
.num_of_services = num_services,
201-
.p_device_name = (uint8_t *)device_name,
202-
.device_name_len = strlen(device_name)
203-
};
204-
205-
(void)device_name;
206-
(void)services;
210+
m_adv_data_uart_service.p_services = services;
211+
m_adv_data_uart_service.num_of_services = num_services;
212+
m_adv_data_uart_service.p_device_name = (uint8_t *)device_name;
213+
m_adv_data_uart_service.device_name_len = strlen(device_name);
214+
m_adv_data_uart_service.connectable = true;
215+
216+
#if BLUETOOTH_WEBBLUETOOTH_REPL
217+
static uint8_t eddystone_url_data[26] = {0x2, 0x1, 0x6,
218+
0x3, 0x3, 0xaa, 0xfe,
219+
18, 0x16, 0xaa, 0xfe, 0x10, 0xee, 0x0, 'm', 'i', 'c', 'r', 'o', 'p', 'y', 't', 'h', 'o', 'n', 0x1};
220+
// eddystone url adv data
221+
m_adv_data_eddystone_url.p_data = eddystone_url_data;
222+
m_adv_data_eddystone_url.data_len = sizeof(eddystone_url_data);
223+
m_adv_data_eddystone_url.connectable = false;
224+
#endif
207225

208226
m_cccd_enabled = false;
209227

@@ -213,15 +231,33 @@ void ble_uart_init0(void) {
213231
m_rx_ring_buffer.end = 0;
214232
m_rx_ring_buffer.elems = m_rx_ring_buffer_data;
215233

216-
adv_data.connectable = true;
234+
m_connected = false;
217235

218-
(void)ble_drv_advertise_data(&adv_data);
236+
ble_uart_advertise();
237+
}
219238

220-
while (m_cccd_enabled != true) {
221-
;
239+
void ble_uart_advertise(void) {
240+
#if BLUETOOTH_WEBBLUETOOTH_REPL
241+
while (!m_connected) {
242+
(void)ble_drv_advertise_data(&m_adv_data_uart_service);
243+
mp_hal_delay_ms(500);
244+
(void)ble_drv_advertise_data(&m_adv_data_eddystone_url);
245+
mp_hal_delay_ms(500);
222246
}
247+
248+
ble_drv_advertise_stop();
249+
#else
250+
(void)ble_drv_advertise_data(&m_adv_data_uart_service);
251+
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
252+
}
253+
254+
bool ble_uart_connected(void) {
255+
return (m_connected);
223256
}
224257

258+
bool ble_uart_enabled(void) {
259+
return (m_cccd_enabled);
260+
}
225261

226262
#endif // MICROPY_PY_BLE_NUS
227263

nrf5/bluetooth/ble_uart.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@
3131
#include "ble_drv.h"
3232

3333
void ble_uart_init0(void);
34-
34+
void ble_uart_advertise(void);
35+
bool ble_uart_connected(void);
36+
bool ble_uart_enabled(void);
3537
#endif // BLUETOOTH_LE_UART_H__

nrf5/bluetooth_conf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
#define MICROPY_PY_BLE (1)
1616
#define MICROPY_PY_BLE_NUS (0)
17+
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
1718
#define MICROPY_PY_UBLUEPY (1)
1819
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
1920

2021
#elif (BLUETOOTH_SD == 132)
2122

2223
#define MICROPY_PY_BLE (1)
2324
#define MICROPY_PY_BLE_NUS (0)
25+
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
2426
#define MICROPY_PY_UBLUEPY (1)
2527
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
2628
#define MICROPY_PY_UBLUEPY_CENTRAL (0)

nrf5/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ int main(int argc, char **argv) {
197197

198198
#if MICROPY_PY_BLE_NUS
199199
ble_uart_init0();
200+
while (!ble_uart_enabled()) {
201+
;
202+
}
200203
#endif
201204

202205
for (;;) {

0 commit comments

Comments
 (0)