-
Notifications
You must be signed in to change notification settings - Fork 0
/
is31fl3733.py
305 lines (250 loc) · 9.97 KB
/
is31fl3733.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from __future__ import print_function
from builtins import bytes
from .constants import *
from smbus2 import SMBus, i2c_msg
import time
class IS31FL3733DeviceNotFound(IOError):
pass
class IS31FL3733(object):
address = 0x50
busnum = 1
syncmode = REGISTER_FUNCTION_CONFIGURATION_SYNC_CLOCK_SINGLE
breathing = 0
softwareshutdown = 0
currentPage = PAGE_LEDONOFF
pixels = [[0] * 16 for i in range(12)]
triggerOpenShortDetection = 1
DEBUG = False
lastDebug = ""
name = "IS31FL3733"
chunkSize = 24
pixelsFlat = [0] * 192
incremental = False
chunksSent = 0
chunksSaved = 0
def debug(self, *args):
if self.DEBUG:
if not hasattr(self, "lastDebug"):
self.lastDebug = ""
if self.lastDebug != args:
print(self.name + ":", *args)
self.lastDebug = args
def __del__(self):
# self.smbus.close()
pass
def __init__(self, *args, **kwargs):
# Flags
if type (args) is not None:
for arg in args:
setattr(self,arg,True)
# key=value parameters
if type (kwargs) is not None:
for key, value in kwargs.items():
if type(value) is dict:
if getattr(self,key):
tempdict = getattr(self,key).copy()
tempdict.update(value)
value = tempdict
setattr(self,key,value)
self.smbus = SMBus(self.busnum)
try:
self.attemptDetection()
except IOError:
raise IS31FL3733DeviceNotFound('Could not communicate with device.')
except TypeError:
raise IS31FL3733DeviceNotFound('Device detection failed.')
self.reset()
self.setContrast(255)
self.triggerOpenShortDetection = True
self.setConfiguration()
self.pixelsFlat = [0] * 192
self.debug("Initialized.")
def attemptDetection(self):
# REGISTER_INTERRUPT_STATUS defaults to 0, can only be 0-3
if self.read(REGISTER_INTERRUPT_STATUS) > 3:
raise TypeError('REGISTER_INTERRUPT_STATUS is an invalid value--is this IS31FL3733?')
# 0xC0 is not a readable address in any of the registers
# It doesn't return a read error, but does always return 0
if self.read(0xC0) != 0:
raise TypeError('Was able to read an address that should not be readable (0xC0)--is this IS31FL3733?')
# later IS31FL37xx devices support this...
try:
idregister=self.read(REGISTER_ID)
if idregister != REGISTER_ID_VALUE_IS31FL3733:
raise TypeError('ID register value',idregister,'does not match IS31FL3733 value:',REGISTER_ID_VALUE_IS31FL3733)
except IOError:
# all's well.
pass
self.debug("IS31FL3733 device detected.")
return True
def selectPage(self,value):
if self.currentPage is not value:
self.debug("changing page to",value,"from",self.currentPage)
self.write(REGISTER_COMMAND_WRITE_LOCK,COMMAND_WRITE_LOCK_DISABLE_ONCE)
self.write(REGISTER_COMMAND,value)
self.currentPage = value
def setContrast(self,value):
self.selectPage(PAGE_FUNCTION)
self.write(REGISTER_FUNCTION_CURRENT_CONTROL,value)
def reset(self):
self.selectPage(PAGE_FUNCTION)
self.currentPage = PAGE_LEDONOFF
self.debug("reset got",self.read(REGISTER_FUNCTION_RESET))
self.pixelsFlat = [0] * 192
def enableAllPixels(self):
self.selectPage(PAGE_LEDONOFF)
self.writeBlock(0, [ 255 ] * 0x18 )
self.selectPage(PAGE_LEDPWM)
for i in range(0,12):
self.writeBlock(i*16,[ 255 ] * (16))
self.pixelsFlat = [255] * 192
def setPixelPower(self,row,col,val):
address = row*2 + (col > 7)
# This needs work
def setPixelPWM(self,row,col,val,immediate=True):
pixel = row*16 + col
self.pixels[row][col] = val
# self.debug(row*16,col,"=",row*16 + col)
if immediate:
self.selectPage(PAGE_LEDPWM)
self.write(pixel,val)
def setAllPixelsPWM(self,values):
# self.debug("length is",len(values))
self.selectPage(PAGE_LEDPWM)
# messageAddress = i2c_msg.write(self.address, [0])
# messageToSend = i2c_msg.write(self.address, values)
# self.smbus.i2c_rdwr(messageAddress,messageToSend)
# TODO set the values in the array
iterator = 0
messages = []
last = list(self.chunks(self.pixelsFlat, self.chunkSize))
incremental = self.incremental
chunksSent = 0
chunksSaved = 0
for index, chunk in enumerate(self.chunks(values, self.chunkSize)):
address = index * self.chunkSize
if (not incremental) or (list(last[index]) != list(chunk)):
messages.append(i2c_msg.write(self.address, bytes([address]) + bytes(chunk)))
chunksSent += 1
else:
chunksSaved += 1
self.chunksSent += chunksSent
self.chunksSaved += chunksSaved
if messages:
self.smbus.i2c_rdwr(*messages)
self.pixelsFlat = values
def setAllPixels(self,values):
self.debug("length is",len(values))
self.selectPage(PAGE_LEDONOFF)
self.writeBlock(0,values)
def setConfiguration(self):
self.selectPage(PAGE_FUNCTION)
regvalue = ( self.breathing * REGISTER_FUNCTION_CONFIGURATION_BREATHING_ENABLE ) | ( self.syncmode ) | ( ( not self.softwareshutdown ) * REGISTER_FUNCTION_CONFIGURATION_SOFTWARE_SHUTDOWN ) | ( self.triggerOpenShortDetection * REGISTER_FUNCTION_CONFIGURATION_TRIGGER_OPEN_SHORT_DETECTION )
self.triggerOpenShortDetection = False
self.write(REGISTER_FUNCTION_CONFIGURATION, regvalue)
def write(self,register,value):
self.smbus.write_byte_data(self.address,register,value)
def writeBlock(self,register,value):
self.smbus.write_i2c_block_data(self.address,register,value)
def read(self,register):
return self.smbus.read_byte_data(self.address,register)
def getOpenPixels(self):
self.triggerOpenShortDetection = 1
self.setConfiguration()
time.sleep(0.01)
self.selectPage(PAGE_LEDONOFF)
returners = []
for i in range(REGISTER_LEDONOFF_OPEN_START, REGISTER_LEDONOFF_OPEN_STOP + 1): # python range not inclusive
returners.append(self.read(i))
return returners
def getShortPixels(self):
self.triggerOpenShortDetection = 1
self.setConfiguration()
time.sleep(0.01)
self.selectPage(PAGE_LEDONOFF)
returners = []
for i in range(REGISTER_LEDONOFF_SHORT_START, REGISTER_LEDONOFF_SHORT_STOP + 1): # python range not inclusive
returners.append(self.read(i))
return returners
def chunks(self, values, length):
for i in range(0, len(values), length):
yield values[i:i + length]
def writeBuffer(self):
flat_list = [item for sublist in self.pixels for item in sublist]
self.setAllPixelsPWM(0,flat_list)
def sevenSegment(self, row, col, value, brightness=0):
if brightness:
self.selectPage(PAGE_LEDPWM)
self.writeBlock(0,[brightness]*8)
self.selectPage(PAGE_LEDONOFF)
bits = 0B00000000
if value == 0:
bits = 0B00111111
elif value == 1:
bits = 0B00000110
elif value == 2:
bits = 0B01011011
elif value == 3:
bits = 0B01001111
elif value == 4:
bits = 0B01100110
elif value == 5:
bits = 0B01101101
elif value == 6:
bits = 0B01111101
elif value == 7:
bits = 0B00000111
elif value == 8:
bits = 0B01111111
elif value == 9:
bits = 0B01101111
# self.debug(value)
# self.debug(str(bits))
# bits = 0b11111111 - bits
self.write(row*2 + col + REGISTER_LEDONOFF_ONOFF_START,bits)
if __name__ == '__main__':
for address in range(0x50,0x60):
print("trying",address)
try:
matrix = IS31FL3733(address=address, busnum=8, DEBUG=True)
matrix.setContrast(255)
print("powering on all pixels")
matrix.enableAllPixels()
time.sleep(2)
print("powering off all pixels via PWM register")
matrix.setAllPixelsPWM([0]*192)
print("let's try some 7-segment values (looks scrambled on most devices)")
for value in range(10):
print(value)
matrix.sevenSegment(0,0,value)
time.sleep(0.5)
time.sleep(2)
print("let's fade up from 0 to 10 on all pixels")
for value in range(10):
matrix.setAllPixelsPWM([value]*192)
print ("let's draw some rows and cols")
for row in range(12):
for col in range(16):
matrix.setPixelPWM(row,col, 2)
print("let's set some arbitrary pixels (check for adjacent shorts)")
for i in range(11):
matrix.setPixelPWM(i,i,40)
for i in range(11):
matrix.setPixelPWM(11-i,i,20)
matrix.setPixelPWM(0,0,100)
matrix.setPixelPWM(0,5,100)
matrix.setPixelPWM(1,6,100)
matrix.setPixelPWM(0,10,100)
matrix.setPixelPWM(11,11,100)
matrix.setPixelPWM(11,11,100)
matrix.setPixelPWM(6,11,100)
# matrix.setPixelPWM(3,12,3)
time.sleep(1);
print("all that done, now let's check for missing/short pixels.")
print("missing pixels")
print(matrix.getOpenPixels())
print("short pixels")
print(matrix.getShortPixels())
except Exception as e:
print("Address",address,"error:",e)
time.sleep(0.1)