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. diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index d02b689..9aab0e2 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) - def blink_rate(self, rate=None): + @property + def blink_rate(self): """The blink rate. Range 0-3.""" - if rate is None: - return self._blink_rate + return self._blink_rate + + @blink_rate.setter + def blink_rate(self, rate=None): + 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 - def brightness(self, brightness): + @property + def brightness(self): """The brightness. Range 0-15.""" - if brightness is None: - return self._brightness + 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 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