Skip to content

Commit d7241c1

Browse files
author
caternuson
committed
added auto_write
1 parent 5c808b0 commit d7241c1

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

adafruit_ht16k33/ht16k33.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040

4141
class HT16K33:
4242
"""The base class for all displays. Contains common methods."""
43-
def __init__(self, i2c, address=0x70):
43+
def __init__(self, i2c, address=0x70, auto_write=True):
4444
self.i2c_device = i2c_device.I2CDevice(i2c, address)
4545
self._temp = bytearray(1)
4646
self._buffer = bytearray(17)
47+
self._auto_write = auto_write
4748
self.fill(0)
4849
self._write_cmd(_HT16K33_OSCILATOR_ON)
4950
self._blink_rate = None
@@ -85,6 +86,14 @@ def brightness(self, brightness):
8586
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
8687
return None
8788

89+
@property
90+
def auto_write(self):
91+
return self._auto_write
92+
93+
@auto_write.setter
94+
def auto_write(self, auto_write):
95+
self._auto_write = auto_write
96+
8897
def show(self):
8998
"""Refresh the display and show the changes."""
9099
with self.i2c_device:
@@ -97,6 +106,8 @@ def fill(self, color):
97106
fill = 0xff if color else 0x00
98107
for i in range(16):
99108
self._buffer[i+1] = fill
109+
if self._auto_write:
110+
self.show()
100111

101112
def _pixel(self, x, y, color=None):
102113
mask = 1 << x
@@ -108,6 +119,8 @@ def _pixel(self, x, y, color=None):
108119
else:
109120
self._buffer[(y * 2) + 1] &= ~(mask & 0xff)
110121
self._buffer[(y * 2) + 2] &= ~(mask >> 8)
122+
if self._auto_write:
123+
self.show()
111124
return None
112125

113126
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

+5-1
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,7 +219,9 @@ def print(self, value):
217219
self._number(value)
218220
else:
219221
raise ValueError('Unsupported display value type: {}'.format(type(value)))
220-
222+
if self._auto_write:
223+
self.show()
224+
221225
def scroll(self, count=1):
222226
"""Scroll the display by specified number of places."""
223227
if count >= 0:

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)