@@ -152,17 +152,10 @@ typedef struct {
152152} pb_lwp3device_obj_t ;
153153
154154// Handles LEGO Wireless protocol messages from the Powered Up Remote.
155- static void handle_remote_notification (pbdrv_bluetooth_peripheral_t * peripheral , const uint8_t * value , uint32_t size ) {
155+ static void handle_remote_notification (void * user , const uint8_t * value , uint32_t size ) {
156156
157- // No object registered for processing notifcations.
158- if (self_obj == MP_OBJ_NULL ) {
159- return ;
160- }
161-
162- pb_lwp3device_obj_t * self = MP_OBJ_TO_PTR (self_obj );
163-
164- // It's not for us.
165- if (self -> peripheral != peripheral ) {
157+ pb_lwp3device_obj_t * self = user ;
158+ if (!self ) {
166159 return ;
167160 }
168161
@@ -306,7 +299,7 @@ static pbio_error_t pb_lwp3device_connect_thread(pbio_os_state_t *state, mp_obj_
306299 PBIO_OS_ASYNC_BEGIN (state );
307300
308301 // Get available peripheral instance.
309- pb_assert (pbdrv_bluetooth_peripheral_get_available (& self -> peripheral ));
302+ pb_assert (pbdrv_bluetooth_peripheral_get_available (& self -> peripheral , self ));
310303
311304 // Scan and connect with timeout.
312305 pb_assert (pbdrv_bluetooth_peripheral_scan_and_connect (& scan_config ));
@@ -484,8 +477,10 @@ mp_obj_t pb_type_remote_button_pressed(mp_obj_t self_in) {
484477 #endif
485478}
486479
487- mp_obj_t pb_lwp3device_close (mp_obj_t self_in ) {
488- self_obj = MP_OBJ_NULL ;
480+ static mp_obj_t pb_lwp3device_close (mp_obj_t self_in ) {
481+ pb_lwp3device_obj_t * self = MP_OBJ_TO_PTR (self_in );
482+ // Disables notification handler from accessing allocated memory.
483+ pbdrv_bluetooth_peripheral_release (self -> peripheral , self );
489484 return mp_const_none ;
490485}
491486MP_DEFINE_CONST_FUN_OBJ_1 (pb_lwp3device_close_obj , pb_lwp3device_close );
@@ -497,10 +492,6 @@ static mp_obj_t pb_type_pupdevices_Remote_make_new(const mp_obj_type_t *type, si
497492
498493 pb_module_tools_assert_blocking ();
499494
500- if (self_obj ) {
501- mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Can use only one Remote" ));
502- }
503-
504495 pb_lwp3device_obj_t * self = mp_obj_malloc_with_finaliser (pb_lwp3device_obj_t , type );
505496 self_obj = MP_OBJ_FROM_PTR (self );
506497
@@ -557,6 +548,10 @@ static mp_obj_t pb_lwp3device_name(size_t n_args, const mp_obj_t *args) {
557548static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pb_lwp3device_name_obj , 1 , 2 , pb_lwp3device_name ) ;
558549
559550static mp_obj_t pb_lwp3device_disconnect (mp_obj_t self_in ) {
551+ // Needed to release claim on allocated data so we can make a new
552+ // connection later.
553+ pb_lwp3device_close (self_in );
554+
560555 pb_assert (pbdrv_bluetooth_peripheral_disconnect ());
561556 return wait_or_await_operation (self_in );
562557}
@@ -588,21 +583,11 @@ MP_DEFINE_CONST_OBJ_TYPE(pb_type_pupdevices_Remote,
588583/**
589584 * Handles LEGO Wireless protocol messages from generic LWP3 devices.
590585 */
591- static void handle_lwp3_generic_notification (pbdrv_bluetooth_peripheral_t * peripheral , const uint8_t * value , uint32_t size ) {
592-
593- // No object registered for processing notifcations.
594- if (self_obj == MP_OBJ_NULL ) {
595- return ;
596- }
586+ static void handle_lwp3_generic_notification (void * user , const uint8_t * value , uint32_t size ) {
597587
598- pb_lwp3device_obj_t * self = MP_OBJ_TO_PTR ( self_obj ) ;
588+ pb_lwp3device_obj_t * self = user ;
599589
600- // It's not for us.
601- if (self -> peripheral != peripheral ) {
602- return ;
603- }
604-
605- if (!self -> noti_num ) {
590+ if (!self || !self -> noti_num ) {
606591 // Allocated data not ready.
607592 return ;
608593 }
@@ -622,7 +607,6 @@ static void handle_lwp3_generic_notification(pbdrv_bluetooth_peripheral_t *perip
622607 return ;
623608}
624609
625-
626610static mp_obj_t pb_type_iodevices_LWP3Device_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
627611 PB_PARSE_ARGS_CLASS (n_args , n_kw , args ,
628612 PB_ARG_REQUIRED (hub_kind ),
@@ -631,10 +615,6 @@ static mp_obj_t pb_type_iodevices_LWP3Device_make_new(const mp_obj_type_t *type,
631615 PB_ARG_DEFAULT_FALSE (pair ),
632616 PB_ARG_DEFAULT_INT (num_notifications , 8 ));
633617
634- if (self_obj ) {
635- mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Can use only one LWP3Device" ));
636- }
637-
638618 uint8_t hub_kind = pb_obj_get_positive_int (hub_kind_in );
639619 bool pair = mp_obj_is_true (pair_in );
640620
0 commit comments