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 , 0 , data_list )
14-
15- < << << << HEAD : scripts / masterI2C . py
13+ bus.write_i2c_block_data(SLAVE_ADDRESS, 48 , data_list)
14+ """
15+ """
1616import struct
1717
1818def read_data(length):
@@ -24,22 +24,33 @@ def read_data(length):
2424 return float_value
2525 else:
2626 raise ValueError("Not enough data received from I2C bus")
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
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+
3444
3545if __name__ == "__main__" :
3646 try :
3747 # Send data to the slave
38- write_data ("Hello Slave!" )
48+ write_data (23 )
49+ print ("sending value: " + str (23 ))
3950 time .sleep (1 ) # Wait for the slave to process the data
4051
4152 # Request data from the slave
42- received = read_data (13 ) # Adjust length as needed
53+ received = read_data (SLAVE_ADDRESS ) # Adjust length as needed
4354 print ("Received from slave:" , received )
4455
4556 except Exception as e :
0 commit comments