@@ -22,6 +22,7 @@ class ESP_SPIcontrol:
2222 REQ_HOST_BY_NAME_CMD = const (0x34 )
2323 GET_HOST_BY_NAME_CMD = const (0x35 )
2424 START_SCAN_NETWORKS = const (0x36 )
25+ PING_CMD = const (0x3E )
2526
2627 GET_FW_VERSION_CMD = const (0x37 )
2728
@@ -54,7 +55,6 @@ def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False)
5455 self ._reset .direction = Direction .OUTPUT
5556 self ._gpio0 .direction = Direction .OUTPUT
5657
57- print ("Nina reset" )
5858 self ._gpio0 .value = True # not bootload mode
5959 self ._cs .value = True
6060 self ._reset .value = False
@@ -278,9 +278,17 @@ def pretty_ip(self, ip):
278278 def get_host_by_name (self , hostname ):
279279 if isinstance (hostname , str ):
280280 hostname = bytes (hostname , 'utf-8' )
281- self ._debug = True
282281 resp = self .send_command_get_response (REQ_HOST_BY_NAME_CMD , [hostname ])
283282 if resp [0 ][0 ] != 1 :
284283 raise RuntimeError ("Failed to request hostname" )
285284 resp = self .send_command_get_response (GET_HOST_BY_NAME_CMD )
286285 return resp [0 ]
286+
287+ def ping (self , dest , ttl = 250 ):
288+ self ._debug = True
289+ if isinstance (dest , str ): # convert to IP address
290+ dest = self .get_host_by_name (dest )
291+ # ttl must be between 0 and 255
292+ ttl = max (0 , min (ttl , 255 ))
293+ resp = self .send_command_get_response (PING_CMD , [dest , [ttl ]])
294+ return struct .unpack ('<H' , resp [0 ])[0 ]
0 commit comments