Skip to content

change to property style access #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
locally verify it will pass.
26 changes: 18 additions & 8 deletions adafruit_ht16k33/ht16k33.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions adafruit_ht16k33/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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."""
Expand All @@ -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
Expand Down