Skip to content

Commit

Permalink
Working example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll committed Nov 18, 2019
1 parent b758f37 commit 8e3f5e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
14 changes: 9 additions & 5 deletions adafruit_radio.py
Expand Up @@ -123,8 +123,12 @@ def receive_full(self):
:return: A tuple representation of the received message, or else None.
"""
for entry in self.ble.start_scan(
AdafruitRadio, minimum_rssi=-255, timeout=1
):
now = time.monotonic()
return (entry.msg, entry.rssi, now)
try:
for entry in self.ble.start_scan(
AdafruitRadio, minimum_rssi=-255, timeout=1
):
now = time.monotonic()
return (entry.msg, entry.rssi, now)
finally:
self.ble.stop_scan()
return None
57 changes: 57 additions & 0 deletions examples/radio_simpletest.py
@@ -0,0 +1,57 @@
"""
This demo uses the adafruit_radio module to send and receive messages.
Devices are switched between broadcast and scanning using the slide switch.
The buttons change the message to be sent.
"""
import digitalio
import board
from adafruit_radio import Radio


slide_switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
slide_switch.pull = digitalio.Pull.UP
button_a = digitalio.DigitalInOut(board.BUTTON_A)
button_a.pull = digitalio.Pull.DOWN
button_b = digitalio.DigitalInOut(board.BUTTON_B)
button_b.pull = digitalio.Pull.DOWN

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

msg = [
"hello",
"hi",
"foo",
"bar",
"baz",
]

i = 0
r = Radio()

while True:
if slide_switch.value:
print("Sending messages...")
while slide_switch.value:
last_i = i
if button_a.value:
i += 1
if button_b.value:
i -= 1
i %= len(msg)
m = msg[i]
print("Sending {}".format(m))
r.send(m)
# Alternative
# r.send_bytes(b"Arbitrary bytes")
else:
print("Scanning for messages...")
while not slide_switch.value:
m = r.receive_full()
if m:
print("Received message: {}".format(m))
# Alternative
# m = r.receive()
# if m:
# print(m)

0 comments on commit 8e3f5e8

Please sign in to comment.