From db75b5535cf3bea8a0eb209a98a19a52458c1946 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Wed, 15 Feb 2017 19:30:45 +0100 Subject: [PATCH] nrf5/modules: Adding two new functions to ubluepy peripheral class to set specific handlers for notificaitons and connection related events. --- nrf5/modules/ubluepy/modubluepy.h | 2 + nrf5/modules/ubluepy/ubluepy_peripheral.c | 60 ++++++++++++++++------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/nrf5/modules/ubluepy/modubluepy.h b/nrf5/modules/ubluepy/modubluepy.h index 7754bc6918..aef5408542 100644 --- a/nrf5/modules/ubluepy/modubluepy.h +++ b/nrf5/modules/ubluepy/modubluepy.h @@ -93,6 +93,8 @@ typedef struct _ubluepy_delegate_obj_t { typedef struct _ubluepy_peripheral_obj_t { mp_obj_base_t base; mp_obj_t delegate; + mp_obj_t notif_handler; + mp_obj_t conn_handler; } ubluepy_peripheral_obj_t; typedef struct _ubluepy_advertise_data_t { diff --git a/nrf5/modules/ubluepy/ubluepy_peripheral.c b/nrf5/modules/ubluepy/ubluepy_peripheral.c index f5b488e83c..12d50fe96f 100644 --- a/nrf5/modules/ubluepy/ubluepy_peripheral.c +++ b/nrf5/modules/ubluepy/ubluepy_peripheral.c @@ -84,6 +84,30 @@ STATIC mp_obj_t peripheral_with_delegate(mp_obj_t self_in, mp_obj_t delegate) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_with_delegate_obj, peripheral_with_delegate); +/// \method setNotificationHandler(func) +/// Set handler for Bluetooth LE notification events. +/// +STATIC mp_obj_t peripheral_set_notif_handler(mp_obj_t self_in, mp_obj_t func) { + ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); + + self->notif_handler = func; + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_set_notif_handler_obj, peripheral_set_notif_handler); + +/// \method setConnectionHandler(func) +/// Set handler for Bluetooth LE connection events. +/// +STATIC mp_obj_t peripheral_set_conn_handler(mp_obj_t self_in, mp_obj_t func) { + ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); + + self->conn_handler = func; + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_set_conn_handler_obj, peripheral_set_conn_handler); + /// \method advertise(device_name, [service=[service1, service2, ...]], [data=bytearray]) /// Start advertising. /// @@ -161,27 +185,29 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_add_service_obj, peripheral_ STATIC const mp_map_elem_t ubluepy_peripheral_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_withDelegate), (mp_obj_t)(&ubluepy_peripheral_with_delegate_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_withDelegate), (mp_obj_t)(&ubluepy_peripheral_with_delegate_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_setNotificationHandler), (mp_obj_t)(&ubluepy_peripheral_set_notif_handler_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_setConnectionHandler), (mp_obj_t)(&ubluepy_peripheral_set_conn_handler_obj) }, #if MICROPY_PY_UBLUEPY_CENTRAL - { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)(&ubluepy_peripheral_connect_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)(&ubluepy_peripheral_disconnect_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_getServices), (mp_obj_t)(&ubluepy_peripheral_get_services_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_getServiceByUUID), (mp_obj_t)(&ubluepy_peripheral_get_service_by_uuid_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_getCharacteristics), (mp_obj_t)(&ubluepy_peripheral_get_chars_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_getDescriptors), (mp_obj_t)(&ubluepy_peripheral_get_descs_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_waitForNotifications), (mp_obj_t)(&ubluepy_peripheral_wait_for_notif_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_writeCharacteristic), (mp_obj_t)(&ubluepy_peripheral_write_char_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_readCharacteristic), (mp_obj_t)(&ubluepy_peripheral_read_char_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)(&ubluepy_peripheral_connect_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)(&ubluepy_peripheral_disconnect_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getServices), (mp_obj_t)(&ubluepy_peripheral_get_services_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getServiceByUUID), (mp_obj_t)(&ubluepy_peripheral_get_service_by_uuid_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getCharacteristics), (mp_obj_t)(&ubluepy_peripheral_get_chars_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getDescriptors), (mp_obj_t)(&ubluepy_peripheral_get_descs_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_waitForNotifications), (mp_obj_t)(&ubluepy_peripheral_wait_for_notif_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeCharacteristic), (mp_obj_t)(&ubluepy_peripheral_write_char_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readCharacteristic), (mp_obj_t)(&ubluepy_peripheral_read_char_obj) }, #endif #if MICROPY_PY_UBLUEPY_PERIPHERAL - { MP_OBJ_NEW_QSTR(MP_QSTR_advertise), (mp_obj_t)(&ubluepy_peripheral_advertise_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)(&ubluepy_peripheral_disconnect_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_addService), (mp_obj_t)(&ubluepy_peripheral_add_service_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_advertise), (mp_obj_t)(&ubluepy_peripheral_advertise_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)(&ubluepy_peripheral_disconnect_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_addService), (mp_obj_t)(&ubluepy_peripheral_add_service_obj) }, #if 0 - { MP_OBJ_NEW_QSTR(MP_QSTR_addCharacteristic), (mp_obj_t)(&ubluepy_peripheral_add_char_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_addDescriptor), (mp_obj_t)(&ubluepy_peripheral_add_desc_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_writeCharacteristic), (mp_obj_t)(&ubluepy_peripheral_write_char_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_readCharacteristic), (mp_obj_t)(&ubluepy_peripheral_read_char_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_addCharacteristic), (mp_obj_t)(&ubluepy_peripheral_add_char_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_addDescriptor), (mp_obj_t)(&ubluepy_peripheral_add_desc_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeCharacteristic), (mp_obj_t)(&ubluepy_peripheral_write_char_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readCharacteristic), (mp_obj_t)(&ubluepy_peripheral_read_char_obj) }, #endif #endif #if MICROPY_PY_UBLUEPY_BROADCASTER