1111from time import sleep
1212from modulino import Modulino
1313
14- print ()
15- bus = None # Change this to the I2C bus you are using on 3rd party host boards
16- devices = Modulino .available_devices (bus )
14+ def main ():
15+ print ()
16+ bus = None # Change this to the I2C bus you are using on 3rd party host boards
17+ devices = Modulino .available_devices (bus )
1718
18- if len (devices ) == 0 :
19- print ("No devices found on the bus. Try resetting the board." )
20- exit ( 1 )
19+ if len (devices ) == 0 :
20+ print ("No devices found on the bus. Try resetting the board." )
21+ return
2122
22- print ("The following devices were found on the bus:" )
23+ print ("The following devices were found on the bus:" )
2324
24- for index , device in enumerate (devices ):
25- print (f"{ index + 1 } ) { device .device_type } at { hex (device .address )} " )
25+ for index , device in enumerate (devices ):
26+ print (f"{ index + 1 } ) { device .device_type } at { hex (device .address )} " )
2627
27- choice = int (input ("\n Enter the device number for which you want to change the address: " ))
28+ choice_is_valid = False
29+ while not choice_is_valid :
30+ try :
31+ choice = int (input ("\n Enter the device number for which you want to change the address: " ))
32+ except ValueError :
33+ print ("Invalid input. Please enter a valid device number." )
34+ continue
35+
36+ if choice < 1 or choice > len (devices ):
37+ print ("Invalid choice. Please select a valid device number." )
38+ else :
39+ choice_is_valid = True
2840
29- if choice < 1 or choice > len (devices ):
30- print ("Invalid choice. Please select a valid device number." )
31- exit (1 )
41+ selected_device = devices [choice - 1 ]
3242
33- selected_device = devices [choice - 1 ]
34- new_address = int (input ("Enter the new address (hexadecimal or decimal): " ), 0 )
3543
36- if new_address < 0 or new_address > 127 :
37- print ("Invalid address. Address must be between 0 and 127" )
38- exit (1 )
44+ new_address_is_valid = False
45+ while not new_address_is_valid :
46+ try :
47+ new_address = int (input ("Enter the new address (hexadecimal or decimal): " ), 0 )
48+ except ValueError :
49+ print ("Invalid input. Please enter a valid hexadecimal (e.g., 0x2A) or decimal (e.g., 42) address." )
50+ continue
3951
40- if new_address == 100 :
41- print ("The address 0x64 is reserved for bootloader mode. Please choose a different address." )
42- exit (1 )
52+ if new_address < 1 or new_address > 127 :
53+ print ("Invalid address. Address must be between 1 and 127" )
54+ elif new_address == 100 :
55+ print ("The address 0x64 (100) is reserved for bootloader mode. Please choose a different address." )
56+ else :
57+ new_address_is_valid = True
4358
44- print (f"Changing address of device at { hex (selected_device .address )} to { hex (new_address )} ..." )
45- selected_device .change_address (new_address )
46- sleep (1 ) # Give the device time to reset
59+ print (f"Changing address of device at { hex (selected_device .address )} to { hex (new_address )} ..." )
60+ selected_device .change_address (new_address )
61+ sleep (1 ) # Give the device time to reset
4762
48- # Check if the address was successfully changed
49- if selected_device .connected :
50- print (f"✅ Address changed successfully to { hex (new_address )} " )
51- else :
52- print ("❌ Failed to change address. Please try again." )
63+ # Check if the address was successfully changed
64+ if selected_device .connected :
65+ print (f"✅ Address changed successfully to { hex (new_address )} " )
66+ else :
67+ print ("❌ Failed to change address. Please try again." )
68+
69+ if __name__ == "__main__" :
70+ try :
71+ main ()
72+ except KeyboardInterrupt :
73+ print ("\Aborted by user" )
0 commit comments