Skip to content

Constructor brightness parameter #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
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
17 changes: 10 additions & 7 deletions adafruit_ht16k33/ht16k33.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class HT16K33:
:param int address: The I2C addess of the HT16K33.
:param bool auto_write: True if the display should immediately change when
set. If False, `show` must be called explicitly.
:param float brightness: 0.0 - 1.0 default brightness level.
"""
def __init__(self, i2c, address=0x70, auto_write=True):
def __init__(self, i2c, address=0x70, auto_write=True, brightness=1.0):
self.i2c_device = i2c_device.I2CDevice(i2c, address)
self._temp = bytearray(1)
self._buffer = bytearray(17)
Expand All @@ -58,7 +59,7 @@ def __init__(self, i2c, address=0x70, auto_write=True):
self._blink_rate = None
self._brightness = None
self.blink_rate = 0
self.brightness = 15
self.brightness = brightness

def _write_cmd(self, byte):
self._temp[0] = byte
Expand All @@ -81,16 +82,18 @@ def blink_rate(self, rate=None):

@property
def brightness(self):
"""The brightness. Range 0-15."""
"""The brightness. Range 0.0-1.0"""
return self._brightness

@brightness.setter
def brightness(self, brightness):
if not 0 <= brightness <= 15:
raise ValueError('Brightness must be an integer in the range: 0-15')
brightness = brightness & 0x0F
if not 0.0 <= brightness <= 1.0:
raise ValueError('Brightness must be a decimal number in the range: 0.0-1.0')

self._brightness = brightness
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
xbright = round(15 * brightness)
xbright = xbright & 0x0F
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | xbright)

@property
def auto_write(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/ht16k33_animation_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
DEFAULT_CYCLES = 5

# Brightness of the display (0 to 15)
DEFAULT_DISPLAY_BRIGHTNESS = 2
DEFAULT_DISPLAY_BRIGHTNESS = 0.3

# Initialize the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
Expand Down