From 4c3001579f5c8f8c7c55673d7f973b0d70432608 Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 13 Jun 2018 16:26:34 -0700 Subject: [PATCH 1/3] change to property style access --- adafruit_ht16k33/ht16k33.py | 22 ++++++++++++++++------ adafruit_ht16k33/matrix.py | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index d02b689..168cd3e 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -48,28 +48,38 @@ def __init__(self, i2c, address=0x70): self._write_cmd(_HT16K33_OSCILATOR_ON) self._blink_rate = None self._brightness = None - self.blink_rate(0) - self.brightness(15) + self.blink_rate = 0 + self.brightness = 15 def _write_cmd(self, byte): self._temp[0] = byte with self.i2c_device: self.i2c_device.write(self._temp) + @property + def blink_rate(self): + return self._blink_rate + + @blink_rate.setter def blink_rate(self, rate=None): """The blink rate. Range 0-3.""" - if rate is None: - return self._blink_rate + if not 0 <= rate <= 3: + raise ValueError('Blink rate must be an integer in the range: 0-3') rate = rate & 0x03 self._blink_rate = rate self._write_cmd(_HT16K33_BLINK_CMD | _HT16K33_BLINK_DISPLAYON | rate << 1) return None + @property + def brightness(self): + return self._brightness + + @brightness.setter def brightness(self, brightness): """The brightness. Range 0-15.""" - if brightness is None: - return self._brightness + if not 0 <= brightness <= 15: + raise ValueError('Brightness must be an integer in the range: 0-15') brightness = brightness & 0x0F self._brightness = brightness self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness) diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index 9b0bd06..41722c6 100644 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -42,6 +42,13 @@ def pixel(self, x, y, color=None): y += 8 return super()._pixel(y, x, color) + def __getitem__(self, key): + x, y = key + return self.pixel(x, y) + + def __setitem__(self, key, value): + x, y = key + self.pixel(x, y, value) class Matrix8x8(HT16K33): """A single matrix.""" @@ -54,6 +61,13 @@ def pixel(self, x, y, color=None): x = (x - 1) % 8 return super()._pixel(x, y, color) + def __getitem__(self, key): + x, y = key + return self.pixel(x, y) + + def __setitem__(self, key, value): + x, y = key + self.pixel(x, y, value) class Matrix8x8x2(HT16K33): """A bi-color matrix.""" @@ -70,6 +84,14 @@ def pixel(self, x, y, color=None): return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1 return None + def __getitem__(self, key): + x, y = key + return self.pixel(x, y) + + def __setitem__(self, key, value): + x, y = key + self.pixel(x, y, value) + def fill(self, color): """Fill the whole display with the given color.""" fill1 = 0xff if color & 0x01 else 0x00 From 3989a098076d6298095cd41df6dfb3aed3906d51 Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 13 Jun 2018 16:37:24 -0700 Subject: [PATCH 2/3] happy now linter? --- adafruit_ht16k33/ht16k33.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index 168cd3e..9aab0e2 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -49,7 +49,7 @@ def __init__(self, i2c, address=0x70): self._blink_rate = None self._brightness = None self.blink_rate = 0 - self.brightness = 15 + self.brightness = 15 def _write_cmd(self, byte): self._temp[0] = byte @@ -58,11 +58,11 @@ def _write_cmd(self, byte): @property def blink_rate(self): + """The blink rate. Range 0-3.""" return self._blink_rate @blink_rate.setter def blink_rate(self, rate=None): - """The blink rate. Range 0-3.""" if not 0 <= rate <= 3: raise ValueError('Blink rate must be an integer in the range: 0-3') rate = rate & 0x03 @@ -73,11 +73,11 @@ def blink_rate(self, rate=None): @property def brightness(self): + """The brightness. Range 0-15.""" return self._brightness @brightness.setter def brightness(self, brightness): - """The brightness. Range 0-15.""" if not 0 <= brightness <= 15: raise ValueError('Brightness must be an integer in the range: 0-15') brightness = brightness & 0x0F From b2400044901582743a7c6af52f246fed23e73c42 Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 14 Jun 2018 10:10:10 -0700 Subject: [PATCH 3/3] update example in README.rst --- README.rst | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 725ad0f..81f6d07 100644 --- a/README.rst +++ b/README.rst @@ -64,11 +64,19 @@ Usage Example matrix.show() # Set a pixel in the origin 0,0 position. - matrix.pixel(0, 0, 1) + matrix.pixel[0, 0] = 1 # Set a pixel in the middle 8, 4 position. - matrix.pixel(8, 4, 1) + matrix.pixel[8, 4] = 1 # Set a pixel in the opposite 15, 7 position. - matrix.pixel(15, 7, 1) + matrix.pixel[15, 7] = 1 + matrix.show() + + # Change the brightness + matrix.brightness = 8 + + # Set the blink rate + matrix.blink_rate = 2 + matrix.show() Contributing @@ -123,4 +131,4 @@ Now, once you have the virtual environment activated: This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to -locally verify it will pass. \ No newline at end of file +locally verify it will pass.