Skip to content

Add ability to use custom characters for 7-segment display #96

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 11 commits into from
Feb 5, 2022
25 changes: 21 additions & 4 deletions adafruit_ht16k33/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from adafruit_ht16k33.ht16k33 import HT16K33

try:
from typing import Union, List, Tuple
from typing import Union, List, Tuple, Optional, Dict
from busio import I2C
except ImportError:
pass
Expand Down Expand Up @@ -335,10 +335,17 @@ class Seg7x4(Seg14x4):

POSITIONS = (0, 2, 6, 8) # The positions of characters.

def __init__(self, i2c: I2C, address: int = 0x70, auto_write: bool = True) -> None:
def __init__(
self,
i2c: I2C,
address: int = 0x70,
auto_write: bool = True,
char_dict: Optional[Dict[str, int]] = None,
) -> None:
super().__init__(i2c, address, auto_write)
# Use colon for controling two-dots indicator at the center (index 0)
self._colon = Colon(self)
self._chardict = char_dict

def scroll(self, count: int = 1) -> None:
"""Scroll the display by specified number of places.
Expand Down Expand Up @@ -370,8 +377,11 @@ def _put(self, char: str, index: int = 0) -> None:
# pylint: disable=too-many-return-statements
if not 0 <= index <= 3:
return
char = char.lower()
index = self.POSITIONS[index]
if self._chardict and char in self._chardict:
self._set_buffer(index, self._chardict[char])
Copy link
Contributor

Choose a reason for hiding this comment

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

I dug for a bit and found that it is making it to here and calling _set_buffer() with values that seem okay to me, but nothing seems to happen on the segment display. Possibly it's an auto_write issue?

I'm putting it down for now but will circle back eventually to look more if you don't end up getting a solution once you've got your hardware for testing.

return
char = char.lower()
if char == ".":
self._set_buffer(index, self._get_buffer(index) | 0b10000000)
return
Expand Down Expand Up @@ -438,11 +448,18 @@ class BigSeg7x4(Seg7x4):
`show` must be called explicitly.
"""

def __init__(self, i2c: I2C, address: int = 0x70, auto_write: bool = True) -> None:
def __init__(
self,
i2c: I2C,
address: int = 0x70,
auto_write: bool = True,
char_dict: Optional[Dict[str, int]] = None,
) -> None:
super().__init__(i2c, address, auto_write)
# Use colon for controling two-dots indicator at the center (index 0)
# or the two-dots indicators at the left (index 1)
self.colon = Colon(self, 2)
self._chardict = char_dict

def _setindicator(self, index: int, value: bool) -> None:
"""Set side LEDs (dots)
Expand Down
44 changes: 44 additions & 0 deletions examples/ht16k33_segments_7x4customchars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Basic example of setting digits on a LED segment display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Alec Delaney
# License: Public Domain

# Import all board pins.
import time
import board
import busio
from adafruit_ht16k33 import segments

# Create the character dictionary
# You can use the list normally referenced as a starting point
custom_chars = {}
typical_list_values = segments.NUMBERS
typical_list_chars = list("0123456789abcdef-")
for char, value in zip(typical_list_chars, typical_list_values):
custom_chars[char] = value

# Add the custom characters you want
custom_chars["s"] = 0b01101101
custom_chars["r"] = 0b01010000
custom_chars["o"] = 0b00111111
custom_chars["l"] = 0b00110000
custom_chars["i"] = 0b00010000
custom_chars["n"] = 0b01010100
custom_chars["g"] = 0b01101111

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
display = segments.Seg7x4(i2c, char_dict=custom_chars)

# Clear the display.
display.fill(0)

# Now you can print custom text
display.print("cool")
time.sleep(3)

# You can also marquee custom text
display.marquee("scrolling... ", 0.2)