Skip to content

Commit 441225f

Browse files
laurensvalkdlech
authored andcommitted
pybricks.common.BLE: Get rssi via dedicated method.
Now that the RSSI is filtered, it no longer belongs to a particular observe event, so there is no benefit of returning it from the same method. This simplifies the observe method to deal only with data, so it is a bit closer to what the broadcast is doing.
1 parent 133278f commit 441225f

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

pybricks/common/pb_type_ble.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,12 @@ STATIC mp_obj_t pb_module_ble_decode(observed_data_t *data, size_t *index) {
361361
/**
362362
* Retrieves the last received advertising data.
363363
*
364-
* @param [in] self_in The BLE object.
365364
* @param [in] channel_in Python object containing the channel number.
366-
* @returns Python object containing a tuple of the RSSI and
367-
* the decoded data.
365+
* @returns Pointer to the last received channel data.
368366
* @throws ValueError If the channel is out of range.
369367
* @throws RuntimeError If the last received data was invalid.
370368
*/
371-
STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {
369+
STATIC observed_data_t *pb_module_ble_get_channel_data(mp_obj_t channel_in) {
372370
mp_int_t channel = mp_obj_get_int(channel_in);
373371

374372
observed_data_t *ch_data = lookup_observed_data(channel);
@@ -383,6 +381,22 @@ STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {
383381
ch_data->rssi = INT8_MIN;
384382
}
385383

384+
return ch_data;
385+
}
386+
387+
/**
388+
* Retrieves the last received advertising data.
389+
*
390+
* @param [in] self_in The BLE object.
391+
* @param [in] channel_in Python object containing the channel number.
392+
* @returns Python object containing a tuple of decoded data.
393+
* @throws ValueError If the channel is out of range.
394+
* @throws RuntimeError If the last received data was invalid.
395+
*/
396+
STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {
397+
398+
observed_data_t *ch_data = pb_module_ble_get_channel_data(channel_in);
399+
386400
// Objects can be encoded in as little as one byte so we could have up to
387401
// this many objects received.
388402
mp_obj_t items[OBSERVED_DATA_MAX_SIZE];
@@ -397,14 +411,24 @@ STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {
397411
items[i] = pb_module_ble_decode(ch_data, &index);
398412
}
399413

400-
mp_obj_t result[2];
401-
result[0] = MP_OBJ_NEW_SMALL_INT(ch_data->rssi); // RSSI
402-
result[1] = mp_obj_new_tuple(i, items); // data
403-
404-
return mp_obj_new_tuple(MP_ARRAY_SIZE(result), result);
414+
return mp_obj_new_tuple(i, items);
405415
}
406416
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pb_module_ble_observe_obj, pb_module_ble_observe);
407417

418+
/**
419+
* Retrieves the filtered RSSI signal strength of the given channel.
420+
*
421+
* @param [in] self_in The BLE object.
422+
* @param [in] channel_in Python object containing the channel number.
423+
* @returns Python object containing the filtered RSSI.
424+
* @throws ValueError If the channel is out of range.
425+
*/
426+
STATIC mp_obj_t pb_module_ble_signal_strength(mp_obj_t self_in, mp_obj_t channel_in) {
427+
observed_data_t *ch_data = pb_module_ble_get_channel_data(channel_in);
428+
return mp_obj_new_int(ch_data->rssi);
429+
}
430+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pb_module_ble_signal_strength_obj, pb_module_ble_signal_strength);
431+
408432
/**
409433
* Gets the Bluetooth chip frimware version.
410434
* @param [in] self_in The BLE MicroPython object instance.
@@ -420,6 +444,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pb_module_ble_version_obj, pb_module_ble_versio
420444
STATIC const mp_rom_map_elem_t common_BLE_locals_dict_table[] = {
421445
{ MP_ROM_QSTR(MP_QSTR_broadcast), MP_ROM_PTR(&pb_module_ble_broadcast_obj) },
422446
{ MP_ROM_QSTR(MP_QSTR_observe), MP_ROM_PTR(&pb_module_ble_observe_obj) },
447+
{ MP_ROM_QSTR(MP_QSTR_signal_strength), MP_ROM_PTR(&pb_module_ble_signal_strength_obj) },
423448
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&pb_module_ble_version_obj) },
424449
};
425450
STATIC MP_DEFINE_CONST_DICT(common_BLE_locals_dict, common_BLE_locals_dict_table);

0 commit comments

Comments
 (0)