39
39
40
40
41
41
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 ):
44
50
self .i2c_device = i2c_device .I2CDevice (i2c , address )
45
51
self ._temp = bytearray (1 )
46
52
self ._buffer = bytearray (17 )
53
+ self ._auto_write = None
54
+ self ._auto_write = auto_write
47
55
self .fill (0 )
48
56
self ._write_cmd (_HT16K33_OSCILATOR_ON )
49
57
self ._blink_rate = None
@@ -85,6 +93,18 @@ def brightness(self, brightness):
85
93
self ._write_cmd (_HT16K33_CMD_BRIGHTNESS | brightness )
86
94
return None
87
95
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
+
88
108
def show (self ):
89
109
"""Refresh the display and show the changes."""
90
110
with self .i2c_device :
@@ -97,6 +117,8 @@ def fill(self, color):
97
117
fill = 0xff if color else 0x00
98
118
for i in range (16 ):
99
119
self ._buffer [i + 1 ] = fill
120
+ if self ._auto_write :
121
+ self .show ()
100
122
101
123
def _pixel (self , x , y , color = None ):
102
124
mask = 1 << x
@@ -108,6 +130,8 @@ def _pixel(self, x, y, color=None):
108
130
else :
109
131
self ._buffer [(y * 2 ) + 1 ] &= ~ (mask & 0xff )
110
132
self ._buffer [(y * 2 ) + 2 ] &= ~ (mask >> 8 )
133
+ if self ._auto_write :
134
+ self .show ()
111
135
return None
112
136
113
137
def _set_buffer (self , i , value ):
0 commit comments