Skip to content

Commit

Permalink
Merge pull request #96 from tekktrik/feature/custom-characters
Browse files Browse the repository at this point in the history
Add ability to use custom characters for 7-segment display
  • Loading branch information
FoamyGuy authored Feb 5, 2022
2 parents f72ce44 + e0c43fb commit f961ffc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
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])
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)

0 comments on commit f961ffc

Please sign in to comment.