Skip to content

Instantly share code, notes, and snippets.

@frohoff frohoff/14seg.py
Created Dec 17, 2018

Embed
What would you like to do?
14 segment display ascii renderer
#!/usr/bin/python
# $ echo bcefgG il | python 14seg.py
#
# | | |
# -- --
# | | |
import string
import sys
CHAR_MATRIX = """
-----
|\|/|
-- --
|/|\|
-----
""".strip()
SEGS_MATRIX = """
aaaaa
fhijb
gg GG
emlkc
ddddd
""".strip()
MAP = "abcdefgGhijklm"
def mask(segs, mapping=MAP):
assert not mapping or len(mapping) == len(MAP)
tr = string.maketrans(mapping or MAP, MAP)
segs = segs.translate(tr)
masked_matrix = ''.join(CHAR_MATRIX[i] if SEGS_MATRIX[i] in segs or SEGS_MATRIX[i] == '\n' else ' ' for i in range(len(SEGS_MATRIX)))
return masked_matrix
def combine(glyphs, space=" "):
num_rows = range(len(CHAR_MATRIX.splitlines()))
combined = '\n'.join(space.join(g.splitlines()[i] for g in glyphs) for i in num_rows)
return combined
if __name__ == '__main__':
mapping = sys.argv[1] if len(sys.argv) > 1 else None
chars = sys.stdin.read().split()
glyphs = [mask(segs, mapping=mapping) for segs in chars]
combined = combine(glyphs)
print combined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.