@@ -91,13 +91,37 @@ static mp_obj_t self_obj;
9191
9292typedef struct {
9393 mp_obj_base_t base ;
94+ /**
95+ * The peripheral instance associated with this MicroPython object.
96+ */
97+ pbdrv_bluetooth_peripheral_t * peripheral ;
98+ /**
99+ * Async iterable state.
100+ */
94101 pb_type_async_t * iter ;
95- pbio_os_state_t sub ;
102+ /**
103+ * Buttons object (property of Remote class).
104+ */
96105 mp_obj_t buttons ;
106+ /**
107+ * Light object (property of Remote class).
108+ */
97109 mp_obj_t light ;
110+ /**
111+ * Powered Up Remote Left button states, populated by notifications.
112+ */
98113 uint8_t left [3 ];
114+ /**
115+ * Powered Up Remote Right button states, populated by notifications.
116+ */
99117 uint8_t right [3 ];
118+ /**
119+ * Powered Up Remote Center button state, populated by notifications.
120+ */
100121 uint8_t center ;
122+ /**
123+ * The hub kind to filter advertisements for.
124+ */
101125 lwp3_hub_kind_t hub_kind ;
102126 // Null-terminated name used to filter advertisements and responses.
103127 // Also used as the name of the device when setting the name, since this
@@ -128,15 +152,20 @@ typedef struct {
128152} pb_lwp3device_obj_t ;
129153
130154// Handles LEGO Wireless protocol messages from the Powered Up Remote.
131- static pbio_pybricks_error_t handle_remote_notification (const uint8_t * value , uint32_t size ) {
155+ static void handle_remote_notification (pbdrv_bluetooth_peripheral_t * peripheral , const uint8_t * value , uint32_t size ) {
132156
157+ // No object registered for processing notifcations.
133158 if (self_obj == MP_OBJ_NULL ) {
134- // Silently ignore incoming notifications when we aren't expecting any.
135- return PBIO_PYBRICKS_ERROR_OK ;
159+ return ;
136160 }
137161
138162 pb_lwp3device_obj_t * self = MP_OBJ_TO_PTR (self_obj );
139163
164+ // It's not for us.
165+ if (self -> peripheral != peripheral ) {
166+ return ;
167+ }
168+
140169 if (value [0 ] == 5 && value [2 ] == LWP3_MSG_TYPE_HW_NET_CMDS && value [3 ] == LWP3_HW_NET_CMD_CONNECTION_REQ ) {
141170 // This message is meant for something else, but contains the center button state
142171 self -> center = value [4 ];
@@ -148,8 +177,6 @@ static pbio_pybricks_error_t handle_remote_notification(const uint8_t *value, ui
148177 memcpy (self -> right , & value [4 ], 3 );
149178 }
150179 }
151-
152- return PBIO_PYBRICKS_ERROR_OK ;
153180}
154181
155182static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_matches (uint8_t event_type , const uint8_t * data , const char * name , const uint8_t * addr , const uint8_t * match_addr ) {
@@ -278,6 +305,9 @@ static pbio_error_t pb_lwp3device_connect_thread(pbio_os_state_t *state, mp_obj_
278305
279306 PBIO_OS_ASYNC_BEGIN (state );
280307
308+ // Get available peripheral instance.
309+ pb_assert (pbdrv_bluetooth_peripheral_get_available (& self -> peripheral ));
310+
281311 // Scan and connect with timeout.
282312 pb_assert (pbdrv_bluetooth_peripheral_scan_and_connect (& scan_config ));
283313 PBIO_OS_AWAIT (state , & unused , err = pbdrv_bluetooth_await_peripheral_command (& unused , NULL ));
@@ -376,7 +406,7 @@ static pbio_error_t pb_lwp3device_connect_thread(pbio_os_state_t *state, mp_obj_
376406 PBIO_OS_ASYNC_END (PBIO_ERROR_IO );
377407}
378408
379- static mp_obj_t pb_lwp3device_connect (mp_obj_t self_in , mp_obj_t name_in , mp_obj_t timeout_in , lwp3_hub_kind_t hub_kind , pbdrv_bluetooth_receive_handler_t notification_handler , bool pair ) {
409+ static mp_obj_t pb_lwp3device_connect (mp_obj_t self_in , mp_obj_t name_in , mp_obj_t timeout_in , lwp3_hub_kind_t hub_kind , pbdrv_bluetooth_peripheral_notification_handler_t notification_handler , bool pair ) {
380410
381411 pb_lwp3device_obj_t * self = MP_OBJ_TO_PTR (self_in );
382412
@@ -555,18 +585,26 @@ MP_DEFINE_CONST_OBJ_TYPE(pb_type_pupdevices_Remote,
555585
556586#if PYBRICKS_PY_IODEVICES_LWP3_DEVICE
557587
558- static pbio_pybricks_error_t handle_lwp3_generic_notification (const uint8_t * value , uint32_t size ) {
588+ /**
589+ * Handles LEGO Wireless protocol messages from generic LWP3 devices.
590+ */
591+ static void handle_lwp3_generic_notification (pbdrv_bluetooth_peripheral_t * peripheral , const uint8_t * value , uint32_t size ) {
559592
593+ // No object registered for processing notifcations.
560594 if (self_obj == MP_OBJ_NULL ) {
561- // Silently ignore incoming notifications when we aren't expecting any.
562- return PBIO_PYBRICKS_ERROR_OK ;
595+ return ;
563596 }
564597
565598 pb_lwp3device_obj_t * self = MP_OBJ_TO_PTR (self_obj );
566599
600+ // It's not for us.
601+ if (self -> peripheral != peripheral ) {
602+ return ;
603+ }
604+
567605 if (!self -> noti_num ) {
568- // Allocated data not ready, but no error .
569- return PBIO_PYBRICKS_ERROR_OK ;
606+ // Allocated data not ready.
607+ return ;
570608 }
571609
572610 // Buffer is full, so drop oldest sample by advancing read index.
@@ -581,7 +619,7 @@ static pbio_pybricks_error_t handle_lwp3_generic_notification(const uint8_t *val
581619 // to-be-read data. If it was already full when we started writing, both
582620 // indexes have now advanced so it is still full now.
583621 self -> noti_data_full = self -> noti_idx_read == self -> noti_idx_write ;
584- return PBIO_PYBRICKS_ERROR_OK ;
622+ return ;
585623}
586624
587625
0 commit comments