Skip to content

Commit b545d77

Browse files
authored
Merge pull request #14 from caternuson/iss13
Thanks @caternuson!!
2 parents 5c808b0 + 7b0f6ff commit b545d77

File tree

6 files changed

+45
-18
lines changed

6 files changed

+45
-18
lines changed

README.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ Usage Example
5858
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
5959
#matrix = matrix.Matrix16x8(i2c, address=0x70)
6060
61-
# Clear the matrix. Always call show after changing pixels to make the display
62-
# update visible!
61+
# Clear the matrix.
6362
matrix.fill(0)
64-
matrix.show()
6563
6664
# Set a pixel in the origin 0,0 position.
6765
matrix.pixel[0, 0] = 1
@@ -77,7 +75,6 @@ Usage Example
7775
# Set the blink rate
7876
matrix.blink_rate = 2
7977
80-
matrix.show()
8178
8279
Contributing
8380
============

adafruit_ht16k33/ht16k33.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@
3939

4040

4141
class HT16K33:
42-
"""The base class for all displays. Contains common methods."""
43-
def __init__(self, i2c, address=0x70):
42+
"""
43+
The base class for all displays. Contains common methods.
44+
45+
:param int address: The I2C addess of the HT16K33.
46+
:param bool auto_write: True if the display should immediately change when
47+
set. If False, `show` must be called explicitly.
48+
"""
49+
def __init__(self, i2c, address=0x70, auto_write=True):
4450
self.i2c_device = i2c_device.I2CDevice(i2c, address)
4551
self._temp = bytearray(1)
4652
self._buffer = bytearray(17)
53+
self._auto_write = None
54+
self._auto_write = auto_write
4755
self.fill(0)
4856
self._write_cmd(_HT16K33_OSCILATOR_ON)
4957
self._blink_rate = None
@@ -85,6 +93,18 @@ def brightness(self, brightness):
8593
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
8694
return None
8795

96+
@property
97+
def auto_write(self):
98+
"""Auto write updates to the display."""
99+
return self._auto_write
100+
101+
@auto_write.setter
102+
def auto_write(self, auto_write):
103+
if isinstance(auto_write, bool):
104+
self._auto_write = auto_write
105+
else:
106+
raise ValueError('Must set to either True or False.')
107+
88108
def show(self):
89109
"""Refresh the display and show the changes."""
90110
with self.i2c_device:
@@ -97,6 +117,8 @@ def fill(self, color):
97117
fill = 0xff if color else 0x00
98118
for i in range(16):
99119
self._buffer[i+1] = fill
120+
if self._auto_write:
121+
self.show()
100122

101123
def _pixel(self, x, y, color=None):
102124
mask = 1 << x
@@ -108,6 +130,8 @@ def _pixel(self, x, y, color=None):
108130
else:
109131
self._buffer[(y * 2) + 1] &= ~(mask & 0xff)
110132
self._buffer[(y * 2) + 2] &= ~(mask >> 8)
133+
if self._auto_write:
134+
self.show()
111135
return None
112136

113137
def _set_buffer(self, i, value):

adafruit_ht16k33/matrix.py

+2
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ def fill(self, color):
9999
for i in range(8):
100100
self._set_buffer(i * 2, fill1)
101101
self._set_buffer(i * 2 + 1, fill2)
102+
if self._auto_write:
103+
self.show()

adafruit_ht16k33/segments.py

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def print(self, value):
156156
self._number(value)
157157
else:
158158
raise ValueError('Unsupported display value type: {}'.format(type(value)))
159+
if self._auto_write:
160+
self.show()
159161

160162
def __setitem__(self, key, value):
161163
self._put(value, key)
@@ -217,6 +219,8 @@ def print(self, value):
217219
self._number(value)
218220
else:
219221
raise ValueError('Unsupported display value type: {}'.format(type(value)))
222+
if self._auto_write:
223+
self.show()
220224

221225
def scroll(self, count=1):
222226
"""Scroll the display by specified number of places."""

examples/matrix.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@
2424
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
2525
#matrix = matrix.Matrix16x8(i2c, address=0x70)
2626

27-
# Clear the matrix. Always call show after changing pixels to make the display
28-
# update visible!
27+
# Clear the matrix.
2928
matrix.fill(0)
30-
matrix.show()
3129

3230
# Set a pixel in the origin 0,0 position.
33-
matrix.pixel(0, 0, 1)
31+
matrix[0, 0] = 1
3432
# Set a pixel in the middle 8, 4 position.
35-
matrix.pixel(8, 4, 1)
33+
matrix[8, 4] = 1
3634
# Set a pixel in the opposite 15, 7 position.
37-
matrix.pixel(15, 7, 1)
38-
matrix.show()
35+
matrix[15, 7] = 1

examples/segments.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Author: Tony DiCola
44
# License: Public Domain
55

6+
import time
7+
68
# Import all board pins.
79
from board import *
810
import busio
@@ -22,11 +24,14 @@
2224
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
2325
#display = segments.Seg7x4(i2c, address=0x70)
2426

25-
# Clear the display. Always call show after changing the display to make the
26-
# update visible!
27+
# Clear the display.
2728
display.fill(0)
28-
display.show()
2929

30+
# Can just print a number
31+
display.print(42)
32+
time.sleep(2)
33+
34+
# Or, can set indivdual digits / characters
3035
# Set the first character to '1':
3136
display[0] = '1'
3237
# Set the second character to '2':
@@ -35,5 +40,3 @@
3540
display[2] = 'A'
3641
# Set the forth character to 'B':
3742
display[3] = 'B'
38-
# Make sure to call show to see the changes above on the display!
39-
display.show()

0 commit comments

Comments
 (0)