Skip to content

added auto_write #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ Usage Example
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
#matrix = matrix.Matrix16x8(i2c, address=0x70)

# Clear the matrix. Always call show after changing pixels to make the display
# update visible!
# Clear the matrix.
matrix.fill(0)
matrix.show()

# Set a pixel in the origin 0,0 position.
matrix.pixel[0, 0] = 1
Expand All @@ -77,7 +75,6 @@ Usage Example
# Set the blink rate
matrix.blink_rate = 2

matrix.show()

Contributing
============
Expand Down
28 changes: 26 additions & 2 deletions adafruit_ht16k33/ht16k33.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@


class HT16K33:
"""The base class for all displays. Contains common methods."""
def __init__(self, i2c, address=0x70):
"""
The base class for all displays. Contains common methods.

:param int address: The I2C addess of the HT16K33.
:param bool auto_write: True if the display should immediately change when
set. If False, `show` must be called explicitly.
"""
def __init__(self, i2c, address=0x70, auto_write=True):
self.i2c_device = i2c_device.I2CDevice(i2c, address)
self._temp = bytearray(1)
self._buffer = bytearray(17)
self._auto_write = None
self._auto_write = auto_write
self.fill(0)
self._write_cmd(_HT16K33_OSCILATOR_ON)
self._blink_rate = None
Expand Down Expand Up @@ -85,6 +93,18 @@ def brightness(self, brightness):
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
return None

@property
def auto_write(self):
"""Auto write updates to the display."""
return self._auto_write

@auto_write.setter
def auto_write(self, auto_write):
if isinstance(auto_write, bool):
self._auto_write = auto_write
else:
raise ValueError('Must set to either True or False.')

def show(self):
"""Refresh the display and show the changes."""
with self.i2c_device:
Expand All @@ -97,6 +117,8 @@ def fill(self, color):
fill = 0xff if color else 0x00
for i in range(16):
self._buffer[i+1] = fill
if self._auto_write:
self.show()

def _pixel(self, x, y, color=None):
mask = 1 << x
Expand All @@ -108,6 +130,8 @@ def _pixel(self, x, y, color=None):
else:
self._buffer[(y * 2) + 1] &= ~(mask & 0xff)
self._buffer[(y * 2) + 2] &= ~(mask >> 8)
if self._auto_write:
self.show()
return None

def _set_buffer(self, i, value):
Expand Down
2 changes: 2 additions & 0 deletions adafruit_ht16k33/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ def fill(self, color):
for i in range(8):
self._set_buffer(i * 2, fill1)
self._set_buffer(i * 2 + 1, fill2)
if self._auto_write:
self.show()
4 changes: 4 additions & 0 deletions adafruit_ht16k33/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def print(self, value):
self._number(value)
else:
raise ValueError('Unsupported display value type: {}'.format(type(value)))
if self._auto_write:
self.show()

def __setitem__(self, key, value):
self._put(value, key)
Expand Down Expand Up @@ -217,6 +219,8 @@ def print(self, value):
self._number(value)
else:
raise ValueError('Unsupported display value type: {}'.format(type(value)))
if self._auto_write:
self.show()

def scroll(self, count=1):
"""Scroll the display by specified number of places."""
Expand Down
11 changes: 4 additions & 7 deletions examples/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
#matrix = matrix.Matrix16x8(i2c, address=0x70)

# Clear the matrix. Always call show after changing pixels to make the display
# update visible!
# Clear the matrix.
matrix.fill(0)
matrix.show()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leave this in, but commented out, denoting that it is required if auto_write == False?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if I agree with this. I can see where @sommersoft is coming from, but I think it can start bloating the simple examples to include all the potentially necessary code if you changed the default kwargs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had something in there, but then took it out for same reason @kattni mentions. I think this is what I had, how about this:

matrix.fill(0)
# If you turn auto_write off, you will need to call show() to update the display
# matrix.show()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can agree with the bloat concern. Having the parameters in the API documentation is enough for me.


# Set a pixel in the origin 0,0 position.
matrix.pixel(0, 0, 1)
matrix[0, 0] = 1
# Set a pixel in the middle 8, 4 position.
matrix.pixel(8, 4, 1)
matrix[8, 4] = 1
# Set a pixel in the opposite 15, 7 position.
matrix.pixel(15, 7, 1)
matrix.show()
matrix[15, 7] = 1
13 changes: 8 additions & 5 deletions examples/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Author: Tony DiCola
# License: Public Domain

import time

# Import all board pins.
from board import *
import busio
Expand All @@ -22,11 +24,14 @@
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
#display = segments.Seg7x4(i2c, address=0x70)

# Clear the display. Always call show after changing the display to make the
# update visible!
# Clear the display.
display.fill(0)
display.show()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above


# Can just print a number
display.print(42)
time.sleep(2)

# Or, can set indivdual digits / characters
# Set the first character to '1':
display[0] = '1'
# Set the second character to '2':
Expand All @@ -35,5 +40,3 @@
display[2] = 'A'
# Set the forth character to 'B':
display[3] = 'B'
# Make sure to call show to see the changes above on the display!
display.show()