@@ -52,7 +52,7 @@ static pbio_task_t broadcast_task;
5252
5353typedef struct {
5454 mp_obj_base_t base ;
55- uint8_t broadcast_channel ;
55+ mp_obj_t broadcast_channel ;
5656 pbio_task_t * broadcast_task ;
5757 observed_data_t observed_data [];
5858} pb_obj_BLE_t ;
@@ -263,14 +263,9 @@ static mp_obj_t pb_module_ble_broadcast(size_t n_args, const mp_obj_t *pos_args,
263263 // move hub is connected to Pybricks Code. Also, broadcasting interferes
264264 // with observing even when not connected to Pybricks Code.
265265
266- // FIXME: This check is (and should only be) done in the BLE constructor,
267- // but it may still pass there since 0 is a valid broadcast channel. That
268- // should be fixed by defaulting to None if no broadcast channel is provided.
269- #if PBSYS_CONFIG_BLUETOOTH_TOGGLE
270- if (!pbsys_storage_settings_bluetooth_enabled ()) {
271- mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Bluetooth not enabled" ));
266+ if (self -> broadcast_channel == mp_const_none ) {
267+ mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("no broadcast channel selected" ));
272268 }
273- #endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
274269
275270 // Stop broadcasting if data is None.
276271 if (data_in == mp_const_none ) {
@@ -309,7 +304,7 @@ static mp_obj_t pb_module_ble_broadcast(size_t n_args, const mp_obj_t *pos_args,
309304 value .v .data [0 ] = index + 4 ; // length
310305 value .v .data [1 ] = MFG_SPECIFIC ;
311306 pbio_set_uint16_le (& value .v .data [2 ], LEGO_CID );
312- value .v .data [4 ] = self -> broadcast_channel ;
307+ value .v .data [4 ] = mp_obj_get_int ( self -> broadcast_channel ) ;
313308
314309 pbdrv_bluetooth_start_broadcasting (self -> broadcast_task , & value .v );
315310 return pb_module_tools_pbio_task_wait_or_await (self -> broadcast_task );
@@ -516,7 +511,7 @@ static MP_DEFINE_CONST_OBJ_TYPE(pb_type_BLE,
516511 *
517512 * Do not call this function more than once unless pb_type_ble_start_cleanup() is called first.
518513 *
519- * @param [in] broadcast_channel_in (int) The channel number to use for broadcasting.
514+ * @param [in] broadcast_channel_in (int) The channel number to use for broadcasting or None for no broadcasting .
520515 * @param [in] observe_channels_in (list[int]) A list of channels numbers to observe.
521516 * @returns A newly allocated object.
522517 * @throws ValueError If either parameter contains an out of range channel number.
@@ -525,29 +520,27 @@ mp_obj_t pb_type_BLE_new(mp_obj_t broadcast_channel_in, mp_obj_t observe_channel
525520 // making the assumption that this is only called once before each pb_type_ble_start_cleanup()
526521 assert (observed_data == NULL );
527522
528- mp_int_t broadcast_channel = mp_obj_get_int (broadcast_channel_in );
529-
530- if (broadcast_channel < 0 || broadcast_channel > UINT8_MAX ) {
531- mp_raise_ValueError (MP_ERROR_TEXT ("broadcast channel must be 0 to 255" ));
523+ // Validate channel arguments.
524+ if (broadcast_channel_in != mp_const_none && (mp_obj_get_int (broadcast_channel_in ) < 0 || mp_obj_get_int (broadcast_channel_in ) > UINT8_MAX )) {
525+ mp_raise_ValueError (MP_ERROR_TEXT ("Broadcast channel must be 0 to 255 or None" ));
526+ }
527+ mp_int_t num_observe_channels = mp_obj_get_int (mp_obj_len (observe_channels_in ));
528+ if (num_observe_channels < 0 || num_observe_channels > UINT8_MAX ) {
529+ mp_raise_ValueError (MP_ERROR_TEXT ("Too many observe channels" ));
532530 }
533531
534- mp_int_t num_channels = mp_obj_get_int (mp_obj_len (observe_channels_in ));
535-
532+ // Raise if Bluetooth is attempted to be used while not enabled.
536533 #if PBSYS_CONFIG_BLUETOOTH_TOGGLE
537- if (!pbsys_storage_settings_bluetooth_enabled () && (num_channels > 0 || broadcast_channel )) {
534+ if (!pbsys_storage_settings_bluetooth_enabled () && (num_observe_channels > 0 || broadcast_channel_in != mp_const_none )) {
538535 mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Bluetooth not enabled" ));
539536 }
540537 #endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
541538
542- if (num_channels < 0 || num_channels > UINT8_MAX ) {
543- mp_raise_ValueError (MP_ERROR_TEXT ("len observe channels must be 0 to 255" ));
544- }
545-
546- pb_obj_BLE_t * self = mp_obj_malloc_var (pb_obj_BLE_t , observed_data_t , num_channels , & pb_type_BLE );
539+ pb_obj_BLE_t * self = mp_obj_malloc_var (pb_obj_BLE_t , observed_data_t , num_observe_channels , & pb_type_BLE );
547540 self -> broadcast_task = & broadcast_task ;
548- self -> broadcast_channel = broadcast_channel ;
541+ self -> broadcast_channel = broadcast_channel_in ;
549542
550- for (mp_int_t i = 0 ; i < num_channels ; i ++ ) {
543+ for (mp_int_t i = 0 ; i < num_observe_channels ; i ++ ) {
551544 mp_int_t channel = mp_obj_get_int (mp_obj_subscr (
552545 observe_channels_in , MP_OBJ_NEW_SMALL_INT (i ), MP_OBJ_SENTINEL ));
553546
@@ -564,10 +557,10 @@ mp_obj_t pb_type_BLE_new(mp_obj_t broadcast_channel_in, mp_obj_t observe_channel
564557
565558 // globals for driver callback
566559 observed_data = self -> observed_data ;
567- num_observed_data = num_channels ;
560+ num_observed_data = num_observe_channels ;
568561
569562 // Start observing.
570- if (num_channels > 0 ) {
563+ if (num_observe_channels > 0 ) {
571564 pbio_task_t task ;
572565 pbdrv_bluetooth_start_observing (& task , handle_observe_event );
573566 pb_module_tools_pbio_task_do_blocking (& task , -1 );
0 commit comments