# seven_segs -- generate 7 segment display characters # Copyright (C) 2007 RADLogic # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; # version 2.1 of the License. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # See http://www.fsf.org/licensing/licenses/lgpl.txt for full license text. """Provide helper classes for generating 7 segment display characters.""" __author__ = 'Tim Wegener ' __date__ = '$Date: 2007/03/27 05:09:05 $' __version__ = '$Revision: 0.8 $' class SegChar: """Definiton of a 7-segment display character. Segment names: a --- | | f| |b | g | --- | | e| |c | | --- .h d """ seg_names = 'abcdefgh' seg_map = {} i = 0 for seg_name in seg_names: seg_map[seg_name] = 2 ** i i += 1 def __init__(self, seg_names): """ seg_names -- string of segment names to be switched on. """ code = 0 for char in seg_names: code += self.seg_map[char] self.code = code def __int__(self): return self.code def segs(self): """Return sequence of segment names that are on for this char. >>> seg_char = SegChar('') >>> seg_char.code = 1 >>> seg_char.segs() 'a' """ segs = [] val = self.code seg_i = 0 for seg_name in self.seg_names: if self.code & self.seg_map[seg_name]: segs.append(seg_name) return ''.join(segs) class SegDisplay: """Map strings of characters to 7-segment display codes.""" seg_chars = { # uppercase letters 'A': SegChar('afbgec'), 'B': SegChar('afbgecd'), 'C': SegChar('adef'), 'D': SegChar('afbecd'), 'E': SegChar('adefg'), 'F': SegChar('agef'), 'G': SegChar('acdef'), 'H': SegChar('bcefg'), 'I': SegChar('bc'), 'J': SegChar('bcde'), 'K': SegChar('bcefg'), 'L': SegChar('def'), 'M': SegChar('abcefh'), 'N': SegChar('abcef'), 'O': SegChar('afbecd'), 'P': SegChar('abefg'), 'Q': SegChar('afbecdh'), 'R': SegChar('afe'), 'S': SegChar('acdfg'), 'T': SegChar('fged'), 'U': SegChar('bcdef'), 'V': SegChar('bcdef'), 'W': SegChar('bcdef'), 'X': SegChar('bcefg'), 'Y': SegChar('bcdfg'), 'Z': SegChar('abdeg'), # lowercase letters 'a': SegChar('afbgec'), 'b': SegChar('cdefg'), 'c': SegChar('deg'), 'd': SegChar('bcdeg'), 'e': SegChar('adefg'), 'f': SegChar('aefg'), 'g': SegChar('abcdfg'), 'h': SegChar('cefg'), 'i': SegChar('c'), 'j': SegChar('bcd'), 'k': SegChar('befgh'), 'l': SegChar('def'), 'm': SegChar('cegh'), 'n': SegChar('ceg'), 'o': SegChar('cdeg'), 'p': SegChar('abefg'), 'q': SegChar('abcfg'), 'r': SegChar('eg'), 's': SegChar('afgcd'), 't': SegChar('fged'), 'u': SegChar('ecd'), 'v': SegChar('ecd'), 'w': SegChar('ecd'), 'x': SegChar('bcefg'), 'y': SegChar('bcdfg'), 'z': SegChar('abdeg'), ' ': SegChar(''), '.': SegChar('h'), '_': SegChar('d'), '~': SegChar('a'), '-': SegChar('g'), '|': SegChar('ef'), '\'': SegChar('f'), '`': SegChar('b'), '"': SegChar('bf'), '?': SegChar('abgeh'), '!': SegChar('bch'), '@': SegChar('abcdeg'), '*': SegChar('abfg'), '[': SegChar('adef'), ']': SegChar('abcd'), # digits '0': SegChar('abcdef'), '1': SegChar('bc'), '2': SegChar('abdeg'), '3': SegChar('abcdg'), '4': SegChar('bcfg'), '5': SegChar('acdfg'), '6': SegChar('acdefg'), '7': SegChar('abc'), '8': SegChar('abcdefg'), '9': SegChar('abcdfg'), } def str2bytes(self, s): """Return list of code bytes for text string to be displayed.""" bytes = [] for char in s: try: seg_char = self.seg_chars[char] except KeyError: raise KeyError("Character '%s' cannot be displayed" % char) bytes.append(int(seg_char)) return bytes def str2segs(self, s): """Return list of code bytes for text string to be displayed. >>> segs = SegDisplay() >>> segs.str2segs('ab') == (chr(int(segs.seg_chars['a'])) ... + chr(int(segs.seg_chars['b']))) 1 """ bytes = [] dotvalue = 0 for char in s: if char == '.': # add the h segment to the next character (byte reversed) dotvalue = 128 else: try: seg_char = self.seg_chars[char] except KeyError: raise KeyError("Character '%s' cannot be displayed" % char) bytes.append(chr(int(seg_char)+dotvalue)) dotvalue = 0 bytes = ''.join(bytes) return bytes