@@ -17,15 +17,15 @@ class ESP_SPIcontrol:
17
17
GET_CURR_ENCT_CMD = const (0x26 )
18
18
19
19
SCAN_NETWORKS = const (0x27 )
20
- GET_SOCKET_CMD = const (0x3F )
21
- GET_STATE_TCP_CMD = const (0x29 )
22
- DATA_SENT_TCP_CMD = const (0x2A )
23
- AVAIL_DATA_TCP_CMD = const (0x2B )
24
- GET_DATA_TCP_CMD = const (0x2C )
25
- START_CLIENT_TCP_CMD = const (0x2D )
26
- STOP_CLIENT_TCP_CMD = const (0x2E )
27
- GET_CLIENT_STATE_TCP_CMD = const (0x2F )
28
- DISCONNECT_CMD = const (0x30 )
20
+ GET_SOCKET_CMD = const (0x3F )
21
+ GET_STATE_TCP_CMD = const (0x29 )
22
+ DATA_SENT_TCP_CMD = const (0x2A )
23
+ AVAIL_DATA_TCP_CMD = const (0x2B )
24
+ GET_DATA_TCP_CMD = const (0x2C )
25
+ START_CLIENT_TCP_CMD = const (0x2D )
26
+ STOP_CLIENT_TCP_CMD = const (0x2E )
27
+ GET_CLIENT_STATE_TCP_CMD = const (0x2F )
28
+ DISCONNECT_CMD = const (0x30 )
29
29
GET_IDX_RSSI_CMD = const (0x32 )
30
30
GET_IDX_ENCT_CMD = const (0x33 )
31
31
REQ_HOST_BY_NAME_CMD = const (0x34 )
@@ -34,7 +34,8 @@ class ESP_SPIcontrol:
34
34
GET_FW_VERSION_CMD = const (0x37 )
35
35
PING_CMD = const (0x3E )
36
36
37
- SEND_DATA_TCP_CMD = const (0x44 )
37
+ SEND_DATA_TCP_CMD = const (0x44 )
38
+ GET_DATABUF_TCP_CMD = const (0x45 )
38
39
39
40
40
41
START_CMD = const (0xE0 )
@@ -172,7 +173,7 @@ def check_data(self, desired):
172
173
if r != desired :
173
174
raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
174
175
175
- def wait_response_cmd (self , cmd , num_responses = None ):
176
+ def wait_response_cmd (self , cmd , num_responses = None , * , param_len_16 = False ):
176
177
self .wait_for_slave_ready ()
177
178
self .spi_slave_select ()
178
179
@@ -186,6 +187,9 @@ def wait_response_cmd(self, cmd, num_responses=None):
186
187
for num in range (num_responses ):
187
188
response = []
188
189
param_len = self .get_param ()
190
+ if param_len_16 :
191
+ param_len <<= 8
192
+ param_len |= self .get_param ()
189
193
if self ._debug :
190
194
print ("parameter #%d length is %d" % (num , param_len ))
191
195
for j in range (param_len ):
@@ -196,9 +200,11 @@ def wait_response_cmd(self, cmd, num_responses=None):
196
200
self .slave_deselect ()
197
201
return responses
198
202
199
- def send_command_get_response (self , cmd , params = None , * , reply_params = 1 , param_len_16 = False ):
200
- self .send_command (cmd , params , param_len_16 = param_len_16 )
201
- return self .wait_response_cmd (cmd , reply_params )
203
+ def send_command_get_response (self , cmd , params = None , * ,
204
+ reply_params = 1 , sent_param_len_16 = False ,
205
+ recv_param_len_16 = False ):
206
+ self .send_command (cmd , params , param_len_16 = sent_param_len_16 )
207
+ return self .wait_response_cmd (cmd , reply_params , param_len_16 = recv_param_len_16 )
202
208
203
209
@property
204
210
def status (self ):
@@ -356,16 +362,30 @@ def socket_connected(self, socket_num):
356
362
def socket_write (self , socket_num , buffer ):
357
363
resp = self .send_command_get_response (SEND_DATA_TCP_CMD ,
358
364
[[socket_num ], buffer ],
359
- param_len_16 = True )
360
- print (resp )
365
+ sent_param_len_16 = True )
366
+
367
+ sent = resp [0 ][0 ]
368
+ if sent != len (buffer ):
369
+ raise RuntimeError ("Failed to send %d bytes (sent %d)" % (len (buffer ), sent ))
370
+ return sent
371
+
372
+ def socket_available (self , socket_num ):
373
+ resp = self .send_command_get_response (AVAIL_DATA_TCP_CMD , [[socket_num ]])
374
+ return struct .unpack ('<H' , resp [0 ])[0 ]
375
+
376
+ def socket_read (self , socket_num , size ):
377
+ resp = self .send_command_get_response (GET_DATABUF_TCP_CMD , [[socket_num ], [size ]],
378
+ sent_param_len_16 = True , recv_param_len_16 = True )
379
+ return resp [0 ]
361
380
362
381
def socket_connect (self , dest , port ):
363
382
if self ._debug :
364
383
print ("*** Socket connect" )
365
384
if isinstance (dest , str ): # convert to IP address
366
385
dest = self .get_host_by_name (dest )
367
386
self .sock_num = self .get_socket ()
368
- print ("Socket #%d" % self .sock_num )
387
+ if self ._debug :
388
+ print ("Allocated socket #%d" % self .sock_num )
369
389
370
390
self .start_client (dest , dest , port , self .sock_num )
371
391
times = time .monotonic ()
0 commit comments