@@ -92,11 +92,11 @@ class IS31FL3731:
92
92
width = 16
93
93
height = 9
94
94
95
- def __init__ (self , i2c , address = 0x74 ):
95
+ def __init__ (self , i2c , address = 0x74 , frames = None ):
96
96
self .i2c = i2c
97
97
self .address = address
98
98
self ._frame = None
99
- self ._init ()
99
+ self ._init (frames = frames )
100
100
101
101
def _i2c_read_reg (self , reg , result ):
102
102
# Read a buffer of data from the specified 8-bit I2C register address.
@@ -111,19 +111,21 @@ def _i2c_read_reg(self, reg, result):
111
111
self .i2c .unlock ()
112
112
return None
113
113
114
- def _i2c_write_reg (self , reg , data ):
115
- # Write a buffer of data (byte array) to the specified I2C register
116
- # address.
114
+ def _i2c_write_block (self , data ):
115
+ # Writes a contiguous block of data (bytearray) where the first byte
116
+ # is the starting I2C register address (register is not an argument) .
117
117
while not self .i2c .try_lock ():
118
118
pass
119
119
try :
120
- buf = bytearray (1 )
121
- buf [0 ] = reg
122
- buf .extend (data )
123
- self .i2c .writeto (self .address , buf )
120
+ self .i2c .writeto (self .address , data )
124
121
finally :
125
122
self .i2c .unlock ()
126
123
124
+ def _i2c_write_reg (self , reg , data ):
125
+ # Write a contiguous block of data (bytearray) starting at the
126
+ # specified I2C register address (register passed as argument).
127
+ self ._i2c_write_block (bytes ([reg ]) + data )
128
+
127
129
def _bank (self , bank = None ):
128
130
if bank is None :
129
131
result = bytearray (1 )
@@ -142,16 +144,21 @@ def _register(self, bank, register, value=None):
142
144
def _mode (self , mode = None ):
143
145
return self ._register (_CONFIG_BANK , _MODE_REGISTER , mode )
144
146
145
- def _init (self ):
147
+ def _init (self , frames = None ):
146
148
self .sleep (True )
147
- time .sleep (0.01 ) # 10 MS pause to reset.
148
- self ._mode (_PICTURE_MODE )
149
- self .frame (0 )
150
- for frame in range (8 ):
151
- self .fill (0 , False , frame = frame )
152
- for col in range (18 ):
153
- self ._register (frame , _ENABLE_OFFSET + col , 0xFF )
154
- self .audio_sync (False )
149
+ # Clear config; sets to Picture Mode, no audio sync, maintains sleep
150
+ self ._bank (_CONFIG_BANK )
151
+ self ._i2c_write_block (bytes ([0 ] * 14 ))
152
+ enable_data = bytes ([_ENABLE_OFFSET ] + [255 ] * 18 )
153
+ fill_data = bytearray ([0 ] * 25 )
154
+ # Initialize requested frames, or all 8 if unspecified
155
+ for frame in frames if frames else range (8 ):
156
+ self ._bank (frame )
157
+ self ._i2c_write_block (enable_data ) # Set all enable bits
158
+ for row in range (6 ): # Barebones quick fill() w/0
159
+ fill_data [0 ] = _COLOR_OFFSET + row * 24
160
+ self ._i2c_write_block (fill_data )
161
+ self ._frame = 0 # To match config bytes above
155
162
self .sleep (False )
156
163
157
164
def reset (self ):
0 commit comments