Skip to content

Commit 0a8ba51

Browse files
authored
Merge pull request #3 from adafruit/examplecodepr
Updated example code in README
2 parents a482f60 + 2abbfee commit 0a8ba51

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

README.rst

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,67 @@ Usage Example
3131

3232
.. code-block:: python
3333
34+
# A simple echo test for the Feather M0 Bluefruit
35+
# Sets the name, then echo's all RX'd data with a reversed packet
36+
37+
import time
3438
import busio
35-
import digitalio
3639
import board
37-
38-
from adafruit_bluefruitspi import BluefruitSPI, MsgType
40+
from digitalio import DigitalInOut
41+
from adafruit_bluefruitspi import BluefruitSPI
3942
4043
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
41-
cs = digitalio.DigitalInOut(board.A5)
42-
irq = digitalio.DigitalInOut(board.A4)
43-
bluefruit = BluefruitSPI(spi_bus, cs, irq, debug=True)
44-
45-
# Send the ATI command
46-
try:
47-
msgtype, msgid, rsp = bluefruit.cmd("ATI\n")
48-
if msgtype == MsgType.ERROR:
49-
print("Error (id:{0})".format(hex(msgid)))
50-
if msgtype == MsgType.RESPONSE:
51-
print("Response:")
52-
print(rsp)
53-
except RuntimeError as error:
54-
print("AT command failure: " + repr(error))
55-
exit()
44+
cs = DigitalInOut(board.D8)
45+
irq = DigitalInOut(board.D7)
46+
rst = DigitalInOut(board.D4)
47+
bluefruit = BluefruitSPI(spi_bus, cs, irq, rst, debug=False)
48+
49+
# Initialize the device and perform a factory reset
50+
print("Initializing the Bluefruit LE SPI Friend module")
51+
bluefruit.init()
52+
bluefruit.command_check_OK(b'AT+FACTORYRESET', delay=1)
53+
54+
# Print the response to 'ATI' (info request) as a string
55+
print(str(bluefruit.command_check_OK(b'ATI'), 'utf-8'))
56+
57+
# Change advertised name
58+
bluefruit.command_check_OK(b'AT+GAPDEVNAME=BlinkaBLE')
59+
60+
while True:
61+
print("Waiting for a connection to Bluefruit LE Connect ...")
62+
# Wait for a connection ...
63+
dotcount = 0
64+
while not bluefruit.connected:
65+
print(".", end="")
66+
dotcount = (dotcount + 1) % 80
67+
if dotcount == 79:
68+
print("")
69+
time.sleep(0.5)
70+
71+
# Once connected, check for incoming BLE UART data
72+
print("\n *Connected!*")
73+
connection_timestamp = time.monotonic()
74+
while True:
75+
# Check our connection status every 3 seconds
76+
if time.monotonic() - connection_timestamp > 3:
77+
connection_timestamp = time.monotonic()
78+
if not bluefruit.connected:
79+
break
80+
81+
# OK we're still connected, see if we have any data waiting
82+
resp = bluefruit.uart_rx()
83+
if not resp:
84+
continue # nothin'
85+
print("Read %d bytes: %s" % (len(resp), resp))
86+
# Now write it!
87+
print("Writing reverse...")
88+
send = []
89+
for i in range(len(resp), 0, -1):
90+
send.append(resp[i-1])
91+
print(bytes(send))
92+
bluefruit.uart_tx(bytes(send))
93+
94+
print("Connection lost.")
5695
5796
Contributing
5897
============

0 commit comments

Comments
 (0)