28
28
29
29
* Author(s): Damien P. George, Limor Fried & Scott Shawcroft
30
30
"""
31
- import math
32
-
33
31
import busio
34
32
import digitalio
35
33
@@ -160,15 +158,17 @@ def _set_item(self, index, value):
160
158
rgb = (value >> 16 , (value >> 8 ) & 0xff , value & 0xff )
161
159
162
160
if len (value ) == 4 :
163
- brightness = value [- 1 ]
164
- rgb = value [: 3 ]
161
+ brightness = value [3 ]
162
+ # Ignore value[3] below.
165
163
else :
166
164
brightness = 1
167
165
168
166
# LED startframe is three "1" bits, followed by 5 brightness bits
169
167
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and
170
168
# vary based on hardware
171
- brightness_byte = math .ceil (brightness * 31 ) & 0b00011111
169
+ # same as math.ceil(brightness * 31) & 0b00011111
170
+ # Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions
171
+ brightness_byte = 32 - int (32 - brightness * 31 ) & 0b00011111
172
172
self ._buf [offset ] = brightness_byte | LED_START
173
173
self ._buf [offset + 1 ] = rgb [self .pixel_order [0 ]]
174
174
self ._buf [offset + 2 ] = rgb [self .pixel_order [1 ]]
@@ -179,7 +179,9 @@ def __setitem__(self, index, val):
179
179
start , stop , step = index .indices (self ._n )
180
180
length = stop - start
181
181
if step != 0 :
182
- length = math .ceil (length / step )
182
+ # same as math.ceil(length / step)
183
+ # Idea from https://fizzbuzzer.com/implement-a-ceil-function/
184
+ length = (length + step - 1 ) // step
183
185
if len (val ) != length :
184
186
raise ValueError ("Slice and input sequence size do not match." )
185
187
for val_i , in_i in enumerate (range (start , stop , step )):
@@ -223,7 +225,7 @@ def fill(self, color):
223
225
"""Colors all pixels the given ***color***."""
224
226
auto_write = self .auto_write
225
227
self .auto_write = False
226
- for i , _ in enumerate (self ):
228
+ for i in range (self . _n ):
227
229
self [i ] = color
228
230
if auto_write :
229
231
self .show ()
0 commit comments