Skip to content

Implemented set_digit_raw for 7 and 14 segment displays #43

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 2 commits into from
Dec 31, 2019
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
35 changes: 34 additions & 1 deletion adafruit_ht16k33/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ def _number(self, number):
self._text(string[:places])
self._auto_write = auto_write

def set_digit_raw(self, index, bitmask):
"""Set digit at position to raw bitmask value. Position should be a value
of 0 to 3 with 0 being the left most character on the display.

bitmask should be 2 bytes such as: 0xFFFF
If can be passed as an integer, list, or tuple
"""
if not 0 <= index <= 3:
return

if isinstance(bitmask, (tuple, list)):
bitmask = bitmask[0] << 8 | bitmask[1]

# Set the digit bitmask value at the appropriate position.
self._set_buffer(index * 2, (bitmask >> 8) & 0xFF)
self._set_buffer(index * 2 + 1, bitmask & 0xFF)

if self._auto_write:
self.show()

class Seg7x4(Seg14x4):
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
supports displaying a limited set of characters."""
Expand Down Expand Up @@ -249,7 +269,7 @@ def _put(self, char, index=0):
if char == '.':
self._set_buffer(index, self._get_buffer(index) | 0b10000000)
return
elif char in 'abcdef':
if char in 'abcdef':
character = ord(char) - 97 + 10
elif char == '-':
character = 16
Expand All @@ -268,6 +288,19 @@ def _put(self, char, index=0):
return
self._set_buffer(index, NUMBERS[character])

def set_digit_raw(self, index, bitmask):
"""Set digit at position to raw bitmask value. Position should be a value
of 0 to 3 with 0 being the left most digit on the display.
"""
if not 0 <= index <= 3:
return

# Set the digit bitmask value at the appropriate position.
self._set_buffer(self.POSITIONS[index], bitmask & 0xFF)

if self._auto_write:
self.show()

class BigSeg7x4(Seg7x4):
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
supports displaying a limited set of characters."""
Expand Down
16 changes: 15 additions & 1 deletion examples/ht16k33_segments_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# Import the HT16K33 LED segment module.
from adafruit_ht16k33 import segments


# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)

Expand Down Expand Up @@ -40,3 +39,18 @@
display[2] = 'A'
# Set the forth character to 'B':
display[3] = 'B'
time.sleep(2)

# Or, can even set the segments to make up characters
if isinstance(display, segments.Seg7x4):
# 7-segment raw digits
display.set_digit_raw(0, 0xFF)
display.set_digit_raw(1, 0b11111111)
display.set_digit_raw(2, 0x79)
display.set_digit_raw(3, 0b01111001)
else:
# 14-segment raw digits
display.set_digit_raw(0, 0x3F2D)
display.set_digit_raw(1, 0b0011111100101101)
display.set_digit_raw(2, (0b00111111, 0b00101101))
display.set_digit_raw(3, [0b00111111, 0b00101101])