48
48
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BluefruitSPI.git"
49
49
50
50
import time
51
- import digitalio
51
+ from digitalio import Direction , Pull
52
52
from adafruit_bus_device .spi_device import SPIDevice
53
53
from micropython import const
54
- try :
55
- from struct import pack_into , unpack
56
- except ImportError :
57
- from ustruct import pack_into , unpack
54
+ import struct
58
55
59
56
60
57
class MsgType : #pylint: disable=too-few-public-methods,bad-whitespace
@@ -125,30 +122,28 @@ class ErrorCode: #pylint: disable=too-few-public-methods,bad-whitespace
125
122
class BluefruitSPI :
126
123
"""Helper for the Bluefruit LE SPI Friend"""
127
124
128
- def __init__ (self , spi , cs , irq , debug = False ):
129
- self ._cs = cs
125
+ def __init__ (self , spi , cs , irq , reset , debug = False ):
130
126
self ._irq = irq
131
- self ._spi = spi
132
- self ._ble = SPIDevice (self ._spi , self ._cs )
133
127
self ._buf_tx = bytearray (20 )
134
128
self ._buf_rx = bytearray (20 )
135
129
self ._debug = debug
136
130
137
- # CS is an active low output, so set pullup
138
- self ._cs .switch_to_output (value = True ,
139
- drive_mode = digitalio .DriveMode .PUSH_PULL )
131
+ # Reset
132
+ reset .direction = Direction .OUTPUT
133
+ reset .value = False
134
+ time .sleep (0.01 )
135
+ reset .value = True
136
+ time .sleep (0.5 )
140
137
141
- # irq line is active high input, so set a pulldown as a precaution
142
- self ._irq .switch_to_input (pull = digitalio .Pull .DOWN )
143
-
144
- # Check out the SPI bus
145
- while not self ._spi .try_lock ():
146
- pass
138
+ # CS is an active low output
139
+ cs .direction = Direction .OUTPUT
140
+ cs .value = True
147
141
148
- # Configure SPI for 4MHz
149
- self ._spi .configure (baudrate = 4000000 , phase = 0 , polarity = 0 )
142
+ # irq line is active high input, so set a pulldown as a precaution
143
+ self ._irq .direction = Direction .INPUT
144
+ self ._irq .pull = Pull .DOWN
150
145
151
- self ._spi . unlock ( )
146
+ self ._spi_device = SPIDevice ( spi , cs )
152
147
153
148
def cmd (self , cmd ):
154
149
"""
@@ -165,25 +160,22 @@ def cmd(self, cmd):
165
160
print ("ERROR: Command too long." )
166
161
raise ValueError ('Command too long.' )
167
162
168
- # Check out the SPI bus
169
- while not self ._spi .try_lock ():
170
- pass
171
-
172
- # Send the packet across the SPI bus
173
- pack_into ("<BHB16s" , self ._buf_tx , 0 ,
174
- MsgType .COMMAND , SDEPCommand .ATCOMMAND ,
175
- len (cmd ), cmd )
163
+ # Construct the SDEP packet
164
+ struct .pack_into ("<BHB16s" , self ._buf_tx , 0 ,
165
+ MsgType .COMMAND , SDEPCommand .ATCOMMAND ,
166
+ len (cmd ), cmd )
176
167
if self ._debug :
177
168
print ("Writing: " , [hex (b ) for b in self ._buf_tx ])
178
- self ._cs .value = False
179
- self ._spi .write (self ._buf_tx , end = len (cmd ) + 4 )
180
- self ._cs .value = True
169
+
170
+ # Send out the SPI bus
171
+ with self ._spi_device as spi :
172
+ spi .write (self ._buf_tx , end = len (cmd ) + 4 )
181
173
182
174
# Wait up to 200ms for a response
183
175
timeout = 0.2
184
- while timeout > 0.0 and self ._irq is False :
185
- time .sleep (0.005 )
186
- timeout -= 0.005
176
+ while timeout > 0 and not self ._irq . value :
177
+ time .sleep (0.01 )
178
+ timeout -= 0.01
187
179
if timeout <= 0 :
188
180
if self ._debug :
189
181
print ("ERROR: Timed out waiting for a response." )
@@ -197,24 +189,33 @@ def cmd(self, cmd):
197
189
while self ._irq .value is True :
198
190
# Read the current response packet
199
191
time .sleep (0.01 )
200
- self ._cs .value = False
201
- self ._spi .readinto (self ._buf_rx )
202
- self ._cs .value = True
192
+ with self ._spi_device as spi :
193
+ spi .readinto (self ._buf_rx )
203
194
204
195
# Read the message envelope and contents
205
- msgtype , rspid , rsplen = unpack ('>BHB' , self ._buf_rx )
196
+ msgtype , rspid , rsplen = struct . unpack ('>BHB' , self ._buf_rx )
206
197
if rsplen >= 16 :
207
198
rsp += self ._buf_rx [4 :20 ]
208
199
else :
209
200
rsp += self ._buf_rx [4 :rsplen + 4 ]
210
201
if self ._debug :
211
202
print ("Reading: " , [hex (b ) for b in self ._buf_rx ])
212
203
213
- # Release the SPI bus
214
- self ._spi .unlock ()
215
-
216
204
# Clean up the response buffer
217
205
if self ._debug :
218
206
print (rsp )
219
207
220
208
return msgtype , rspid , rsp
209
+
210
+ def uarttx (self , txt ):
211
+ """
212
+ Sends the specific string out over BLE UART.
213
+ :param txt: The new-line terminated string to send.
214
+ """
215
+ return self .cmd ("AT+BLEUARTTX=" + txt + "\n " )
216
+
217
+ def uartrx (self ):
218
+ """
219
+ Reads data from the BLE UART FIFO.
220
+ """
221
+ return self .cmd ("AT+BLEUARTRX\n " )
0 commit comments