Created
September 19, 2018 14:58
-
-
Save emilio1625/f0edb713b070b68a876a4263ad2da6ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
File: str27seg.py | |
Author: emilio1625 | |
Email: [email protected] | |
Github: https://github.com/emilio1625 | |
Description: Creates a hex file with the bytes needed to display a string in a | |
7 segment display. Useful for programming an EEPROM to display text. | |
""" | |
from intelhex import IntelHex | |
char2seg = { | |
# got with http://www.uize.com/examples/seven-segment-display.html | |
'0': 0x7E, | |
'1': 0x30, | |
'2': 0x6D, | |
'3': 0x79, | |
'4': 0x33, | |
'5': 0x5B, | |
'6': 0x5F, | |
'7': 0x70, | |
'8': 0x7F, | |
'9': 0x7B, | |
'a': 0x77, | |
'b': 0x1F, | |
'c': 0x0D, | |
'd': 0x3D, | |
'e': 0x4F, | |
'f': 0x47, | |
'g': 0x5E, | |
'h': 0x17, | |
'i': 0x10, | |
'j': 0x38, | |
'k': 0x57, | |
'l': 0x0E, | |
'*': 0x36, # placeholder for ll | |
'#': 0x34, # placeholder for il | |
'&': 0x16, # placeholder for li | |
'n': 0x15, | |
'ñ': 0x55, | |
'o': 0x1d, | |
'p': 0x67, | |
'q': 0x73, | |
'r': 0x05, | |
's': 0x5B, | |
't': 0x0f, | |
'u': 0x3E, | |
'v': 0x3E, | |
'x': 0x37, | |
'y': 0x3B, | |
'z': 0x6D, | |
'-': 0x01, | |
'~': 0x01, | |
'_': 0x08, | |
'=': 0x48, | |
'[': 0x4E, | |
'(': 0x4E, | |
']': 0x78, | |
')': 0x78, | |
"'": 0x20, | |
"`": 0x02, | |
'°': 0x63, | |
'"': 0x22, | |
'@': 0x5F, | |
'?': 0x65, | |
'¿': 0x2D, | |
'!': 0xB0, | |
'^': 0x62, | |
' ': 0x00, | |
} | |
def str27seg(string, address=0, common_cathode=True): | |
"""Creates a IntelHex object with the bytes needed to display a string in | |
a 7 segments display. It suposses the folowing connections. | |
bit | segment | |
0 | g | |
1 | f | |
2 | e | |
3 | d | |
4 | c | |
5 | b | |
6 | a | |
7 | dp | |
replaces m and w by nn and uu. | |
@param string: Cadena a transformar | |
@type string: str | |
@return: mem | |
@rtype : IntelHex | |
""" | |
# *, #, & are our placeholders, so we delete it from the string | |
string = string.replace('*', '').replace('#', '').replace('&', '') | |
string = ' '.join(string.split()) | |
string = string.casefold() | |
string = string.replace('11', '*').replace('ll', '*').replace('il', '#') | |
string = string.replace('li', '&').replace('m', 'nn').replace('w', 'uu') | |
print(string) | |
mem = IntelHex() | |
for char in string: | |
if char in char2seg: | |
previous = char | |
mem[address] = char2seg[char] | |
address += 1 | |
elif char == '.' or char == ',': | |
# Solo ponemos punto si el caracter anterior era representable | |
if previous: | |
mem[address - 1] &= 0x80 | |
else: | |
previous = 0 | |
if not common_cathode: | |
for i in range(len(mem)): | |
mem[i] = ~mem[i] | |
return mem | |
def str2hex(string, filen): | |
hexf = str27seg(string) | |
hexf.write_hex_file(filen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment