nrf5/modules: updating ubluepy and bluetooth driver to support python created event handler. Added registration of callback from ubluepy against the bluetooth driver and dispatching of events to the user supplied python function.
This commit is contained in:
parent
d29539a395
commit
832a7ffd14
|
@ -37,6 +37,9 @@ p.advertise(device_name="MicroPython")
|
||||||
|
|
||||||
DB setup:
|
DB setup:
|
||||||
|
|
||||||
|
def event_handler(id, length, data):
|
||||||
|
print("BLE event:", id, " length: ", length)
|
||||||
|
|
||||||
from ubluepy import Service, Characteristic, UUID, Peripheral
|
from ubluepy import Service, Characteristic, UUID, Peripheral
|
||||||
u0 = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
|
u0 = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
|
||||||
s = Service(u0)
|
s = Service(u0)
|
||||||
|
@ -44,6 +47,7 @@ u1 = UUID("6e400002-b5a3-f393-e0a9-e50e24dcca9e")
|
||||||
c = Characteristic(u1)
|
c = Characteristic(u1)
|
||||||
s.addCharacteristic(c)
|
s.addCharacteristic(c)
|
||||||
p = Peripheral()
|
p = Peripheral()
|
||||||
|
p.setConnectionHandler(event_handler)
|
||||||
p.advertise(device_name="micr", services=[s])
|
p.advertise(device_name="micr", services=[s])
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -104,6 +108,6 @@ typedef struct _ubluepy_advertise_data_t {
|
||||||
uint8_t num_of_services;
|
uint8_t num_of_services;
|
||||||
} ubluepy_advertise_data_t;
|
} ubluepy_advertise_data_t;
|
||||||
|
|
||||||
|
typedef void (*ubluepy_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t length, uint8_t * data);
|
||||||
|
|
||||||
#endif // UBLUEPY_H__
|
#endif // UBLUEPY_H__
|
||||||
|
|
|
@ -39,6 +39,25 @@ STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_pri
|
||||||
mp_printf(print, "Peripheral");
|
mp_printf(print, "Peripheral");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STATIC void event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t length, uint8_t * data) {
|
||||||
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
|
|
||||||
|
mp_obj_t args[3];
|
||||||
|
mp_uint_t num_of_args = 3;
|
||||||
|
args[0] = MP_OBJ_NEW_SMALL_INT(event_id);
|
||||||
|
args[1] = MP_OBJ_NEW_SMALL_INT(length);
|
||||||
|
if (data != NULL) {
|
||||||
|
args[2] = mp_obj_new_bytearray_by_ref(length, data);
|
||||||
|
} else {
|
||||||
|
args[2] = mp_const_none;
|
||||||
|
}
|
||||||
|
|
||||||
|
// for now hard-code all events to conn_handler
|
||||||
|
mp_call_function_n_kw(self->conn_handler, num_of_args, 0, args);
|
||||||
|
|
||||||
|
(void)self;
|
||||||
|
}
|
||||||
|
|
||||||
STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||||
enum {
|
enum {
|
||||||
ARG_NEW_DEVICE_ADDR,
|
ARG_NEW_DEVICE_ADDR,
|
||||||
|
@ -46,8 +65,8 @@ STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_
|
||||||
};
|
};
|
||||||
|
|
||||||
static const mp_arg_t allowed_args[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ ARG_NEW_DEVICE_ADDR, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
{ ARG_NEW_DEVICE_ADDR, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||||
{ ARG_NEW_ADDR_TYPE, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
{ ARG_NEW_ADDR_TYPE, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||||
};
|
};
|
||||||
|
|
||||||
// parse args
|
// parse args
|
||||||
|
@ -57,21 +76,11 @@ STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_
|
||||||
ubluepy_peripheral_obj_t *s = m_new_obj(ubluepy_peripheral_obj_t);
|
ubluepy_peripheral_obj_t *s = m_new_obj(ubluepy_peripheral_obj_t);
|
||||||
s->base.type = type;
|
s->base.type = type;
|
||||||
|
|
||||||
|
sd_event_handler_set(MP_OBJ_FROM_PTR(s), event_handler);
|
||||||
|
|
||||||
return MP_OBJ_FROM_PTR(s);
|
return MP_OBJ_FROM_PTR(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
|
||||||
static void peripheral_delegate(void) {
|
|
||||||
// delegate
|
|
||||||
mp_obj_t args[3];
|
|
||||||
mp_uint_t num_of_args = 3;
|
|
||||||
args[0] = type;
|
|
||||||
args[1] = MP_OBJ_NEW_SMALL_INT(size);
|
|
||||||
args[2] = mp_obj_new_bytearray_by_ref(size, evt_data);
|
|
||||||
mp_call_function_n_kw(delegate->handleConnection, num_of_args, 0, args);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// \method withDelegate(DefaultDelegate)
|
/// \method withDelegate(DefaultDelegate)
|
||||||
/// Set delegate instance for handling Bluetooth LE events.
|
/// Set delegate instance for handling Bluetooth LE events.
|
||||||
///
|
///
|
||||||
|
|
|
@ -41,7 +41,10 @@ if (sd_enabled() == 0) { \
|
||||||
(void)sd_enable(); \
|
(void)sd_enable(); \
|
||||||
}
|
}
|
||||||
|
|
||||||
bool m_adv_in_progress = false;
|
static bool m_adv_in_progress = false;
|
||||||
|
|
||||||
|
static ubluepy_evt_callback_t ubluepy_event_handler;
|
||||||
|
static mp_obj_t mp_observer;
|
||||||
|
|
||||||
#if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110)
|
#if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110)
|
||||||
#include "nrf_nvic.h"
|
#include "nrf_nvic.h"
|
||||||
|
@ -445,6 +448,11 @@ bool sd_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sd_event_handler_set(mp_obj_t obj, ubluepy_evt_callback_t evt_handler) {
|
||||||
|
mp_observer = obj;
|
||||||
|
ubluepy_event_handler = evt_handler;
|
||||||
|
}
|
||||||
|
|
||||||
static void ble_evt_handler(ble_evt_t * p_ble_evt) {
|
static void ble_evt_handler(ble_evt_t * p_ble_evt) {
|
||||||
// S132 event ranges.
|
// S132 event ranges.
|
||||||
// Common 0x01 -> 0x0F
|
// Common 0x01 -> 0x0F
|
||||||
|
@ -455,6 +463,7 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
|
||||||
|
|
||||||
switch (p_ble_evt->header.evt_id) {
|
switch (p_ble_evt->header.evt_id) {
|
||||||
case BLE_GAP_EVT_CONNECTED:
|
case BLE_GAP_EVT_CONNECTED:
|
||||||
|
ubluepy_event_handler(mp_observer, BLE_GAP_EVT_CONNECTED, p_ble_evt->header.evt_len - sizeof(uint16_t), NULL);
|
||||||
printf("GAP CONNECT\n");
|
printf("GAP CONNECT\n");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -49,4 +49,7 @@ bool sd_service_add(ubluepy_service_obj_t * p_service_obj);
|
||||||
bool sd_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj);
|
bool sd_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj);
|
||||||
|
|
||||||
bool sd_advertise_data(ubluepy_advertise_data_t * p_adv_params);
|
bool sd_advertise_data(ubluepy_advertise_data_t * p_adv_params);
|
||||||
|
|
||||||
|
void sd_event_handler_set(mp_obj_t obs, ubluepy_evt_callback_t evt_handler);
|
||||||
|
|
||||||
#endif // BLUETOOTH_LE_DRIVER_H__
|
#endif // BLUETOOTH_LE_DRIVER_H__
|
||||||
|
|
Loading…
Reference in New Issue