3939
4040
4141class 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 ):
0 commit comments