Skip to content

Commit e465eb3

Browse files
committed
Improve PI-SPI-DIN-4AO support
1 parent aa60379 commit e465eb3

2 files changed

Lines changed: 104 additions & 5 deletions

File tree

python/pi_spi_din_4ao_prog.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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')

python/widgetlords/pi_spi_din.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def read_single(self, channel: int):
5151
__all__.append('Mod8AI')
5252

5353
class Mod4AO:
54-
def __init__(self):
55-
widgetlords.pi_spi_din_4ao_init()
54+
def __init__(self, address: int = 0):
55+
self.address = address
5656

57-
def write_single(self, channel: int, counts: int, address = 0):
58-
widgetlords.pi_spi_din_4ao_write_single(address, channel, counts)
57+
def write_single(self, channel: int, counts: int):
58+
widgetlords.pi_spi_din_4ao_write_single(self.address, channel, counts)
5959
__all__.append('Mod4AO')
60-
60+
6161
class Mod4Freq:
6262
def __init__(self, chip_enable: ChipEnable, address: int = 0):
6363
self.chip_enable = chip_enable.value

0 commit comments

Comments
 (0)