Skip to content

Commit

Permalink
pybricks.common.BLE: return None if no observed data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dlech committed May 2, 2023
1 parent c1e3eb5 commit df58454
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pybricks/common/pb_type_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,21 @@ STATIC observed_data_t *pb_module_ble_get_channel_data(mp_obj_t channel_in) {
*
* @param [in] self_in The BLE object.
* @param [in] channel_in Python object containing the channel number.
* @returns Python object containing a tuple of decoded data.
* @returns Python object containing a tuple of decoded data or
* None if no data has been received within
* ::OBSERVED_DATA_TIMEOUT_MS.
* @throws ValueError If the channel is out of range.
* @throws RuntimeError If the last received data was invalid.
*/
STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {

observed_data_t *ch_data = pb_module_ble_get_channel_data(channel_in);

// Have not received data yet or timed out.
if (ch_data->rssi == INT8_MIN) {
return mp_const_none;
}

// Objects can be encoded in as little as one byte so we could have up to
// this many objects received.
mp_obj_t items[OBSERVED_DATA_MAX_SIZE];
Expand Down

0 comments on commit df58454

Please sign in to comment.