Skip to content

Updates for segment displays #12

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 3 commits into from
Jun 21, 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
54 changes: 32 additions & 22 deletions adafruit_ht16k33/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@

class Seg14x4(HT16K33):
"""Alpha-numeric, 14-segment display."""
def print(self, value):
"""Print the value to the display."""
if isinstance(value, (str)):
self._text(value)
elif isinstance(value, (int, float)):
self._number(value)
else:
raise ValueError('Unsupported display value type: {}'.format(type(value)))

def __setitem__(self, key, value):
self._put(value, key)

def scroll(self, count=1):
"""Scroll the display by specified number of places."""
if count >= 0:
Expand All @@ -157,7 +169,7 @@ def scroll(self, count=1):
for i in range(6):
self._set_buffer(i + offset, self._get_buffer(i + 2 * count))

def put(self, char, index=0):
def _put(self, char, index=0):
"""Put a character at the specified place."""
if not 0 <= index <= 3:
return
Expand All @@ -170,44 +182,42 @@ def put(self, char, index=0):
self._set_buffer(index * 2, CHARS[1 + character])
self._set_buffer(index * 2 + 1, CHARS[character])

def push(self, char):
def _push(self, char):
"""Scroll the display and add a character at the end."""
if char != '.' or self._get_buffer(7) & 0b01000000:
self.scroll()
self.put(' ', 3)
self.put(char, 3)
self._put(' ', 3)
self._put(char, 3)

def text(self, text):
def _text(self, text):
"""Display the specified text."""
for character in text:
self.push(character)
self._push(character)

def number(self, number):
def _number(self, number):
"""Display the specified decimal number."""
string = "{:f}".format(number)
string = "{}".format(number)
if len(string) > 4:
if string.find('.') > 4:
raise ValueError("Overflow")
self.fill(False)
places = 4
if '.' in string:
places += 1
self.text(string[:places])

def hex(self, number):
"""Display the specified hexadecimal number."""
string = "{:x}".format(number)
if len(string) > 4:
raise ValueError("Overflow")
self.fill(False)
self.text(string)

self._text(string[:places])

class Seg7x4(Seg14x4):
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
supports displaying decimal and hex digits, period and a minus sign."""
POSITIONS = [0, 2, 6, 8] # The positions of characters.

def print(self, value):
"""Print the value to the display."""
if isinstance(value, (int, float)):
self._number(value)
else:
raise ValueError('Unsupported display value type: {}'.format(type(value)))

def scroll(self, count=1):
"""Scroll the display by specified number of places."""
if count >= 0:
Expand All @@ -218,14 +228,14 @@ def scroll(self, count=1):
self._set_buffer(self.POSITIONS[i + offset],
self._get_buffer(self.POSITIONS[i + count]))

def push(self, char):
def _push(self, char):
"""Scroll the display and add a character at the end."""
if char in ':;':
self.put(char)
self._put(char)
else:
super().push(char)
super()._push(char)

def put(self, char, index=0):
def _put(self, char, index=0):
"""Put a character at the specified place."""
if not 0 <= index <= 3:
return
Expand Down
8 changes: 4 additions & 4 deletions examples/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
display.show()

# Set the first character to '1':
display.put('1', 0)
display[0] = '1'
# Set the second character to '2':
display.put('2', 1)
display[1] = '2'
# Set the third character to 'A':
display.put('A', 2)
display[2] = 'A'
# Set the forth character to 'B':
display.put('B', 3)
display[3] = 'B'
# Make sure to call show to see the changes above on the display!
display.show()