@@ -146,31 +146,42 @@ def __init__(self, spi, cs, irq, reset, debug=False):
146
146
self ._spi_device = SPIDevice (spi , cs ,
147
147
baudrate = 4000000 , phase = 0 , polarity = 0 )
148
148
149
- def cmd (self , cmd ):
149
+ def _cmd (self , cmd ):
150
150
"""
151
151
Executes the supplied AT command, which must be terminated with
152
152
a new-line character.
153
153
Returns msgtype, rspid, rsp, which are 8-bit int, 16-bit int and a
154
154
bytearray.
155
155
:param cmd: The new-line terminated AT command to execute.
156
156
"""
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 :
160
159
if self ._debug :
161
160
print ("ERROR: Command too long." )
162
161
raise ValueError ('Command too long.' )
163
162
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
170
181
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 )
174
185
175
186
# Wait up to 200ms for a response
176
187
timeout = 0.2
@@ -231,10 +242,30 @@ def uarttx(self, txt):
231
242
Sends the specific string out over BLE UART.
232
243
:param txt: The new-line terminated string to send.
233
244
"""
234
- return self .cmd ("AT+BLEUARTTX=" + txt + "\n " )
245
+ return self ._cmd ("AT+BLEUARTTX=" + txt + "\n " )
235
246
236
247
def uartrx (self ):
237
248
"""
238
249
Reads data from the BLE UART FIFO.
239
250
"""
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' )
0 commit comments