Simple test

Ensure your device works with this simple test.

examples/bluefruitspi_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# A simple echo test for the Feather M0 Bluefruit
 5# Sets the name, then echo's all RX'd data with a reversed packet
 6
 7import time
 8import busio
 9import board
10from digitalio import DigitalInOut
11from adafruit_bluefruitspi import BluefruitSPI
12
13spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
14cs = DigitalInOut(board.D8)
15irq = DigitalInOut(board.D7)
16rst = DigitalInOut(board.D4)
17bluefruit = BluefruitSPI(spi_bus, cs, irq, rst, debug=False)
18
19# Initialize the device and perform a factory reset
20print("Initializing the Bluefruit LE SPI Friend module")
21bluefruit.init()
22bluefruit.command_check_OK(b"AT+FACTORYRESET", delay=1)
23
24# Print the response to 'ATI' (info request) as a string
25print(str(bluefruit.command_check_OK(b"ATI"), "utf-8"))
26
27# Change advertised name
28bluefruit.command_check_OK(b"AT+GAPDEVNAME=BlinkaBLE")
29
30while True:
31    print("Waiting for a connection to Bluefruit LE Connect ...")
32    # Wait for a connection ...
33    dotcount = 0
34    while not bluefruit.connected:
35        print(".", end="")
36        dotcount = (dotcount + 1) % 80
37        if dotcount == 79:
38            print("")
39        time.sleep(0.5)
40
41    # Once connected, check for incoming BLE UART data
42    print("\n *Connected!*")
43    connection_timestamp = time.monotonic()
44    while True:
45        # Check our connection status every 3 seconds
46        if time.monotonic() - connection_timestamp > 3:
47            connection_timestamp = time.monotonic()
48            if not bluefruit.connected:
49                break
50
51        # OK we're still connected, see if we have any data waiting
52        resp = bluefruit.uart_rx()
53        if not resp:
54            continue  # nothin'
55        print("Read %d bytes: %s" % (len(resp), resp))
56        # Now write it!
57        print("Writing reverse...")
58        send = []
59        for i in range(len(resp), 0, -1):
60            send.append(resp[i - 1])
61        print(bytes(send))
62        bluefruit.uart_tx(bytes(send))
63
64    print("Connection lost.")