Skip to content

Commit 21f44ff

Browse files
dhalbertmattytrentini
authored andcommitted
remove math dependency; other speedups
1 parent 5ffcf98 commit 21f44ff

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

adafruit_dotstar.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
2929
* Author(s): Damien P. George, Limor Fried & Scott Shawcroft
3030
"""
31-
import math
32-
3331
import busio
3432
import digitalio
3533

@@ -160,15 +158,17 @@ def _set_item(self, index, value):
160158
rgb = (value >> 16, (value >> 8) & 0xff, value & 0xff)
161159

162160
if len(value) == 4:
163-
brightness = value[-1]
164-
rgb = value[:3]
161+
brightness = value[3]
162+
# Ignore value[3] below.
165163
else:
166164
brightness = 1
167165

168166
# LED startframe is three "1" bits, followed by 5 brightness bits
169167
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and
170168
# 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
172172
self._buf[offset] = brightness_byte | LED_START
173173
self._buf[offset + 1] = rgb[self.pixel_order[0]]
174174
self._buf[offset + 2] = rgb[self.pixel_order[1]]
@@ -179,7 +179,9 @@ def __setitem__(self, index, val):
179179
start, stop, step = index.indices(self._n)
180180
length = stop - start
181181
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
183185
if len(val) != length:
184186
raise ValueError("Slice and input sequence size do not match.")
185187
for val_i, in_i in enumerate(range(start, stop, step)):
@@ -223,7 +225,7 @@ def fill(self, color):
223225
"""Colors all pixels the given ***color***."""
224226
auto_write = self.auto_write
225227
self.auto_write = False
226-
for i, _ in enumerate(self):
228+
for i in range(self._n):
227229
self[i] = color
228230
if auto_write:
229231
self.show()

0 commit comments

Comments
 (0)