Skip to content

Commit f961ffc

Browse files
authored
Merge pull request #96 from tekktrik/feature/custom-characters
Add ability to use custom characters for 7-segment display
2 parents f72ce44 + e0c43fb commit f961ffc

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

adafruit_ht16k33/segments.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from adafruit_ht16k33.ht16k33 import HT16K33
1313

1414
try:
15-
from typing import Union, List, Tuple
15+
from typing import Union, List, Tuple, Optional, Dict
1616
from busio import I2C
1717
except ImportError:
1818
pass
@@ -335,10 +335,17 @@ class Seg7x4(Seg14x4):
335335

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

338-
def __init__(self, i2c: I2C, address: int = 0x70, auto_write: bool = True) -> None:
338+
def __init__(
339+
self,
340+
i2c: I2C,
341+
address: int = 0x70,
342+
auto_write: bool = True,
343+
char_dict: Optional[Dict[str, int]] = None,
344+
) -> None:
339345
super().__init__(i2c, address, auto_write)
340346
# Use colon for controling two-dots indicator at the center (index 0)
341347
self._colon = Colon(self)
348+
self._chardict = char_dict
342349

343350
def scroll(self, count: int = 1) -> None:
344351
"""Scroll the display by specified number of places.
@@ -370,8 +377,11 @@ def _put(self, char: str, index: int = 0) -> None:
370377
# pylint: disable=too-many-return-statements
371378
if not 0 <= index <= 3:
372379
return
373-
char = char.lower()
374380
index = self.POSITIONS[index]
381+
if self._chardict and char in self._chardict:
382+
self._set_buffer(index, self._chardict[char])
383+
return
384+
char = char.lower()
375385
if char == ".":
376386
self._set_buffer(index, self._get_buffer(index) | 0b10000000)
377387
return
@@ -438,11 +448,18 @@ class BigSeg7x4(Seg7x4):
438448
`show` must be called explicitly.
439449
"""
440450

441-
def __init__(self, i2c: I2C, address: int = 0x70, auto_write: bool = True) -> None:
451+
def __init__(
452+
self,
453+
i2c: I2C,
454+
address: int = 0x70,
455+
auto_write: bool = True,
456+
char_dict: Optional[Dict[str, int]] = None,
457+
) -> None:
442458
super().__init__(i2c, address, auto_write)
443459
# Use colon for controling two-dots indicator at the center (index 0)
444460
# or the two-dots indicators at the left (index 1)
445461
self.colon = Colon(self, 2)
462+
self._chardict = char_dict
446463

447464
def _setindicator(self, index: int, value: bool) -> None:
448465
"""Set side LEDs (dots)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Basic example of setting digits on a LED segment display.
5+
# This example and library is meant to work with Adafruit CircuitPython API.
6+
# Author: Alec Delaney
7+
# License: Public Domain
8+
9+
# Import all board pins.
10+
import time
11+
import board
12+
import busio
13+
from adafruit_ht16k33 import segments
14+
15+
# Create the character dictionary
16+
# You can use the list normally referenced as a starting point
17+
custom_chars = {}
18+
typical_list_values = segments.NUMBERS
19+
typical_list_chars = list("0123456789abcdef-")
20+
for char, value in zip(typical_list_chars, typical_list_values):
21+
custom_chars[char] = value
22+
23+
# Add the custom characters you want
24+
custom_chars["s"] = 0b01101101
25+
custom_chars["r"] = 0b01010000
26+
custom_chars["o"] = 0b00111111
27+
custom_chars["l"] = 0b00110000
28+
custom_chars["i"] = 0b00010000
29+
custom_chars["n"] = 0b01010100
30+
custom_chars["g"] = 0b01101111
31+
32+
# Create the I2C interface.
33+
i2c = busio.I2C(board.SCL, board.SDA)
34+
display = segments.Seg7x4(i2c, char_dict=custom_chars)
35+
36+
# Clear the display.
37+
display.fill(0)
38+
39+
# Now you can print custom text
40+
display.print("cool")
41+
time.sleep(3)
42+
43+
# You can also marquee custom text
44+
display.marquee("scrolling... ", 0.2)

0 commit comments

Comments
 (0)