From 92d73acb72f8e27a930cbcd725d8b91dfaf412e3 Mon Sep 17 00:00:00 2001 From: =Dale Weber <=geekguy.wy@gmail.com> Date: Tue, 18 Feb 2020 11:15:46 -0800 Subject: [PATCH 1/3] Modified the brightness property to use floats instead of ints, and changed the animation demo to use it. --- adafruit_ht16k33/ht16k33.py | 14 ++++++++------ examples/ht16k33_animation_demo.py | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index f0c4643..5fccd8f 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -58,7 +58,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 = 1.0 def _write_cmd(self, byte): self._temp[0] = byte @@ -81,16 +81,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 = int(15 * brightness) + xbright = xbright & 0x0F + self._write_cmd(_HT16K33_CMD_BRIGHTNESS | xbright) @property def auto_write(self): diff --git a/examples/ht16k33_animation_demo.py b/examples/ht16k33_animation_demo.py index 9ef80cd..d12f1e5 100644 --- a/examples/ht16k33_animation_demo.py +++ b/examples/ht16k33_animation_demo.py @@ -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) From 0aea7b85853fcad59ba9a7c37f5819715a48ab57 Mon Sep 17 00:00:00 2001 From: =Dale Weber <=geekguy.wy@gmail.com> Date: Sat, 7 Mar 2020 12:47:56 -0800 Subject: [PATCH 2/3] Switched from using int() to using round() in the brightness routine. --- adafruit_ht16k33/ht16k33.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 5fccd8f..53515d9 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -90,7 +90,7 @@ def brightness(self, brightness): raise ValueError('Brightness must be a decimal number in the range: 0.0-1.0') self._brightness = brightness - xbright = int(15 * brightness) + xbright = round(15 * brightness) xbright = xbright & 0x0F self._write_cmd(_HT16K33_CMD_BRIGHTNESS | xbright) From 98f47e14601eebc558c8b7f5e5340e3390e1c51d Mon Sep 17 00:00:00 2001 From: =Dale Weber <=geekguy.wy@gmail.com> Date: Sat, 7 Mar 2020 18:32:03 -0800 Subject: [PATCH 3/3] Added documentation for the new constructor brightness parameter. --- adafruit_ht16k33/ht16k33.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 53515d9..dff931c 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -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) @@ -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 = 1.0 + self.brightness = brightness def _write_cmd(self, byte): self._temp[0] = byte