Skip to content

Commit f2c2619

Browse files
authored
Merge pull request #11 from caternuson/iss3
change to property style access
2 parents 83e7819 + b240004 commit f2c2619

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

README.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ Usage Example
6464
matrix.show()
6565
6666
# Set a pixel in the origin 0,0 position.
67-
matrix.pixel(0, 0, 1)
67+
matrix.pixel[0, 0] = 1
6868
# Set a pixel in the middle 8, 4 position.
69-
matrix.pixel(8, 4, 1)
69+
matrix.pixel[8, 4] = 1
7070
# Set a pixel in the opposite 15, 7 position.
71-
matrix.pixel(15, 7, 1)
71+
matrix.pixel[15, 7] = 1
72+
matrix.show()
73+
74+
# Change the brightness
75+
matrix.brightness = 8
76+
77+
# Set the blink rate
78+
matrix.blink_rate = 2
79+
7280
matrix.show()
7381
7482
Contributing
@@ -123,4 +131,4 @@ Now, once you have the virtual environment activated:
123131
124132
This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
125133
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
126-
locally verify it will pass.
134+
locally verify it will pass.

adafruit_ht16k33/ht16k33.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,38 @@ def __init__(self, i2c, address=0x70):
4848
self._write_cmd(_HT16K33_OSCILATOR_ON)
4949
self._blink_rate = None
5050
self._brightness = None
51-
self.blink_rate(0)
52-
self.brightness(15)
51+
self.blink_rate = 0
52+
self.brightness = 15
5353

5454
def _write_cmd(self, byte):
5555
self._temp[0] = byte
5656
with self.i2c_device:
5757
self.i2c_device.write(self._temp)
5858

59-
def blink_rate(self, rate=None):
59+
@property
60+
def blink_rate(self):
6061
"""The blink rate. Range 0-3."""
61-
if rate is None:
62-
return self._blink_rate
62+
return self._blink_rate
63+
64+
@blink_rate.setter
65+
def blink_rate(self, rate=None):
66+
if not 0 <= rate <= 3:
67+
raise ValueError('Blink rate must be an integer in the range: 0-3')
6368
rate = rate & 0x03
6469
self._blink_rate = rate
6570
self._write_cmd(_HT16K33_BLINK_CMD |
6671
_HT16K33_BLINK_DISPLAYON | rate << 1)
6772
return None
6873

69-
def brightness(self, brightness):
74+
@property
75+
def brightness(self):
7076
"""The brightness. Range 0-15."""
71-
if brightness is None:
72-
return self._brightness
77+
return self._brightness
78+
79+
@brightness.setter
80+
def brightness(self, brightness):
81+
if not 0 <= brightness <= 15:
82+
raise ValueError('Brightness must be an integer in the range: 0-15')
7383
brightness = brightness & 0x0F
7484
self._brightness = brightness
7585
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)

adafruit_ht16k33/matrix.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def pixel(self, x, y, color=None):
4242
y += 8
4343
return super()._pixel(y, x, color)
4444

45+
def __getitem__(self, key):
46+
x, y = key
47+
return self.pixel(x, y)
48+
49+
def __setitem__(self, key, value):
50+
x, y = key
51+
self.pixel(x, y, value)
4552

4653
class Matrix8x8(HT16K33):
4754
"""A single matrix."""
@@ -54,6 +61,13 @@ def pixel(self, x, y, color=None):
5461
x = (x - 1) % 8
5562
return super()._pixel(x, y, color)
5663

64+
def __getitem__(self, key):
65+
x, y = key
66+
return self.pixel(x, y)
67+
68+
def __setitem__(self, key, value):
69+
x, y = key
70+
self.pixel(x, y, value)
5771

5872
class Matrix8x8x2(HT16K33):
5973
"""A bi-color matrix."""
@@ -70,6 +84,14 @@ def pixel(self, x, y, color=None):
7084
return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1
7185
return None
7286

87+
def __getitem__(self, key):
88+
x, y = key
89+
return self.pixel(x, y)
90+
91+
def __setitem__(self, key, value):
92+
x, y = key
93+
self.pixel(x, y, value)
94+
7395
def fill(self, color):
7496
"""Fill the whole display with the given color."""
7597
fill1 = 0xff if color & 0x01 else 0x00

0 commit comments

Comments
 (0)