-
Notifications
You must be signed in to change notification settings - Fork 1
/
SX1509.py
160 lines (141 loc) · 6.25 KB
/
SX1509.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import wiringpi
class SX1509:
REGISTERS = {
'CHECK': 0x13,
'RESET': 0x7D,
'PIN_DIRECTION': 0x0E,
'PIN_DATA': 0x10,
'CLOCK': 0x1E,
'MISC': 0x1F,
'DISABLE_INPUT_BUFFER': 0x00,
'PULLUP_RESISTORS': 0x06,
'LED_DRIVER': 0x20,
'PWM_INTENSITY': [0x2A, 0x2D, 0x30, 0x33, 0x36, 0x3B, 0x40, 0x45, 0x4A, 0x4D, 0x50, 0x53, 0x56, 0x5B, 0x60, 0x65]
}
PIN_MODE = {
'OUTPUT': 0,
'INPUT': 1,
'PWM': 3
}
PIN_STATE = {
'LOW': 0,
'HIGH': 1
}
def __init__(self, address=0x3E, interruptPin=None, resetPin=None, oscillatorPin=None):
self.address = address
self.interruptPin = interruptPin
self.resetPin = resetPin
self.oscillatorPin = oscillatorPin
wiringpi.wiringPiSetupSys()
self.i2c = wiringpi.I2C()
self.device = self.i2c.setup(self.address)
data = [0x00, 0x00]
data[0] = self.i2c.readReg8(self.device, self.REGISTERS['CHECK'])
data[1] = self.i2c.readReg8(self.device, self.REGISTERS['CHECK']+1)
if data[0] != 0xFF or data[1] != 0x00:
raise Error('SX1509 not detected!')
self.reset(False)
def reset(self, hardware):
if hardware:
if self.resetPin == None:
raise ValueError('SX1509 cannot hardware reset when .resetPin is None')
else:
wiringpi.pinMode(self.resetPin, 1)
wiringpi.digitalWrite(self.resetPin, 0)
wiringpi.digitalWrite(self.resetPin, 1)
else:
self.i2c.writeReg8(self.device, self.REGISTERS['RESET'], 0x12)
self.i2c.writeReg8(self.device, self.REGISTERS['RESET'], 0x34)
def startInternalClock(self):
clockStatus = self.i2c.readReg8(self.device, self.REGISTERS['CLOCK'])
clockStatus = [0x00, clockStatus]
miscStatus = self.i2c.readReg8(self.device, self.REGISTERS['MISC'])
miscStatus = [0x00, miscStatus]
clockStatus = self.useBitMask(clockStatus, 5, False)
clockStatus = self.useBitMask(clockStatus, 6, True)
miscStatus = self.useBitMask(miscStatus, 6, False)
miscStatus = self.useBitMask(miscStatus, 5, False)
miscStatus = self.useBitMask(miscStatus, 4, True)
print(clockStatus)
print(miscStatus)
self.i2c.writeReg8(self.device, self.REGISTERS['MISC'], miscStatus[1])
self.i2c.writeReg8(self.device, self.REGISTERS['CLOCK'], clockStatus[1])
def setDisableInputBuffer(self, pin, disableInputBuffer):
disableInputBufferStatus = [0x00, 0x00]
disableInputBufferStatus[0] = self.i2c.readReg8(self.device, self.REGISTERS['DISABLE_INPUT_BUFFER'])
disableInputBufferStatus[1] = self.i2c.readReg8(self.device, self.REGISTERS['DISABLE_INPUT_BUFFER'] + 1)
disableInputBufferStatus = self.useBitMask(disableInputBufferStatus, pin, disableInputBuffer)
self.i2c.writeReg8(self.device, self.REGISTERS['DISABLE_INPUT_BUFFER'], disableInputBufferStatus[0])
self.i2c.writeReg8(self.device, self.REGISTERS['DISABLE_INPUT_BUFFER'] + 1, disableInputBufferStatus[1])
def setPullupResistor(self, pin, pullupResistorOn):
pullupResistorStatus = [0x00, 0x00]
pullupResistorStatus[0] = self.i2c.readReg8(self.device, self.REGISTERS['PULLUP_RESISTORS'])
pullupResistorStatus[1] = self.i2c.readReg8(self.device, self.REGISTERS['PULLUP_RESISTORS'] + 1)
pullupResistorStatus = self.useBitMask(pullupResistorStatus, pin, pullupResistorOn)
self.i2c.writeReg8(self.device, self.REGISTERS['PULLUP_RESISTORS'], pullupResistorStatus[0])
self.i2c.writeReg8(self.device, self.REGISTERS['PULLUP_RESISTORS'] + 1, pullupResistorStatus[1])
def setPinDirection(self, pin, direction):
currentPinState = [0x00, 0x00]
currentPinState[0] = self.i2c.readReg8(self.device, self.REGISTERS['PIN_DIRECTION'])
currentPinState[1] = self.i2c.readReg8(self.device, self.REGISTERS['PIN_DIRECTION'] + 1)
bitOn = True
if direction == self.PIN_MODE['OUTPUT']:
bitOn = False
newPinState = self.useBitMask(currentPinState, pin, bitOn)
self.i2c.writeReg8(self.device, self.REGISTERS['PIN_DIRECTION'], newPinState[0])
self.i2c.writeReg8(self.device, self.REGISTERS['PIN_DIRECTION'] + 1, newPinState[1])
def setDigitalPinValue(self, pin, value):
currentPinState = [0x00, 0x00]
currentPinState[0] = self.i2c.readReg8(self.device, self.REGISTERS['PIN_DATA'])
currentPinState[1] = self.i2c.readReg8(self.device, self.REGISTERS['PIN_DATA'] + 1)
bitOn = True
if not bitOn == 1:
bitOn = False
newPinState = self.useBitMask(currentPinState, pin, bitOn)
self.i2c.writeReg8(self.device, self.REGISTERS['PIN_DATA'], newPinState[0])
self.i2c.writeReg8(self.device, self.REGISTERS['PIN_DATA'] + 1, newPinState[1])
def enableLEDDriver(self, pin, LEDDriverOn):
ledDriverState = [0x00, 0x00]
ledDriverState[0] = self.i2c.readReg8(self.device, self.REGISTERS['LED_DRIVER'])
ledDriverState[1] = self.i2c.readReg8(self.device, self.REGISTERS['LED_DRIVER'] + 1)
ledDriverState = self.useBitMask(ledDriverState, pin, LEDDriverOn)
self.i2c.writeReg8(self.device, self.REGISTERS['LED_DRIVER'], ledDriverState[0])
self.i2c.writeReg8(self.device, self.REGISTERS['LED_DRIVER'] + 1, ledDriverState[1])
def enablePWMPin(self, pin):
self.startInternalClock()
self.setDisableInputBuffer(pin, True)
self.setPullupResistor(pin, True)
self.setPinDirection(pin, self.PIN_MODE['OUTPUT'])
self.setDigitalPinValue(pin, 0)
self.startInternalClock()
self.enableLEDDriver(pin, True)
def enablePWMPins(self, pins):
self.startInternalClock()
for pin in pins:
self.setDisableInputBuffer(pin, True)
for pin in pins:
self.setPullupResistor(pin, True)
for pin in pins:
self.setPinDirection(pin, self.PIN_MODE['OUTPUT'])
for pin in pins:
self.setDigitalPinValue(pin, 0)
self.startInternalClock()
for pin in pins:
self.enableLEDDriver(pin, True)
def setPWMPinValue(self, pin, value):
# byteValue = 0xFF & value
self.i2c.writeReg8(self.device, self.REGISTERS['PWM_INTENSITY'][pin], value)
def useBitMask(self, currentState, bit, bitOn):
mask = [0x00, 0x00]
maskBase = 0x0000
maskBase |= (1 << bit)
if not bitOn:
maskBase = ~maskBase
highByte = maskBase >> 8
if bitOn:
mask[0] = currentState[0] | highByte
mask[1] = currentState[1] | maskBase
else:
mask[0] = currentState[0] & highByte
mask[1] = currentState[1] & maskBase
return mask