6
6
7
7
class ESP_SPIcontrol :
8
8
GET_CONN_STATUS_CMD = const (0x20 )
9
- GET_FW_VERSION_CMD = const (0x37 )
10
9
GET_MACADDR_CMD = const (0x22 )
10
+ SCAN_NETWORKS = const (0x27 )
11
+ START_SCAN_NETWORKS = const (0x36 )
12
+ GET_FW_VERSION_CMD = const (0x37 )
13
+
11
14
START_CMD = const (0xE0 )
12
15
END_CMD = const (0xEE )
13
16
ERR_CMD = const (0xEF )
@@ -63,9 +66,11 @@ def slave_ready(self):
63
66
return self ._ready .value == False
64
67
65
68
def wait_for_slave_ready (self ):
66
- print ("wait for slave ready" )
69
+ print ("Wait for slave ready" , end = '' )
67
70
while not self .slave_ready ():
68
- print ('.' )
71
+ print ('.' , end = '' )
72
+ time .sleep (0.01 )
73
+ print ()
69
74
70
75
def wait_for_slave_select (self ):
71
76
self .wait_for_slave_ready ()
@@ -113,18 +118,21 @@ def check_data(self, desired):
113
118
if r != desired :
114
119
raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
115
120
116
- def wait_response_cmd (self , cmd , num_responses ):
121
+ def wait_response_cmd (self , cmd , num_responses = None ):
117
122
self .wait_spi_char (START_CMD )
118
123
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 ()
120
128
responses = []
121
129
for num in range (num_responses ):
122
130
response = []
123
131
param_len = self .get_param ()
124
132
print ("parameter #%d length is %d" % (num , param_len ))
125
133
for j in range (param_len ):
126
134
response .append (self .get_param ())
127
- responses .append (response )
135
+ responses .append (bytes ( response ) )
128
136
self .check_data (END_CMD )
129
137
return responses
130
138
@@ -151,7 +159,7 @@ def get_firmware_version(self):
151
159
self .spi_slave_select ()
152
160
resp = self .wait_response_cmd (GET_FW_VERSION_CMD , 1 )
153
161
self .slave_deselect ()
154
- return '' . join ([ chr ( c ) for c in resp [0 ]])
162
+ return resp [0 ]
155
163
156
164
def get_MAC (self ):
157
165
print ("MAC address" )
@@ -164,3 +172,37 @@ def get_MAC(self):
164
172
resp = self .wait_response_cmd (GET_MACADDR_CMD , 1 )
165
173
self .slave_deselect ()
166
174
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