@@ -578,11 +578,11 @@ def _i2c_write_bytes(self, data):
578578 for byte in data :
579579 # Write byte.
580580 self ._command .append (str (bytearray ((0x11 , 0x00 , 0x00 , byte ))))
581- # Read bit for ACK/NAK.
582- self ._command .append ('\x22 \x00 ' )
583581 # Make sure pins are back in idle state with clock low and data high.
584582 self ._ft232h .output_pins ({0 : GPIO .LOW , 1 : GPIO .HIGH }, write = False )
585583 self ._command .append (self ._ft232h .mpsse_gpio () * _REPEAT_DELAY )
584+ # Read bit for ACK/NAK.
585+ self ._command .append ('\x22 \x00 ' )
586586 # Increase expected response bytes.
587587 self ._expected += len (data )
588588
@@ -603,6 +603,21 @@ def _verify_acks(self, response):
603603 if byte & 0x01 != 0x00 :
604604 raise RuntimeError ('Failed to find expected I2C ACK!' )
605605
606+ def ping (self ):
607+ """Attempt to detect if a device at this address is present on the I2C
608+ bus. Will send out the device's address for writing and verify an ACK
609+ is received. Returns true if the ACK is received, and false if not.
610+ """
611+ self ._idle ()
612+ self ._transaction_start ()
613+ self ._i2c_start ()
614+ self ._i2c_write_bytes ([self ._address_byte (False )])
615+ self ._i2c_stop ()
616+ response = self ._transaction_end ()
617+ if len (response ) != 1 :
618+ raise RuntimeError ('Expected 1 response byte but received {0} byte(s).' .format (len (response )))
619+ return ((response [0 ] & 0x01 ) == 0x00 )
620+
606621 def writeRaw8 (self , value ):
607622 """Write an 8-bit value on the bus (without register)."""
608623 value = value & 0xFF
0 commit comments