Skip to content

Commit 661b490

Browse files
committed
pybricks.common.BLE: return None if no observed data
This modifies ble.observe() to return None if no data has been observed yet or there has been a time out since the last received data. This way there is an unambiguous way to check for valid data.
1 parent 441225f commit 661b490

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

pybricks/common/pb_type_ble.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,21 @@ STATIC observed_data_t *pb_module_ble_get_channel_data(mp_obj_t channel_in) {
389389
*
390390
* @param [in] self_in The BLE object.
391391
* @param [in] channel_in Python object containing the channel number.
392-
* @returns Python object containing a tuple of decoded data.
392+
* @returns Python object containing a tuple of decoded data or
393+
* None if no data has been received within
394+
* ::OBSERVED_DATA_TIMEOUT_MS.
393395
* @throws ValueError If the channel is out of range.
394396
* @throws RuntimeError If the last received data was invalid.
395397
*/
396398
STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {
397399

398400
observed_data_t *ch_data = pb_module_ble_get_channel_data(channel_in);
399401

402+
// Have not received data yet or timed out.
403+
if (ch_data->rssi == INT8_MIN) {
404+
return mp_const_none;
405+
}
406+
400407
// Objects can be encoded in as little as one byte so we could have up to
401408
// this many objects received.
402409
mp_obj_t items[OBSERVED_DATA_MAX_SIZE];

0 commit comments

Comments
 (0)