Skip to content

Commit

Permalink
tests/multi_bluetooth: Use time.sleep_ms instead of time.sleep.
Browse files Browse the repository at this point in the history
On unix, time.sleep is implemented as select(timeout=<time>) which means
that it does not run the poll hook during sleeping.

Signed-off-by: Jim Mussared <[email protected]>
  • Loading branch information
jimmo committed Apr 26, 2023
1 parent a6aa739 commit dcb863e
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions tests/multi_bluetooth/ble_gap_advertise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)

ADV_TIME_S = 3
ADV_TIME_MS = 3000


def instance0():
multitest.globals(BDADDR=ble.config("mac"))
multitest.next()

adv_data = b"\x02\x01\x06\x04\xffMPY"

print("gap_advertise(100_000, connectable=False)")
ble.gap_advertise(100_000, b"\x02\x01\x06\x04\xffMPY", connectable=False)
time.sleep(ADV_TIME_S)
ble.gap_advertise(100_000, adv_data, connectable=False)
time.sleep_ms(ADV_TIME_MS)

print("gap_advertise(20_000, connectable=True)")
ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY", connectable=True)
time.sleep(ADV_TIME_S)
ble.gap_advertise(20_000, adv_data, connectable=True)
time.sleep_ms(ADV_TIME_MS)

print("gap_advertise(None)")
ble.gap_advertise(None)
Expand All @@ -30,19 +32,20 @@ def instance0():
def instance1():
multitest.next()
finished = False
adv_types = {}
adv_data = None
matched_adv_types = {}
matched_adv_data = None

def irq(ev, data):
nonlocal finished, adv_types, adv_data
nonlocal finished, matched_adv_types, matched_adv_data
if ev == _IRQ_SCAN_RESULT:
if data[0] == BDADDR[0] and data[1] == BDADDR[1]:
adv_types[data[2]] = True
if adv_data is None:
adv_data = bytes(data[4])
addr_type, addr, adv_type, rssi, adv_data = data
if addr_type == BDADDR[0] and addr == BDADDR[1]:
matched_adv_types[adv_type] = True
if matched_adv_data is None:
matched_adv_data = bytes(adv_data)
else:
if adv_data != data[4]:
adv_data = b"MISMATCH"
if adv_data != matched_adv_data:
matched_adv_data = b"MISMATCH"
elif ev == _IRQ_SCAN_DONE:
finished = True

Expand All @@ -51,12 +54,12 @@ def irq(ev, data):
except:
pass
ble.irq(irq)
ble.gap_scan(2 * ADV_TIME_S * 1000, 10000, 10000)
ble.gap_scan(2 * ADV_TIME_MS, 30000, 30000)
while not finished:
machine.idle()
ble.active(0)
print("adv_types:", sorted(adv_types))
print("adv_data:", adv_data)
print("adv_types:", sorted(matched_adv_types))
print("adv_data:", matched_adv_data)


ble = bluetooth.BLE()
Expand Down

0 comments on commit dcb863e

Please sign in to comment.