1+ #!/usr/bin/python3
2+
3+ import RPi .GPIO as GPIO
4+
5+ from subprocess import call
6+ from sys import exit
7+ from time import sleep
8+
9+ DELAY = 0.005
10+
11+ SCK = 3
12+ SDA = 2
13+
14+ try :
15+ old_address = int (input ('Old address: ' ))
16+ new_address = int (input ('New address: ' ))
17+ if old_address > 7 or old_address < 0 or new_address > 7 or new_address < 0 :
18+ raise
19+ except :
20+ print ('Invalid address' )
21+ print ('Address must be between 0 and 7' )
22+ exit (1 )
23+
24+ #if old_address > 0x67 or old_address < 0x60 or new_address > 0x67 or new_address < 0x60:
25+ # print('Invalid address')
26+ # print('Address must be between 0x60 and 0x67')
27+ # exit(1)
28+
29+ GPIO .setmode (GPIO .BCM ) # Use RPi GPIO numbers
30+ GPIO .setwarnings (False ) # disable warnings
31+
32+ GPIO .setup (SCK , GPIO .OUT )
33+ GPIO .setup (SDA , GPIO .OUT )
34+ GPIO .output (SCK , 1 )
35+ GPIO .output (SDA , 1 )
36+
37+ def i2c_ack_byte ():
38+ GPIO .setup (SDA , GPIO .IN )
39+ GPIO .output (SCK , 1 )
40+ sleep (DELAY )
41+ GPIO .output (SCK , 0 )
42+ GPIO .setup (SDA , GPIO .OUT )
43+
44+ def i2c_write_byte (byte ):
45+ for i in range (8 ):
46+ sleep (DELAY )
47+ GPIO .output (SCK , 0 )
48+
49+ if byte & 0x80 :
50+ GPIO .output (SDA , 1 )
51+ else :
52+ GPIO .output (SDA , 0 )
53+
54+ sleep (DELAY )
55+ GPIO .output (SCK , 1 )
56+
57+ byte <<= 1
58+
59+ sleep (DELAY )
60+ GPIO .output (SCK , 0 )
61+ GPIO .output (SDA , 0 )
62+
63+
64+ data = [
65+ (old_address + 0x60 ) << 1 ,
66+ 0x61 | (old_address << 2 ),
67+ 0x62 | (new_address << 2 ),
68+ 0x63 | (new_address << 2 )
69+ ]
70+
71+ GPIO .output (SDA , 0 )
72+ sleep (DELAY )
73+
74+ i2c_write_byte (data [0 ])
75+ i2c_ack_byte ()
76+
77+ i2c_write_byte (data [1 ])
78+ input ('Insert jumper J1 and press enter' )
79+ i2c_ack_byte ()
80+
81+ i2c_write_byte (data [2 ])
82+ i2c_ack_byte ()
83+
84+ i2c_write_byte (data [3 ])
85+ i2c_ack_byte ()
86+
87+ GPIO .output (SDA , 0 )
88+
89+ sleep (DELAY )
90+ GPIO .output (SCK , 1 )
91+
92+ GPIO .output (SDA , 1 )
93+
94+ GPIO .cleanup ()
95+ print ('Attempting to reset I2C bus...' )
96+ call (['modprobe' , '-r' , 'i2c_bcm2835' ])
97+ call (['modprobe' , 'i2c_bcm2835' ])
98+
99+ print ('Done! Remove the jumper' )
0 commit comments