Skip to content

Commit c8cb722

Browse files
author
Kevin Townsend
committed
Multiple packets for TX
1 parent 6f92693 commit c8cb722

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

adafruit_bluefruitspi.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,42 @@ def __init__(self, spi, cs, irq, reset, debug=False):
146146
self._spi_device = SPIDevice(spi, cs,
147147
baudrate=4000000, phase=0, polarity=0)
148148

149-
def cmd(self, cmd):
149+
def _cmd(self, cmd):
150150
"""
151151
Executes the supplied AT command, which must be terminated with
152152
a new-line character.
153153
Returns msgtype, rspid, rsp, which are 8-bit int, 16-bit int and a
154154
bytearray.
155155
:param cmd: The new-line terminated AT command to execute.
156156
"""
157-
# Make sure we stay within the 20 byte limit
158-
if len(cmd) > 16:
159-
# TODO: Split command into multiple packets
157+
# Make sure we stay within the 255 byte limit
158+
if len(cmd) > 127:
160159
if self._debug:
161160
print("ERROR: Command too long.")
162161
raise ValueError('Command too long.')
163162

164-
# Construct the SDEP packet
165-
struct.pack_into("<BHB16s", self._buf_tx, 0,
166-
MsgType.COMMAND, SDEPCommand.ATCOMMAND,
167-
len(cmd), cmd)
168-
if self._debug:
169-
print("Writing: ", [hex(b) for b in self._buf_tx])
163+
more = 0x80 # More bit is in pos 8, 1 = more data available
164+
pos = 0
165+
while len(cmd) - pos:
166+
# Construct the SDEP packet
167+
if len(cmd) - pos <= 16:
168+
# Last or sole packet
169+
more = 0
170+
plen = len(cmd) - pos
171+
if plen > 16:
172+
plen = 16
173+
# Note the 'more' value in bit 8 of the packet len
174+
struct.pack_into("<BHB16s", self._buf_tx, 0,
175+
MsgType.COMMAND, SDEPCommand.ATCOMMAND,
176+
plen | more, cmd[pos:pos+plen])
177+
if self._debug:
178+
print("Writing: ", [hex(b) for b in self._buf_tx])
179+
# Update the position if there is data remaining
180+
pos += plen
170181

171-
# Send out the SPI bus
172-
with self._spi_device as spi:
173-
spi.write(self._buf_tx, end=len(cmd) + 4)
182+
# Send out the SPI bus
183+
with self._spi_device as spi:
184+
spi.write(self._buf_tx, end=len(cmd) + 4)
174185

175186
# Wait up to 200ms for a response
176187
timeout = 0.2
@@ -231,10 +242,30 @@ def uarttx(self, txt):
231242
Sends the specific string out over BLE UART.
232243
:param txt: The new-line terminated string to send.
233244
"""
234-
return self.cmd("AT+BLEUARTTX="+txt+"\n")
245+
return self._cmd("AT+BLEUARTTX="+txt+"\n")
235246

236247
def uartrx(self):
237248
"""
238249
Reads data from the BLE UART FIFO.
239250
"""
240-
return self.cmd("AT+BLEUARTRX\n")
251+
return self._cmd("AT+BLEUARTRX\n")
252+
253+
def command(self, string):
254+
try:
255+
msgtype, msgid, rsp = self._cmd(string+"\n")
256+
if msgtype == MsgType.ERROR:
257+
raise RuntimeError("Error (id:{0})".format(hex(msgid)))
258+
if msgtype == MsgType.RESPONSE:
259+
return rsp
260+
except RuntimeError as error:
261+
raise RuntimeError("AT command failure: " + repr(error))
262+
263+
def command_check_OK(self, string, delay=0.0):
264+
ret = self.command(string)
265+
time.sleep(delay)
266+
if not ret or not ret[-4:]:
267+
raise RuntimeError("Not OK")
268+
if ret[-4:] != b'OK\r\n':
269+
raise RuntimeError("Not OK")
270+
if ret[:-4]:
271+
return str(ret[:-4], 'utf-8')

examples/bluefruitspi_simpletest.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,34 @@
1010
rst = DigitalInOut(board.D4)
1111
bluefruit = BluefruitSPI(spi_bus, cs, irq, rst, debug=False)
1212

13-
def send_command(string):
14-
try:
15-
msgtype, msgid, rsp = bluefruit.cmd(string+"\n")
16-
if msgtype == MsgType.ERROR:
17-
raise RuntimeError("Error (id:{0})".format(hex(msgid)))
18-
if msgtype == MsgType.RESPONSE:
19-
return rsp
20-
except RuntimeError as error:
21-
raise RuntimeError("AT command failure: " + repr(error))
22-
23-
def command_check_OK(string, delay=0.0):
24-
ret = send_command(string)
25-
time.sleep(delay)
26-
if not ret or not ret[-4:]:
27-
raise RuntimeError("Not OK")
28-
if ret[-4:] != b'OK\r\n':
29-
raise RuntimeError("Not OK")
30-
if ret[:-4]:
31-
return str(ret[:-4], 'utf-8')
32-
33-
# Initialize the device
13+
# Initialize the device and perform a factory reset
14+
print("Initializing the Bluefruit LE SPI Friend module")
3415
bluefruit.init()
35-
print(command_check_OK("AT+FACTORYRESET", 1.0))
36-
print(command_check_OK("ATI"))
37-
#print(command_check_OK("AT+GAPDEVNAME=ColorLamp"))
16+
bluefruit.command_check_OK("AT+FACTORYRESET", 1.0)
17+
print(bluefruit.command_check_OK("ATI"))
18+
print(bluefruit.command_check_OK("AT+GAPDEVNAME=ColorLamp"))
3819

3920
while True:
40-
# our main loop, if not connected, wait till we are
4121
connected = False
4222
dotcount = 0
4323
print("Waiting for a connection to Bluefruit LE Connect ...")
24+
# Wait for a connection ...
4425
while not connected:
45-
connected = int(command_check_OK("AT+GAPGETCONN")) == 1
26+
connected = int(bluefruit.command_check_OK("AT+GAPGETCONN")) == 1
4627
dotcount += 1
4728
if dotcount == 79:
4829
print(".")
4930
dotcount = 0
5031
else:
5132
print(".", end="")
5233
time.sleep(0.5)
53-
# Yay!
34+
# Once connected, check for incoming BLE UART data
5435
print("\nConnected!")
5536
while connected:
56-
command_check_OK("AT+BLEUARTTX=*")
57-
resp = command_check_OK("AT+BLEUARTRX")
37+
#bluefruit.command_check_OK("AT+BLEUARTTX=0123456789")
38+
resp = bluefruit.command_check_OK("AT+BLEUARTRX")
5839
if resp:
5940
print(resp)
60-
# Check connection status with a 1s delay
61-
connected = int(command_check_OK("AT+GAPGETCONN", 0.5)) == 1
41+
# Check connection status followed by a 500ms delay
42+
connected = int(bluefruit.command_check_OK("AT+GAPGETCONN", 0.5)) == 1
6243
print("Connection lost.")

0 commit comments

Comments
 (0)