Skip to content

Commit

Permalink
Merge pull request #70 from geekguy-wy/constructor_brighness_parameter
Browse files Browse the repository at this point in the history
Constructor brightness parameter
  • Loading branch information
makermelissa authored Mar 10, 2020
2 parents 24fb972 + 98f47e1 commit f38cd30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
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

0 comments on commit f38cd30

Please sign in to comment.