Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion adafruit_dotstar.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git"

START_HEADER_SIZE = 4
GAMMA_CORRECT_FACTOR = 2.8
LED_START = 0b11100000 # Three "1" bits, followed by 5 brightness bits

# Pixel color order constants
Expand Down Expand Up @@ -78,7 +79,15 @@ class DotStar:
time.sleep(2)
"""

def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True, pixel_order=BGR):
def __init__(self,
clock,
data,
n,
*,
brightness=1.0,
auto_write=True,
pixel_order=BGR,
gamma_correct=False):
self._spi = None
try:
self._spi = busio.SPI(clock, MOSI=data)
Expand All @@ -91,6 +100,7 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True, pixel_ord
self.dpin.direction = digitalio.Direction.OUTPUT
self.cpin.direction = digitalio.Direction.OUTPUT
self.cpin.value = False
self.gamma_correct = gamma_correct
self._n = n
# Supply one extra clock cycle for each two pixels in the strip.
self.end_header_size = n // 16
Expand Down Expand Up @@ -165,6 +175,8 @@ def _set_item(self, index, value):
else:
brightness = 1

if self.gamma_correct:
rgb = map(self.gamma_correct, rgb)
# LED startframe is three "1" bits, followed by 5 brightness bits
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and
# vary based on hardware
Expand Down Expand Up @@ -208,6 +220,11 @@ def __getitem__(self, index):
def __len__(self):
return self._n

def gamma_correct(self, led_val):
max_val = (1 << 8) - 1.0
corrected = pow(led_val / max_val, GAMMA_CORRECT_FACTOR) * max_val
return int(min(255, max(0, corrected)))

@property
def brightness(self):
"""Overall brightness of the pixel"""
Expand Down