#NEWLINE# def scan_i2c( self , debug = False ) : #NEWLINE# #INDENT# 'scans i2c port returning a list of detected addresses.\n            Requires sudo access.\n            Returns True for in use by a device already (ie UU observed)' #NEWLINE# import raspberrypi #NEWLINE# proc = subprocess . Popen ( [ 'sudo' , 'i2cdetect' , '-y' , raspberrypi . i2c_bus_num ( ) ] , stdout = subprocess . PIPE , close_fds = True ) #NEWLINE# ( std_out_txt , std_err_txt ) = proc . communicate ( ) #NEWLINE# if debug : #NEWLINE# #INDENT# logging . debug ( std_out_txt ) #NEWLINE# logging . debug ( std_err_txt ) #NEWLINE# #UNINDENT# addr = [ ] #NEWLINE# lines = std_out_txt . rstrip ( ) . split ( '\n' ) #NEWLINE# if ( lines [ 0 ] in 'command not found' ) : #NEWLINE# #INDENT# raise RuntimeError ( 'i2cdetect not found' ) #NEWLINE# #UNINDENT# for i in range ( 0 , 8 ) : #NEWLINE# #INDENT# for j in range ( 0 , 16 ) : #NEWLINE# #INDENT# idx_i = ( i + 1 ) #NEWLINE# idx_j = ( ( j * 3 ) + 4 ) #NEWLINE# cell = lines [ idx_i ] [ idx_j : ( idx_j + 2 ) ] . strip ( ) #NEWLINE# if ( cell and ( cell != '--' ) ) : #NEWLINE# #INDENT# logging . info ( '    ...device at: %s %s' , hex ( ( ( 16 * i ) + j ) ) , cell ) #NEWLINE# hexAddr = ( ( 16 * i ) + j ) #NEWLINE# if ( cell == 'UU' ) : #NEWLINE# #INDENT# addr . append ( [ hexAddr , True ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# addr . append ( [ hexAddr , False ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return addr