1- from sys import exit
2- from machine import I2C
3- from time import sleep
4-
5- pinstrap_map = {
6- 0x3C : "BUZZER" ,
7- 0x7C : "BUTTONS" ,
8- 0x76 : "ENCODER" ,
9- 0x74 : "ENCODER" ,
10- 0x6C : "SMARTLEDS"
11- }
1+ """
2+ This example shows how to change the I2C address of a Modulino device.
3+ After changing the address, the device will be reset and the new address will be verified.
4+ From then on, when creating a Modulino object, you should use the new address.
5+ e.g. ModulinoBuzzer(address=0x2A)
126
13- def pinstrap_to_name (address ):
14- if address in pinstrap_map :
15- return pinstrap_map [address ]
16- return "UNKNOWN"
7+ Initial author: Sebastian Romero ([email protected] ) 8+ """
179
18- bus = I2C (0 )
19- devices_on_bus = bus .scan ()
10+ from sys import exit
11+ from time import sleep
12+ from modulino import Modulino
2013
2114print ()
15+ devices = Modulino .available_devices ()
2216
23- if len (devices_on_bus ) == 0 :
17+ if len (devices ) == 0 :
2418 print ("No devices found on the bus. Try resetting the board." )
2519 exit (1 )
2620
2721print ("The following devices were found on the bus:" )
2822
29- for index , device_address in enumerate (devices_on_bus ):
30- pinstrap_address = bus .readfrom (device_address , 1 )
31- device_name = pinstrap_to_name (pinstrap_address [0 ])
32- print (f"{ index + 1 } ) { device_name } at { hex (device_address )} " )
23+ for index , device in enumerate (devices ):
24+ print (f"{ index + 1 } ) { device .device_type } at { hex (device .address )} " )
3325
3426choice = int (input ("\n Enter the device number for which you want to change the address: " ))
35- if choice < 1 or choice > len (devices_on_bus ):
27+
28+ if choice < 1 or choice > len (devices ):
3629 print ("Invalid choice. Please select a valid device number." )
3730 exit (1 )
3831
39- selected_device_address = devices_on_bus [choice - 1 ]
40-
41- # Read address from user input
32+ selected_device = devices [choice - 1 ]
4233new_address = int (input ("Enter the new address (hexadecimal or decimal): " ), 0 )
4334
4435if new_address < 0 or new_address > 127 :
4536 print ("Invalid address. Address must be between 0 and 127" )
4637 exit (1 )
4738
48- print (f"Changing address of device at { hex (selected_device_address )} to { hex (new_address )} ..." )
49-
50- data = bytearray (40 )
51- # Set the first two bytes to 'C' and 'F' followed by the new address
52- data [0 :2 ] = b'CF'
53- data [2 ] = new_address * 2
54-
55- try :
56- bus .writeto (selected_device_address , data )
57- except OSError :
58- pass # Device resets immediately and causes ENODEV to be thrown which is expected
59- sleep (1 )
39+ print (f"Changing address of device at { hex (selected_device .address )} to { hex (new_address )} ..." )
40+ selected_device .change_address (new_address )
41+ sleep (1 ) # Give the device time to reset
6042
6143# Check if the address was successfully changed
62- devices_on_bus = bus .scan ()
63- if new_address in devices_on_bus :
44+ if selected_device .connected :
6445 print (f"✅ Address changed successfully to { hex (new_address )} " )
6546else :
6647 print ("❌ Failed to change address. Please try again." )
0 commit comments