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"""
 9import digitalio
10import board
11from adafruit_ble_radio import Radio
12
13
14slide_switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
15slide_switch.pull = digitalio.Pull.UP
16button_a = digitalio.DigitalInOut(board.BUTTON_A)
17button_a.pull = digitalio.Pull.DOWN
18button_b = digitalio.DigitalInOut(board.BUTTON_B)
19button_b.pull = digitalio.Pull.DOWN
20
21led = digitalio.DigitalInOut(board.D13)
22led.switch_to_output()
23
24msg = [
25    "hello",
26    "hi",
27    "foo",
28    "bar",
29    "baz",
30]
31
32i = 0
33r = Radio()
34
35while True:
36    if slide_switch.value:
37        print("Sending messages...")
38        while slide_switch.value:
39            last_i = i
40            if button_a.value:
41                i += 1
42            if button_b.value:
43                i -= 1
44            i %= len(msg)
45            m = msg[i]
46            print("Sending {}".format(m))
47            r.send(m)
48            # Alternative
49            # r.send_bytes(b"Arbitrary bytes")
50    else:
51        print("Scanning for messages...")
52        while not slide_switch.value:
53            m = r.receive_full()
54            if m:
55                print("Received message: {}".format(m))
56            # Alternative
57            # m = r.receive()
58            # if m:
59            #    print(m)