66
77# I2C address of the slave
88SLAVE_ADDRESS = 0x08
9- """
9+
1010def write_data (data ):
1111 # Convert string to list of ASCII values
1212 data_list = [ord (char ) for char in data ]
13- bus.write_i2c_block_data(SLAVE_ADDRESS, 48 , data_list)
14- """
15- """
13+ bus .write_i2c_block_data (SLAVE_ADDRESS , 0 , data_list )
14+
15+ < << << << HEAD : scripts / masterI2C . py
1616import struct
1717
1818def read_data (length ):
@@ -24,33 +24,22 @@ def read_data(length):
2424 return float_value
2525 else :
2626 raise ValueError ("Not enough data received from I2C bus" )
27- """
28-
29-
30- def write_data (data ):
31- try :
32- bus .write_byte (I2C_SLAVE_ADDRESS , value_to_send )
33- print ("sending value:" , value_to_send )
34- except Exception as e :
35- print ("Erreur écriture:" , e )
36-
37- def read_data (addr ):
38- try :
39- n = bus .read_byte (I2C_SLAVE_ADDRESS )
40- print ("new received value:" , n )
41- except Exception as e :
42- print ("Erreur lecture:" , e )
43-
27+ == == == =
28+ def read_data (length ):
29+ # Read a block of data from the slave
30+ data = bus .read_i2c_block_data (SLAVE_ADDRESS , 0 , length )
31+ # Convert list of ASCII values to string
32+ return '' .join (chr (byte ) for byte in data )
33+ > >> >> >> origin / oled_annimation :HL / masterI2C .py
4434
4535if __name__ == "__main__" :
4636 try :
4737 # Send data to the slave
48- write_data (23 )
49- print ("sending value: " + str (23 ))
38+ write_data ("Hello Slave!" )
5039 time .sleep (1 ) # Wait for the slave to process the data
5140
5241 # Request data from the slave
53- received = read_data (SLAVE_ADDRESS ) # Adjust length as needed
42+ received = read_data (13 ) # Adjust length as needed
5443 print ("Received from slave:" , received )
5544
5645 except Exception as e :
0 commit comments