1+ import struct
12import time
23import board
34import busio
@@ -8,6 +9,8 @@ class ESP_SPIcontrol:
89 GET_CONN_STATUS_CMD = const (0x20 )
910 GET_MACADDR_CMD = const (0x22 )
1011 SCAN_NETWORKS = const (0x27 )
12+ GET_IDX_RSSI_CMD = const (0x32 )
13+ GET_IDX_ENCT_CMD = const (0x33 )
1114 START_SCAN_NETWORKS = const (0x36 )
1215 GET_FW_VERSION_CMD = const (0x37 )
1316
@@ -94,8 +97,11 @@ def send_command(self, cmd, params=None):
9497 print ("packet len:" , len (packet ))
9598 while len (packet ) % 4 != 0 :
9699 packet .append (0xFF )
100+
101+ self .wait_for_slave_select ()
97102 self ._spi .write (bytearray (packet ))
98103 print ("Wrote: " , [hex (b ) for b in packet ])
104+ self .slave_deselect ()
99105
100106 def get_param (self ):
101107 self ._spi .readinto (self ._pbuf )
@@ -119,6 +125,9 @@ def check_data(self, desired):
119125 raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
120126
121127 def wait_response_cmd (self , cmd , num_responses = None ):
128+ self .wait_for_slave_ready ()
129+ self .spi_slave_select ()
130+
122131 self .wait_spi_char (START_CMD )
123132 self .check_data (cmd | REPLY_FLAG )
124133 if num_responses is not None :
@@ -134,68 +143,49 @@ def wait_response_cmd(self, cmd, num_responses=None):
134143 response .append (self .get_param ())
135144 responses .append (bytes (response ))
136145 self .check_data (END_CMD )
146+
147+ self .slave_deselect ()
137148 return responses
138149
150+ def send_command_get_response (self , cmd , params = None , * , reply_params = 1 ):
151+ self .send_command (cmd , params )
152+ return self .wait_response_cmd (cmd , reply_params )
153+
139154 def get_connection_status (self ):
140155 print ("Connection status" )
141- self .wait_for_slave_select ()
142- self .send_command (GET_CONN_STATUS_CMD )
143- self .slave_deselect ()
144-
145- self .wait_for_slave_ready ()
146- self .spi_slave_select ()
147- resp = self .wait_response_cmd (GET_CONN_STATUS_CMD , 1 )
148- self .slave_deselect ()
156+ resp = self .send_command_get_response (GET_CONN_STATUS_CMD )
149157 print ("Status:" , resp [0 ][0 ])
150158 return resp [0 ][0 ] # one byte response
151159
152160 def get_firmware_version (self ):
153161 print ("Firmware version" )
154- self .wait_for_slave_select ()
155- self .send_command (GET_FW_VERSION_CMD )
156- self .slave_deselect ()
157-
158- self .wait_for_slave_ready ()
159- self .spi_slave_select ()
160- resp = self .wait_response_cmd (GET_FW_VERSION_CMD , 1 )
161- self .slave_deselect ()
162+ resp = self .send_command_get_response (GET_FW_VERSION_CMD )
162163 return resp [0 ]
163164
164165 def get_MAC (self ):
165166 print ("MAC address" )
166- self .wait_for_slave_select ()
167- self .send_command (GET_MACADDR_CMD , [[0xFF ]])
168- self .slave_deselect ()
169-
170- self .wait_for_slave_ready ()
171- self .spi_slave_select ()
172- resp = self .wait_response_cmd (GET_MACADDR_CMD , 1 )
173- self .slave_deselect ()
167+ resp = self .send_command_get_response (GET_MACADDR_CMD , [b'\xFF ' ])
174168 return resp [0 ]
175169
176170 def start_scan_networks (self ):
177171 print ("Start scan" )
178- self .wait_for_slave_select ()
179- self .send_command (START_SCAN_NETWORKS )
180- self .slave_deselect ()
181-
182- self .wait_for_slave_ready ()
183- self .spi_slave_select ()
184- resp = self .wait_response_cmd (START_SCAN_NETWORKS , 1 )
185- self .slave_deselect ()
172+ resp = self .send_command_get_response (START_SCAN_NETWORKS )
186173 if resp [0 ][0 ] != 1 :
187174 raise RuntimeError ("Failed to start AP scan" )
188175
189176 def get_scan_networks (self ):
190- self .wait_for_slave_select ()
191177 self .send_command (SCAN_NETWORKS )
192- self .slave_deselect ()
193-
194- self .wait_for_slave_ready ()
195- self .spi_slave_select ()
196- resp = self .wait_response_cmd (SCAN_NETWORKS )
197- self .slave_deselect ()
198- return resp
178+ names = self .wait_response_cmd (SCAN_NETWORKS )
179+ print ("SSID names:" , names )
180+ APs = []
181+ for i , name in enumerate (names ):
182+ AP = {'ssid' : name }
183+ rssi = self .send_command_get_response (GET_IDX_RSSI_CMD , [[i ]])[0 ]
184+ AP ['rssi' ] = struct .unpack ('<i' , rssi )[0 ]
185+ encr = self .send_command_get_response (GET_IDX_ENCT_CMD , [[i ]])[0 ]
186+ AP ['encryption' ] = encr [0 ]
187+ APs .append (AP )
188+ return APs
199189
200190 def scan_networks (self ):
201191 self .start_scan_networks ()
0 commit comments