Skip to content

Commit b5da5a3

Browse files
committed
send socket data and receive too
1 parent 1d9092d commit b5da5a3

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

adafruit_esp32spi.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class ESP_SPIcontrol:
1717
GET_CURR_ENCT_CMD = const(0x26)
1818

1919
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)
2929
GET_IDX_RSSI_CMD = const(0x32)
3030
GET_IDX_ENCT_CMD = const(0x33)
3131
REQ_HOST_BY_NAME_CMD = const(0x34)
@@ -34,7 +34,8 @@ class ESP_SPIcontrol:
3434
GET_FW_VERSION_CMD = const(0x37)
3535
PING_CMD = const(0x3E)
3636

37-
SEND_DATA_TCP_CMD = const(0x44)
37+
SEND_DATA_TCP_CMD = const(0x44)
38+
GET_DATABUF_TCP_CMD = const(0x45)
3839

3940

4041
START_CMD = const(0xE0)
@@ -172,7 +173,7 @@ def check_data(self, desired):
172173
if r != desired:
173174
raise RuntimeError("Expected %02X but got %02X" % (desired, r))
174175

175-
def wait_response_cmd(self, cmd, num_responses=None):
176+
def wait_response_cmd(self, cmd, num_responses=None, *, param_len_16=False):
176177
self.wait_for_slave_ready()
177178
self.spi_slave_select()
178179

@@ -186,6 +187,9 @@ def wait_response_cmd(self, cmd, num_responses=None):
186187
for num in range(num_responses):
187188
response = []
188189
param_len = self.get_param()
190+
if param_len_16:
191+
param_len <<= 8
192+
param_len |= self.get_param()
189193
if self._debug:
190194
print("parameter #%d length is %d" % (num, param_len))
191195
for j in range(param_len):
@@ -196,9 +200,11 @@ def wait_response_cmd(self, cmd, num_responses=None):
196200
self.slave_deselect()
197201
return responses
198202

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)
202208

203209
@property
204210
def status(self):
@@ -356,16 +362,30 @@ def socket_connected(self, socket_num):
356362
def socket_write(self, socket_num, buffer):
357363
resp = self.send_command_get_response(SEND_DATA_TCP_CMD,
358364
[[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]
361380

362381
def socket_connect(self, dest, port):
363382
if self._debug:
364383
print("*** Socket connect")
365384
if isinstance(dest, str): # convert to IP address
366385
dest = self.get_host_by_name(dest)
367386
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)
369389

370390
self.start_client(dest, dest, port, self.sock_num)
371391
times = time.monotonic()

0 commit comments

Comments
 (0)