MCPcopy Create free account
hub / github.com/RetiredWizard/PyDOS / _send_command

Method _send_command

mpython/NanoConnect/lib/mp_esp32spi.py:42–89  ·  view source on GitHub ↗

Send over a command with a list of parameters

(self, cmd, params=None, *, param_len_16=False)

Source from the content-addressed store, hash-verified

40 self.reset()
41
42 def _send_command(self, cmd, params=None, *, param_len_16=False):
43 """Send over a command with a list of parameters"""
44 if not params:
45 params = ()
46
47 packet_len = 4 # header + end byte
48 for i, param in enumerate(params):
49 packet_len += len(param) # parameter
50 packet_len += 1 # size byte
51 if param_len_16:
52 packet_len += 1 # 2 of em here!
53 while packet_len % 4 != 0:
54 packet_len += 1
55 # we may need more space
56 if packet_len > len(self._sendbuf):
57 self._sendbuf = bytearray(packet_len)
58
59 self._sendbuf[0] = _START_CMD
60 self._sendbuf[1] = cmd & ~_REPLY_FLAG
61 self._sendbuf[2] = len(params)
62
63 # handle parameters here
64 ptr = 3
65 for i, param in enumerate(params):
66 if self._debug >= 2:
67 print("\tSending param #%d is %d bytes long" % (i, len(param)))
68 if param_len_16:
69 self._sendbuf[ptr] = (len(param) >> 8) & 0xFF
70 ptr += 1
71 self._sendbuf[ptr] = len(param) & 0xFF
72 ptr += 1
73 for j, par in enumerate(param):
74 self._sendbuf[ptr + j] = par
75 ptr += len(param)
76 self._sendbuf[ptr] = _END_CMD
77
78 self._wait_for_ready()
79 with self._spi_device as spi:
80 times = time.ticks_ms()
81 while (time.ticks_ms() - times) < 1000: # wait up to 1000ms
82 if self._ready.value(): # ok ready to send!
83 break
84 else:
85 raise RuntimeError("ESP32 timed out on SPI select")
86 spi.write(self._sendbuf)
87 # , start=0, end=packet_len) # pylint: disable=no-member
88 if self._debug >= 3:
89 print("Wrote: ", [hex(b) for b in self._sendbuf[0:packet_len]])
90
91 def _send_command_get_response(
92 self,

Callers 1

Calls 3

_wait_for_readyMethod · 0.95
valueMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected