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 9aab0e2..aaa77d0 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -39,11 +39,19 @@ class HT16K33: - """The base class for all displays. Contains common methods.""" - def __init__(self, i2c, address=0x70): + """ + 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) self._buffer = bytearray(17) + self._auto_write = None + self._auto_write = auto_write self.fill(0) self._write_cmd(_HT16K33_OSCILATOR_ON) self._blink_rate = None @@ -85,6 +93,18 @@ def brightness(self, brightness): self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness) return None + @property + def auto_write(self): + """Auto write updates to the display.""" + return self._auto_write + + @auto_write.setter + def auto_write(self, 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.""" with self.i2c_device: @@ -97,6 +117,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 +130,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..43e2229 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,6 +219,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 scroll(self, count=1): """Scroll the display by specified number of places.""" 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()