From d7241c184a60ac1c2ec683914804c9dcc46cd676 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 22 Jun 2018 16:30:49 -0700 Subject: [PATCH 1/4] added auto_write --- adafruit_ht16k33/ht16k33.py | 15 ++++++++++++++- adafruit_ht16k33/matrix.py | 2 ++ adafruit_ht16k33/segments.py | 6 +++++- examples/matrix.py | 11 ++++------- examples/segments.py | 13 ++++++++----- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 9aab0e2..28fa0fc 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -40,10 +40,11 @@ class HT16K33: """The base class for all displays. Contains common methods.""" - def __init__(self, i2c, address=0x70): + def __init__(self, i2c, address=0x70, auto_write=True): self.i2c_device = i2c_device.I2CDevice(i2c, address) self._temp = bytearray(1) self._buffer = bytearray(17) + self._auto_write = auto_write self.fill(0) self._write_cmd(_HT16K33_OSCILATOR_ON) self._blink_rate = None @@ -85,6 +86,14 @@ def brightness(self, brightness): self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness) return None + @property + def auto_write(self): + return self._auto_write + + @auto_write.setter + def auto_write(self, auto_write): + self._auto_write = auto_write + def show(self): """Refresh the display and show the changes.""" with self.i2c_device: @@ -97,6 +106,8 @@ def fill(self, color): fill = 0xff if color else 0x00 for i in range(16): self._buffer[i+1] = fill + if self._auto_write: + self.show() def _pixel(self, x, y, color=None): mask = 1 << x @@ -108,6 +119,8 @@ def _pixel(self, x, y, color=None): else: self._buffer[(y * 2) + 1] &= ~(mask & 0xff) self._buffer[(y * 2) + 2] &= ~(mask >> 8) + if self._auto_write: + self.show() return None def _set_buffer(self, i, value): diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index 41722c6..000c0e7 100644 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -99,3 +99,5 @@ def fill(self, color): for i in range(8): self._set_buffer(i * 2, fill1) self._set_buffer(i * 2 + 1, fill2) + if self._auto_write: + self.show() diff --git a/adafruit_ht16k33/segments.py b/adafruit_ht16k33/segments.py index 06610f0..51e4c29 100644 --- a/adafruit_ht16k33/segments.py +++ b/adafruit_ht16k33/segments.py @@ -156,6 +156,8 @@ def print(self, value): self._number(value) else: raise ValueError('Unsupported display value type: {}'.format(type(value))) + if self._auto_write: + self.show() def __setitem__(self, key, value): self._put(value, key) @@ -217,7 +219,9 @@ def print(self, value): self._number(value) else: raise ValueError('Unsupported display value type: {}'.format(type(value))) - + if self._auto_write: + self.show() + def scroll(self, count=1): """Scroll the display by specified number of places.""" if count >= 0: diff --git a/examples/matrix.py b/examples/matrix.py index 297542e..958cda9 100644 --- a/examples/matrix.py +++ b/examples/matrix.py @@ -24,15 +24,12 @@ # Finally you can optionally specify a custom I2C address of the HT16k33 like: #matrix = matrix.Matrix16x8(i2c, address=0x70) -# Clear the matrix. Always call show after changing pixels to make the display -# update visible! +# Clear the matrix. matrix.fill(0) -matrix.show() # Set a pixel in the origin 0,0 position. -matrix.pixel(0, 0, 1) +matrix[0, 0] = 1 # Set a pixel in the middle 8, 4 position. -matrix.pixel(8, 4, 1) +matrix[8, 4] = 1 # Set a pixel in the opposite 15, 7 position. -matrix.pixel(15, 7, 1) -matrix.show() +matrix[15, 7] = 1 diff --git a/examples/segments.py b/examples/segments.py index f9e5abf..4deac99 100644 --- a/examples/segments.py +++ b/examples/segments.py @@ -3,6 +3,8 @@ # Author: Tony DiCola # License: Public Domain +import time + # Import all board pins. from board import * import busio @@ -22,11 +24,14 @@ # Finally you can optionally specify a custom I2C address of the HT16k33 like: #display = segments.Seg7x4(i2c, address=0x70) -# Clear the display. Always call show after changing the display to make the -# update visible! +# Clear the display. display.fill(0) -display.show() +# Can just print a number +display.print(42) +time.sleep(2) + +# Or, can set indivdual digits / characters # Set the first character to '1': display[0] = '1' # Set the second character to '2': @@ -35,5 +40,3 @@ display[2] = 'A' # Set the forth character to 'B': display[3] = 'B' -# Make sure to call show to see the changes above on the display! -display.show() From e92c95f4c589bff4262b2846cc14730878982c35 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 22 Jun 2018 16:38:55 -0700 Subject: [PATCH 2/4] linty linterson --- adafruit_ht16k33/ht16k33.py | 1 + adafruit_ht16k33/segments.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 28fa0fc..21095c5 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -88,6 +88,7 @@ def brightness(self, brightness): @property def auto_write(self): + """Auto write updates to the display.""" return self._auto_write @auto_write.setter diff --git a/adafruit_ht16k33/segments.py b/adafruit_ht16k33/segments.py index 51e4c29..43e2229 100644 --- a/adafruit_ht16k33/segments.py +++ b/adafruit_ht16k33/segments.py @@ -221,7 +221,7 @@ def print(self, value): raise ValueError('Unsupported display value type: {}'.format(type(value))) if self._auto_write: self.show() - + def scroll(self, count=1): """Scroll the display by specified number of places.""" if count >= 0: From 1d6f7954dba50901aeb3c0af1a6e8d48efc1aa30 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 22 Jun 2018 16:50:50 -0700 Subject: [PATCH 3/4] tweak the init, add check --- adafruit_ht16k33/ht16k33.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 21095c5..2b2eb9e 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -44,6 +44,7 @@ def __init__(self, i2c, address=0x70, auto_write=True): self.i2c_device = i2c_device.I2CDevice(i2c, address) self._temp = bytearray(1) self._buffer = bytearray(17) + self._auto_write = None self._auto_write = auto_write self.fill(0) self._write_cmd(_HT16K33_OSCILATOR_ON) @@ -93,7 +94,10 @@ def auto_write(self): @auto_write.setter def auto_write(self, auto_write): - self._auto_write = auto_write + if isinstance(auto_write, bool): + self._auto_write = auto_write + else: + raise ValueError('Must set to either True or False.') def show(self): """Refresh the display and show the changes.""" From 7b0f6ff4a26fe999f0f8b735500b3f75afbb3dbc Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 25 Jun 2018 16:38:22 -0700 Subject: [PATCH 4/4] README and docstring update --- README.rst | 5 +---- adafruit_ht16k33/ht16k33.py | 8 +++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 81f6d07..c8c67be 100644 --- a/README.rst +++ b/README.rst @@ -58,10 +58,8 @@ Usage Example # Finally you can optionally specify a custom I2C address of the HT16k33 like: #matrix = matrix.Matrix16x8(i2c, address=0x70) - # Clear the matrix. Always call show after changing pixels to make the display - # update visible! + # Clear the matrix. matrix.fill(0) - matrix.show() # Set a pixel in the origin 0,0 position. matrix.pixel[0, 0] = 1 @@ -77,7 +75,6 @@ Usage Example # Set the blink rate matrix.blink_rate = 2 - matrix.show() Contributing ============ diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 2b2eb9e..aaa77d0 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -39,7 +39,13 @@ class HT16K33: - """The base class for all displays. Contains common methods.""" + """ + The base class for all displays. Contains common methods. + + :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. + """ def __init__(self, i2c, address=0x70, auto_write=True): self.i2c_device = i2c_device.I2CDevice(i2c, address) self._temp = bytearray(1)