Some small refactorings

rebase020124
Ben 1 year ago
parent be3ba32458
commit f1849a5f88
  1. 56
      led-badge-11x44.py

@ -247,9 +247,9 @@ charmap = u'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + \
u"àäòöùüèéêëôöûîïÿç" + \ u"àäòöùüèéêëôöûîïÿç" + \
u"ÀÅÄÉÈÊËÖÔÜÛÙŸ" u"ÀÅÄÉÈÊËÖÔÜÛÙŸ"
char_offset = {} char_offsets = {}
for i in range(len(charmap)): for i in range(len(charmap)):
char_offset[charmap[i]] = 11 * i char_offsets[charmap[i]] = 11 * i
# print(i, charmap[i], char_offset[charmap[i]]) # print(i, charmap[i], char_offset[charmap[i]])
bitmap_preloaded = [([], 0)] bitmap_preloaded = [([], 0)]
@ -320,7 +320,7 @@ def bitmap_char(ch):
bitmaps_preloaded_unused = False bitmaps_preloaded_unused = False
return bitmap_preloaded[ord(ch)] return bitmap_preloaded[ord(ch)]
o = char_offset[ch] o = char_offsets[ch]
return (font_11x44[o:o + 11], 1) return (font_11x44[o:o + 11], 1)
@ -334,7 +334,7 @@ def bitmap_text(text):
":gfx/logo.png:" preloads the file gfx/logo.png and is replaced the corresponding control char. ":gfx/logo.png:" preloads the file gfx/logo.png and is replaced the corresponding control char.
""" """
def colonrepl(m): def replace_symbolic(m):
name = m.group(1) name = m.group(1)
if name == '': if name == '':
return ':' return ':'
@ -343,10 +343,9 @@ def bitmap_text(text):
if '.' in name: if '.' in name:
bitmap_preloaded.append(bitmap_img(name)) bitmap_preloaded.append(bitmap_img(name))
return chr(len(bitmap_preloaded) - 1) return chr(len(bitmap_preloaded) - 1)
b = bitmap_named[name] return bitmap_named[name][2]
return b[2]
text = re.sub(r':([^:]*):', colonrepl, text) text = re.sub(r':([^:]*):', replace_symbolic, text)
buf = array('B') buf = array('B')
cols = 0 cols = 0
for c in text: for c in text:
@ -391,7 +390,7 @@ def bitmap_img(file):
def bitmap(arg): def bitmap(arg):
""" if arg is a valid and existing path name, we load it as an image. """ if arg is a valid and existing path name, we load it as an image.
Otherwise we take it as a string. Otherwise, we take it as a string.
""" """
if os.path.exists(arg): if os.path.exists(arg):
return bitmap_img(arg) return bitmap_img(arg)
@ -406,7 +405,7 @@ proto_header = (
) )
def header(lengths, speeds, modes, blink, ants, brightness=100): def header(lengths, speeds, modes, blink, ants, brightness=100, date=datetime.now()):
""" lengths[0] is the number of chars of the first text """ lengths[0] is the number of chars of the first text
Speeds come in as 1..8, but are needed 0..7 here. Speeds come in as 1..8, but are needed 0..7 here.
@ -443,17 +442,18 @@ def header(lengths, speeds, modes, blink, ants, brightness=100):
h[17 + (2 * i) - 1] = lengths[i] // 256 h[17 + (2 * i) - 1] = lengths[i] // 256
h[17 + (2 * i)] = lengths[i] % 256 h[17 + (2 * i)] = lengths[i] % 256
cdate = datetime.now() h[38 + 0] = date.year % 100
h[38 + 0] = cdate.year % 100 h[38 + 1] = date.month
h[38 + 1] = cdate.month h[38 + 2] = date.day
h[38 + 2] = cdate.day h[38 + 3] = date.hour
h[38 + 3] = cdate.hour h[38 + 4] = date.minute
h[38 + 4] = cdate.minute h[38 + 5] = date.second
h[38 + 5] = cdate.second
return h return h
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description='Upload messages or graphics to a 11x44 led badge via USB HID.\nVersion %s from https://github.com/jnweiger/led-badge-ls32\n -- see there for more examples and for updates.' % __version, description='Upload messages or graphics to a 11x44 led badge via USB HID.\nVersion %s from https://github.com/jnweiger/led-badge-ls32\n -- see there for more examples and for updates.' % __version,
epilog='Example combining image and text:\n sudo %s "I:HEART2:you"' % sys.argv[0]) epilog='Example combining image and text:\n sudo %s "I:HEART2:you"' % sys.argv[0])
@ -528,9 +528,9 @@ if args.preload:
bitmap_preloaded.append(bitmap_img(file)) bitmap_preloaded.append(bitmap_img(file))
bitmaps_preloaded_unused = True bitmaps_preloaded_unused = True
msgs = [] msg_bitmaps = []
for arg in args.message: for msg_arg in args.message:
msgs.append(bitmap(arg)) msg_bitmaps.append(bitmap(msg_arg))
if bitmaps_preloaded_unused == True: if bitmaps_preloaded_unused == True:
print( print(
@ -538,23 +538,23 @@ if bitmaps_preloaded_unused == True:
if '12' in args.type or '12' in sys.argv[0]: if '12' in args.type or '12' in sys.argv[0]:
print("Type: 12x48") print("Type: 12x48")
for msg in msgs: for msg_bitmap in msg_bitmaps:
# trivial hack to support 12x48 badges: # trivial hack to support 12x48 badges:
# patch extra empty lines into the message stream. # patch extra empty lines into the message stream.
for o in reversed(range(1, int(len(msg[0]) / 11) + 1)): for o in reversed(range(1, int(len(msg_bitmap[0]) / 11) + 1)):
msg[0][o * 11:o * 11] = array('B', [0]) msg_bitmap[0][o * 11:o * 11] = array('B', [0])
else: else:
print("Type: 11x44") print("Type: 11x44")
buf = array('B') buf = array('B')
buf.extend(header(list(map(lambda x: x[1], msgs)), args.speed, args.mode, args.blink, args.ants, int(args.brightness))) buf.extend(header(list(map(lambda x: x[1], msg_bitmaps)), args.speed, args.mode, args.blink, args.ants, int(args.brightness)))
for msg in msgs: for msg_bitmap in msg_bitmaps:
buf.extend(msg[0]) buf.extend(msg_bitmap[0])
needpadding = len(buf) % 64 need_padding = len(buf) % 64
if needpadding: if need_padding:
buf.extend((0,) * (64 - needpadding)) buf.extend((0,) * (64 - need_padding))
# print(buf) # array('B', [119, 97, 110, 103, 0, 0, 0, 0, 48, 48, 48, 48, 48, 48, 48, 48, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 126, 255, 255, 255, 255, 126, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # print(buf) # array('B', [119, 97, 110, 103, 0, 0, 0, 0, 48, 48, 48, 48, 48, 48, 48, 48, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 126, 255, 255, 255, 255, 126, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Loading…
Cancel
Save