Skip to content

Commit 4e45148

Browse files
committed
Merge branch 'constructor_brighness_parameter' of https://github.com/geekguy-wy/Adafruit_CircuitPython_HT16K33
2 parents 24fb972 + 98f47e1 commit 4e45148

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

adafruit_ht16k33/ht16k33.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class HT16K33:
4747
:param int address: The I2C addess of the HT16K33.
4848
:param bool auto_write: True if the display should immediately change when
4949
set. If False, `show` must be called explicitly.
50+
:param float brightness: 0.0 - 1.0 default brightness level.
5051
"""
51-
def __init__(self, i2c, address=0x70, auto_write=True):
52+
def __init__(self, i2c, address=0x70, auto_write=True, brightness=1.0):
5253
self.i2c_device = i2c_device.I2CDevice(i2c, address)
5354
self._temp = bytearray(1)
5455
self._buffer = bytearray(17)
@@ -58,7 +59,7 @@ def __init__(self, i2c, address=0x70, auto_write=True):
5859
self._blink_rate = None
5960
self._brightness = None
6061
self.blink_rate = 0
61-
self.brightness = 15
62+
self.brightness = brightness
6263

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

8283
@property
8384
def brightness(self):
84-
"""The brightness. Range 0-15."""
85+
"""The brightness. Range 0.0-1.0"""
8586
return self._brightness
8687

8788
@brightness.setter
8889
def brightness(self, brightness):
89-
if not 0 <= brightness <= 15:
90-
raise ValueError('Brightness must be an integer in the range: 0-15')
91-
brightness = brightness & 0x0F
90+
if not 0.0 <= brightness <= 1.0:
91+
raise ValueError('Brightness must be a decimal number in the range: 0.0-1.0')
92+
9293
self._brightness = brightness
93-
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
94+
xbright = round(15 * brightness)
95+
xbright = xbright & 0x0F
96+
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | xbright)
9497

9598
@property
9699
def auto_write(self):

examples/ht16k33_animation_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
DEFAULT_CYCLES = 5
3939

4040
# Brightness of the display (0 to 15)
41-
DEFAULT_DISPLAY_BRIGHTNESS = 2
41+
DEFAULT_DISPLAY_BRIGHTNESS = 0.3
4242

4343
# Initialize the I2C bus
4444
i2c = busio.I2C(board.SCL, board.SDA)

0 commit comments

Comments
 (0)