Skip to content

Commit a397af9

Browse files
committed
Round BLE timing values; fix timeout check
1 parent 55c8075 commit a397af9

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ mp_obj_t common_hal_bleio_adapter_start_scan(bleio_adapter_obj_t *self, uint8_t*
469469

470470
ble_drv_add_event_handler(scan_on_ble_evt, self->scan_results);
471471

472-
uint32_t nrf_timeout = SEC_TO_UNITS(timeout, UNIT_10_MS);
472+
uint32_t nrf_timeout = SEC_TO_UNITS(timeout, UNIT_10_MS) + 0.5f;
473473
if (nrf_timeout > UINT16_MAX) {
474474
// 0xffff / 100
475475
mp_raise_ValueError(translate("timeout must be < 655.35 secs"));
@@ -479,15 +479,15 @@ mp_obj_t common_hal_bleio_adapter_start_scan(bleio_adapter_obj_t *self, uint8_t*
479479
mp_raise_ValueError(translate("non-zero timeout must be > 0.01"));
480480
}
481481

482-
if (nrf_timeout) {
482+
if (nrf_timeout == 0) {
483483
nrf_timeout = BLE_GAP_SCAN_TIMEOUT_UNLIMITED;
484484
}
485485

486486
ble_gap_scan_params_t scan_params = {
487487
.extended = extended,
488-
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS),
488+
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS) + 0.5f,
489489
.timeout = nrf_timeout,
490-
.window = SEC_TO_UNITS(window, UNIT_0_625_MS),
490+
.window = SEC_TO_UNITS(window, UNIT_0_625_MS) + 0.5f,
491491
.scan_phys = BLE_GAP_PHY_1MBPS,
492492
.active = active
493493
};
@@ -553,7 +553,7 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre
553553
.window = MSEC_TO_UNITS(100, UNIT_0_625_MS),
554554
.scan_phys = BLE_GAP_PHY_1MBPS,
555555
// timeout of 0 means no timeout
556-
.timeout = SEC_TO_UNITS(timeout, UNIT_10_MS),
556+
.timeout = SEC_TO_UNITS(timeout, UNIT_10_MS) + 0.5f,
557557
};
558558

559559
ble_gap_conn_params_t conn_params = {
@@ -696,7 +696,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
696696
}
697697

698698
ble_gap_adv_params_t adv_params = {
699-
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS),
699+
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS) + 0.5f,
700700
.properties.type = adv_type,
701701
.duration = SEC_TO_UNITS(timeout, UNIT_10_MS),
702702
.filter_policy = BLE_GAP_ADV_FP_ANY,

shared-bindings/_bleio/Adapter.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
#include "shared-bindings/_bleio/Address.h"
3434
#include "shared-bindings/_bleio/Adapter.h"
3535

36-
#define ADV_INTERVAL_MIN (0.02001f)
37-
#define ADV_INTERVAL_MIN_STRING "0.02001"
36+
#define ADV_INTERVAL_MIN (0.02f)
37+
#define ADV_INTERVAL_MIN_STRING "0.02"
3838
#define ADV_INTERVAL_MAX (10.24f)
3939
#define ADV_INTERVAL_MAX_STRING "10.24"
4040
// 20ms is recommended by Apple
@@ -204,7 +204,7 @@ const mp_obj_property_t bleio_adapter_name_obj = {
204204
//| :param ~_typing.ReadableBuffer scan_response: scan response data packet bytes. ``None`` if no scan response is needed.
205205
//| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral.
206206
//| :param bool anonymous: If `True` then this device's MAC address is randomized before advertising.
207-
//| :param int timeout: If set, we will only advertise for this many seconds.
207+
//| :param int timeout: If set, we will only advertise for this many seconds. Zero means no timeout.
208208
//| :param float interval: advertising interval, in seconds"""
209209
//| ...
210210
//|
@@ -237,7 +237,7 @@ STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t
237237
args[ARG_interval].u_obj = mp_obj_new_float(ADV_INTERVAL_DEFAULT);
238238
}
239239

240-
const mp_float_t interval = mp_obj_float_get(args[ARG_interval].u_obj);
240+
const mp_float_t interval = mp_obj_get_float(args[ARG_interval].u_obj);
241241
if (interval < ADV_INTERVAL_MIN || interval > ADV_INTERVAL_MAX) {
242242
mp_raise_ValueError_varg(translate("interval must be in range %s-%s"),
243243
ADV_INTERVAL_MIN_STRING, ADV_INTERVAL_MAX_STRING);
@@ -279,7 +279,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_stop_advertising_obj, bleio_adapt
279279
//| ignored. Format is one byte for length (n) and n bytes of prefix and can be repeated.
280280
//| :param int buffer_size: the maximum number of advertising bytes to buffer.
281281
//| :param bool extended: When True, support extended advertising packets. Increasing buffer_size is recommended when this is set.
282-
//| :param float timeout: the scan timeout in seconds. If None, will scan until `stop_scan` is called.
282+
//| :param float timeout: the scan timeout in seconds. If None or zero, will scan until `stop_scan` is called.
283283
//| :param float interval: the interval (in seconds) between the start of two consecutive scan windows
284284
//| Must be in the range 0.0025 - 40.959375 seconds.
285285
//| :param float window: the duration (in seconds) to scan a single BLE channel.
@@ -320,7 +320,7 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
320320
args[ARG_window].u_obj = mp_obj_new_float(WINDOW_DEFAULT);
321321
}
322322

323-
const mp_float_t interval = mp_obj_float_get(args[ARG_interval].u_obj);
323+
const mp_float_t interval = mp_obj_get_float(args[ARG_interval].u_obj);
324324
if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) {
325325
mp_raise_ValueError_varg(translate("interval must be in range %s-%s"), INTERVAL_MIN_STRING, INTERVAL_MAX_STRING);
326326
}
@@ -332,7 +332,7 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
332332
}
333333
#pragma GCC diagnostic pop
334334

335-
const mp_float_t window = mp_obj_float_get(args[ARG_window].u_obj);
335+
const mp_float_t window = mp_obj_get_float(args[ARG_window].u_obj);
336336
if (window > interval) {
337337
mp_raise_ValueError(translate("window must be <= interval"));
338338
}

shared-bindings/_pixelbuf/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
//|
5252

5353
STATIC mp_obj_t pixelbuf_colorwheel(mp_obj_t n) {
54-
return MP_OBJ_NEW_SMALL_INT(colorwheel(MP_OBJ_IS_SMALL_INT(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_float_get(n)));
54+
return MP_OBJ_NEW_SMALL_INT(colorwheel(MP_OBJ_IS_SMALL_INT(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
5555
}
5656
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_colorwheel_obj, pixelbuf_colorwheel);
5757

0 commit comments

Comments
 (0)