Skip to content

Commit 6f92693

Browse files
author
Kevin Townsend
committed
Added init sequence
1 parent 2dc1846 commit 6f92693

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

adafruit_bluefruitspi.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def __init__(self, spi, cs, irq, reset, debug=False):
143143
self._irq.direction = Direction.INPUT
144144
self._irq.pull = Pull.DOWN
145145

146-
self._spi_device = SPIDevice(spi, cs)
146+
self._spi_device = SPIDevice(spi, cs,
147+
baudrate=4000000, phase=0, polarity=0)
147148

148149
def cmd(self, cmd):
149150
"""
@@ -207,6 +208,24 @@ def cmd(self, cmd):
207208

208209
return msgtype, rspid, rsp
209210

211+
def init(self):
212+
"""
213+
Sends the SDEP initialize command, which causes the board to reset.
214+
This command should complete in under 1s.
215+
"""
216+
# Construct the SDEP packet
217+
struct.pack_into("<BHB", self._buf_tx, 0,
218+
MsgType.COMMAND, SDEPCommand.INITIALIZE, 0)
219+
if self._debug:
220+
print("Writing: ", [hex(b) for b in self._buf_tx])
221+
222+
# Send out the SPI bus
223+
with self._spi_device as spi:
224+
spi.write(self._buf_tx, end=4)
225+
226+
# Wait 1 second for the command to complete.
227+
time.sleep(1)
228+
210229
def uarttx(self, txt):
211230
"""
212231
Sends the specific string out over BLE UART.

examples/bluefruitspi_simpletest.py

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,19 @@ def send_command(string):
2020
except RuntimeError as error:
2121
raise RuntimeError("AT command failure: " + repr(error))
2222

23-
def command_check_OK(string):
23+
def command_check_OK(string, delay=0.0):
2424
ret = send_command(string)
25+
time.sleep(delay)
2526
if not ret or not ret[-4:]:
2627
raise RuntimeError("Not OK")
2728
if ret[-4:] != b'OK\r\n':
2829
raise RuntimeError("Not OK")
2930
if ret[:-4]:
3031
return str(ret[:-4], 'utf-8')
3132

32-
def uarttx(string):
33-
try:
34-
msgtype, msgid, rsp = bluefruit.uarttx(string)
35-
if msgtype == MsgType.ERROR:
36-
raise RuntimeError("Error (id:{0})".format(hex(msgid)))
37-
except RuntimeError as error:
38-
raise RuntimeError("UARTTX command failure: " + repr(error))
39-
if not rsp or not rsp[-4:]:
40-
raise RuntimeError("Not OK")
41-
if rsp[-4:] != b'OK\r\n':
42-
raise RuntimeError("Not OK")
43-
if rsp[:-4]:
44-
return str(ret[:-4], 'utf-8')
45-
46-
def uartrx():
47-
try:
48-
msgtype, msgid, rsp = bluefruit.uartrx()
49-
if msgtype == MsgType.ERROR:
50-
raise RuntimeError("Error (id:{0})".format(hex(msgid)))
51-
except RuntimeError as error:
52-
raise RuntimeError("UARTRX command failure: " + repr(error))
53-
if not rsp or not rsp[-4:]:
54-
raise RuntimeError("Not OK")
55-
if rsp[-4:] != b'OK\r\n':
56-
raise RuntimeError("Not OK")
57-
if rsp[:-4]:
58-
return str(ret[:-4], 'utf-8')
59-
60-
# Send the ATI command
61-
print(command_check_OK("AT+FACTORYRESET"))
62-
time.sleep(1)
33+
# Initialize the device
34+
bluefruit.init()
35+
print(command_check_OK("AT+FACTORYRESET", 1.0))
6336
print(command_check_OK("ATI"))
6437
#print(command_check_OK("AT+GAPDEVNAME=ColorLamp"))
6538

@@ -80,6 +53,10 @@ def uartrx():
8053
# Yay!
8154
print("\nConnected!")
8255
while connected:
83-
uarttx("1")
84-
connected = int(command_check_OK("AT+GAPGETCONN")) == 1
56+
command_check_OK("AT+BLEUARTTX=*")
57+
resp = command_check_OK("AT+BLEUARTRX")
58+
if resp:
59+
print(resp)
60+
# Check connection status with a 1s delay
61+
connected = int(command_check_OK("AT+GAPGETCONN", 0.5)) == 1
8562
print("Connection lost.")

0 commit comments

Comments
 (0)