@@ -83,6 +83,8 @@ def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False)
83
83
self ._reset .direction = Direction .OUTPUT
84
84
self ._gpio0 .direction = Direction .OUTPUT
85
85
86
+ if self ._debug :
87
+ print ("Reset ESP32" )
86
88
self ._gpio0 .value = True # not bootload mode
87
89
self ._cs .value = True
88
90
self ._reset .value = False
@@ -134,16 +136,14 @@ def send_command(self, cmd, params=None, *, param_len_16=False):
134
136
135
137
# handle parameters here
136
138
for i , param in enumerate (params ):
137
- if self ._debug :
138
- print ("sending param #%d is %d bytes long" % (i , len (param )))
139
+ if self ._debug >= 2 :
140
+ print ("\t Sending param #%d is %d bytes long" % (i , len (param )))
139
141
if param_len_16 :
140
142
packet .append ((len (param ) >> 8 ) & 0xFF )
141
143
packet .append (len (param ) & 0xFF )
142
144
packet += (param )
143
145
144
146
packet .append (END_CMD )
145
- if self ._debug :
146
- print ("packet len:" , len (packet ))
147
147
while len (packet ) % 4 != 0 :
148
148
packet .append (0xFF )
149
149
@@ -153,16 +153,16 @@ def send_command(self, cmd, params=None, *, param_len_16=False):
153
153
print ("Wrote: " , [hex (b ) for b in packet ])
154
154
self .slave_deselect ()
155
155
156
- def get_param (self ):
156
+ def read_byte (self ):
157
157
self ._spi .readinto (self ._pbuf )
158
- if self ._debug :
159
- print ("Read param " , hex (self ._pbuf [0 ]))
158
+ if self ._debug >= 2 :
159
+ print ("\t \t Read: " , hex (self ._pbuf [0 ]))
160
160
return self ._pbuf [0 ]
161
161
162
162
def wait_spi_char (self , desired ):
163
163
times = time .monotonic ()
164
164
while (time .monotonic () - times ) < 0.1 :
165
- r = self .get_param ()
165
+ r = self .read_byte ()
166
166
if r == ERR_CMD :
167
167
raise RuntimeError ("Error response to command" )
168
168
if r == desired :
@@ -171,7 +171,7 @@ def wait_spi_char(self, desired):
171
171
raise RuntimeError ("Timed out waiting for SPI char" )
172
172
173
173
def check_data (self , desired ):
174
- r = self .get_param ()
174
+ r = self .read_byte ()
175
175
if r != desired :
176
176
raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
177
177
@@ -184,22 +184,24 @@ def wait_response_cmd(self, cmd, num_responses=None, *, param_len_16=False):
184
184
if num_responses is not None :
185
185
self .check_data (num_responses )
186
186
else :
187
- num_responses = self .get_param ()
187
+ num_responses = self .read_byte ()
188
188
responses = []
189
189
for num in range (num_responses ):
190
190
response = []
191
- param_len = self .get_param ()
191
+ param_len = self .read_byte ()
192
192
if param_len_16 :
193
193
param_len <<= 8
194
- param_len |= self .get_param ()
195
- if self ._debug :
196
- print ("parameter #%d length is %d" % (num , param_len ))
194
+ param_len |= self .read_byte ()
195
+ if self ._debug >= 2 :
196
+ print ("\t Parameter #%d length is %d" % (num , param_len ))
197
197
for j in range (param_len ):
198
- response .append (self .get_param ())
198
+ response .append (self .read_byte ())
199
199
responses .append (bytes (response ))
200
200
self .check_data (END_CMD )
201
201
202
202
self .slave_deselect ()
203
+ if self ._debug :
204
+ print ("Read: " , responses )
203
205
return responses
204
206
205
207
def send_command_get_response (self , cmd , params = None , * ,
@@ -291,10 +293,22 @@ def network_data(self):
291
293
def ip_address (self ):
292
294
return self .network_data ['ip_addr' ]
293
295
296
+
297
+ @property
298
+ def is_connected (self ):
299
+ return self .status == WL_CONNECTED
300
+
301
+ def connect (self , settings ):
302
+ self .connect_AP (settings ['ssid' ], settings ['password' ])
303
+
294
304
def connect_AP (self , ssid , password ):
295
305
if self ._debug :
296
- print ("Connect to AP" )
306
+ print ("Connect to AP" , ssid , password )
307
+ if isinstance (ssid , str ):
308
+ ssid = bytes (ssid , 'utf-8' )
297
309
if password :
310
+ if isinstance (password , str ):
311
+ password = bytes (password , 'utf-8' )
298
312
self .wifi_set_passphrase (ssid , password )
299
313
else :
300
314
self .wifi_set_network (ssid )
@@ -379,9 +393,8 @@ def socket_write(self, socket_num, buffer):
379
393
raise RuntimeError ("Failed to send %d bytes (sent %d)" % (len (buffer ), sent ))
380
394
381
395
resp = self .send_command_get_response (DATA_SENT_TCP_CMD , [[socket_num ]])
382
- if self ._debug :
383
- print ("** DATA SENT: " ,resp )
384
- return sent
396
+ if resp [0 ][0 ] != 1 :
397
+ raise RuntimeError ("Failed to verify data sent" )
385
398
386
399
def socket_available (self , socket_num ):
387
400
resp = self .send_command_get_response (AVAIL_DATA_TCP_CMD , [[socket_num ]])
0 commit comments