1
+ import struct
1
2
import time
2
3
import board
3
4
import busio
@@ -8,6 +9,8 @@ class ESP_SPIcontrol:
8
9
GET_CONN_STATUS_CMD = const (0x20 )
9
10
GET_MACADDR_CMD = const (0x22 )
10
11
SCAN_NETWORKS = const (0x27 )
12
+ GET_IDX_RSSI_CMD = const (0x32 )
13
+ GET_IDX_ENCT_CMD = const (0x33 )
11
14
START_SCAN_NETWORKS = const (0x36 )
12
15
GET_FW_VERSION_CMD = const (0x37 )
13
16
@@ -94,8 +97,11 @@ def send_command(self, cmd, params=None):
94
97
print ("packet len:" , len (packet ))
95
98
while len (packet ) % 4 != 0 :
96
99
packet .append (0xFF )
100
+
101
+ self .wait_for_slave_select ()
97
102
self ._spi .write (bytearray (packet ))
98
103
print ("Wrote: " , [hex (b ) for b in packet ])
104
+ self .slave_deselect ()
99
105
100
106
def get_param (self ):
101
107
self ._spi .readinto (self ._pbuf )
@@ -119,6 +125,9 @@ def check_data(self, desired):
119
125
raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
120
126
121
127
def wait_response_cmd (self , cmd , num_responses = None ):
128
+ self .wait_for_slave_ready ()
129
+ self .spi_slave_select ()
130
+
122
131
self .wait_spi_char (START_CMD )
123
132
self .check_data (cmd | REPLY_FLAG )
124
133
if num_responses is not None :
@@ -134,68 +143,49 @@ def wait_response_cmd(self, cmd, num_responses=None):
134
143
response .append (self .get_param ())
135
144
responses .append (bytes (response ))
136
145
self .check_data (END_CMD )
146
+
147
+ self .slave_deselect ()
137
148
return responses
138
149
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
+
139
154
def get_connection_status (self ):
140
155
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 )
149
157
print ("Status:" , resp [0 ][0 ])
150
158
return resp [0 ][0 ] # one byte response
151
159
152
160
def get_firmware_version (self ):
153
161
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 )
162
163
return resp [0 ]
163
164
164
165
def get_MAC (self ):
165
166
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 ' ])
174
168
return resp [0 ]
175
169
176
170
def start_scan_networks (self ):
177
171
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 )
186
173
if resp [0 ][0 ] != 1 :
187
174
raise RuntimeError ("Failed to start AP scan" )
188
175
189
176
def get_scan_networks (self ):
190
- self .wait_for_slave_select ()
191
177
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
199
189
200
190
def scan_networks (self ):
201
191
self .start_scan_networks ()
0 commit comments