|
37 | 37 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git"
|
38 | 38 |
|
39 | 39 | START_HEADER_SIZE = 4
|
| 40 | +LED_START = 0b11100000 # Three "1" bits, followed by 5 brightness bits |
40 | 41 |
|
41 | 42 | # Pixel color order constants
|
42 | 43 | RGB = (0, 1, 2)
|
@@ -171,14 +172,14 @@ def _set_item(self, index, value):
|
171 | 172 | else:
|
172 | 173 | brightness = 100
|
173 | 174 |
|
174 |
| - brightness_byte = ceil(brightness * 31) & 0b00011111 |
| 175 | + brightness_byte = math.ceil(brightness * 31) & 0b00011111 |
175 | 176 | # LED startframe is three "1" bits, followed by 5 brightness bits
|
176 | 177 | # then 8 bits for each of R, G, and B. The order of those 3 are configurable and
|
177 | 178 | # vary based on hardware
|
178 | 179 | self._buf[offset] = brightness_byte | LED_START
|
179 |
| - self._buf[offset + 1] = int(rgb[self.pixel_order[0]] * self.brightness) |
180 |
| - self._buf[offset + 2] = int(rgb[self.pixel_order[1]] * self.brightness) |
181 |
| - self._buf[offset + 3] = (rgb[self.pixel_order[2]] * self.brightness) |
| 180 | + self._buf[offset + 1] = int(rgb[self.pixel_order[0]]) |
| 181 | + self._buf[offset + 2] = int(rgb[self.pixel_order[1]]) |
| 182 | + self._buf[offset + 3] = (rgb[self.pixel_order[2]]) |
182 | 183 |
|
183 | 184 | def __setitem__(self, index, val):
|
184 | 185 | if isinstance(index, slice):
|
@@ -249,7 +250,23 @@ def show(self):
|
249 | 250 |
|
250 | 251 | The colors may or may not be showing after this function returns because
|
251 | 252 | it may be done asynchronously."""
|
| 253 | + # Create a second output buffer if we need to compute brightness |
| 254 | + buf = self._buf |
| 255 | + if self.brightness < 1.0: |
| 256 | + buf = bytearray(self._buf) |
| 257 | + # Four empty bytes to start. |
| 258 | + for i in range(START_HEADER_SIZE): |
| 259 | + buf[i] = 0x00 |
| 260 | + for i in range(START_HEADER_SIZE, self.end_header_index): |
| 261 | + buf[i] = self._buf[i] if i % 4 == 0 else math.ceil(self._buf[i] * self._brightness) |
| 262 | + # Four 0xff bytes at the end. |
| 263 | + for i in range(self.end_header_index, len(buf)): |
| 264 | + buf[i] = 0xff |
252 | 265 |
|
| 266 | + if self._spi: |
| 267 | + self._spi.write(buf) |
| 268 | + else: |
| 269 | + self._ds_writebytes(buf) |
253 | 270 | if self._spi:
|
254 | 271 | self._spi.write(self._buf)
|
255 | 272 | else:
|
|
0 commit comments