Simple test

Ensure your device works with this simple test.

examples/ble_radio_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This demo uses the adafruit_radio module to send and receive messages.
 6Devices are switched between broadcast and scanning using the slide switch.
 7The buttons change the message to be sent.
 8"""
 9
10import board
11import digitalio
12
13from adafruit_ble_radio import Radio
14
15slide_switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
16slide_switch.pull = digitalio.Pull.UP
17button_a = digitalio.DigitalInOut(board.BUTTON_A)
18button_a.pull = digitalio.Pull.DOWN
19button_b = digitalio.DigitalInOut(board.BUTTON_B)
20button_b.pull = digitalio.Pull.DOWN
21
22led = digitalio.DigitalInOut(board.D13)
23led.switch_to_output()
24
25msg = [
26    "hello",
27    "hi",
28    "foo",
29    "bar",
30    "baz",
31]
32
33i = 0
34r = Radio()
35
36while True:
37    if slide_switch.value:
38        print("Sending messages...")
39        while slide_switch.value:
40            last_i = i
41            if button_a.value:
42                i += 1
43            if button_b.value:
44                i -= 1
45            i %= len(msg)
46            m = msg[i]
47            print(f"Sending {m}")
48            r.send(m)
49            # Alternative
50            # r.send_bytes(b"Arbitrary bytes")
51    else:
52        print("Scanning for messages...")
53        while not slide_switch.value:
54            m = r.receive_full()
55            if m:
56                print(f"Received message: {m}")
57            # Alternative
58            # m = r.receive()
59            # if m:
60            #    print(m)