66
77class ESP_SPIcontrol :
88 GET_CONN_STATUS_CMD = const (0x20 )
9- GET_FW_VERSION_CMD = const (0x37 )
109 GET_MACADDR_CMD = const (0x22 )
10+ SCAN_NETWORKS = const (0x27 )
11+ START_SCAN_NETWORKS = const (0x36 )
12+ GET_FW_VERSION_CMD = const (0x37 )
13+
1114 START_CMD = const (0xE0 )
1215 END_CMD = const (0xEE )
1316 ERR_CMD = const (0xEF )
@@ -63,9 +66,11 @@ def slave_ready(self):
6366 return self ._ready .value == False
6467
6568 def wait_for_slave_ready (self ):
66- print ("wait for slave ready" )
69+ print ("Wait for slave ready" , end = '' )
6770 while not self .slave_ready ():
68- print ('.' )
71+ print ('.' , end = '' )
72+ time .sleep (0.01 )
73+ print ()
6974
7075 def wait_for_slave_select (self ):
7176 self .wait_for_slave_ready ()
@@ -113,18 +118,21 @@ def check_data(self, desired):
113118 if r != desired :
114119 raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
115120
116- def wait_response_cmd (self , cmd , num_responses ):
121+ def wait_response_cmd (self , cmd , num_responses = None ):
117122 self .wait_spi_char (START_CMD )
118123 self .check_data (cmd | REPLY_FLAG )
119- self .check_data (num_responses )
124+ if num_responses is not None :
125+ self .check_data (num_responses )
126+ else :
127+ num_responses = self .get_param ()
120128 responses = []
121129 for num in range (num_responses ):
122130 response = []
123131 param_len = self .get_param ()
124132 print ("parameter #%d length is %d" % (num , param_len ))
125133 for j in range (param_len ):
126134 response .append (self .get_param ())
127- responses .append (response )
135+ responses .append (bytes ( response ) )
128136 self .check_data (END_CMD )
129137 return responses
130138
@@ -151,7 +159,7 @@ def get_firmware_version(self):
151159 self .spi_slave_select ()
152160 resp = self .wait_response_cmd (GET_FW_VERSION_CMD , 1 )
153161 self .slave_deselect ()
154- return '' . join ([ chr ( c ) for c in resp [0 ]])
162+ return resp [0 ]
155163
156164 def get_MAC (self ):
157165 print ("MAC address" )
@@ -164,3 +172,37 @@ def get_MAC(self):
164172 resp = self .wait_response_cmd (GET_MACADDR_CMD , 1 )
165173 self .slave_deselect ()
166174 return resp [0 ]
175+
176+ def start_scan_networks (self ):
177+ 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 ()
186+ if resp [0 ][0 ] != 1 :
187+ raise RuntimeError ("Failed to start AP scan" )
188+
189+ def get_scan_networks (self ):
190+ self .wait_for_slave_select ()
191+ 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
199+
200+ def scan_networks (self ):
201+ self .start_scan_networks ()
202+ APs = None
203+ for _ in range (10 ): # attempts
204+ time .sleep (2 )
205+ APs = self .get_scan_networks ()
206+ if len (APs ):
207+ break
208+ return APs
0 commit comments