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"ÀÅÄÉÈÊËÖÔÜÛÙŸ"
char_offset = {}
char_offsets = {}
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]])
bitmap_preloaded = [([], 0)]
@ -320,7 +320,7 @@ def bitmap_char(ch):
bitmaps_preloaded_unused = False
return bitmap_preloaded[ord(ch)]
o = char_offset[ch]
o = char_offsets[ch]
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.
"""
def colonrepl(m):
def replace_symbolic(m):
name = m.group(1)
if name == '':
return ':'
@ -343,10 +343,9 @@ def bitmap_text(text):
if '.' in name:
bitmap_preloaded.append(bitmap_img(name))
return chr(len(bitmap_preloaded) - 1)
b = bitmap_named[name]
return b[2]
return bitmap_named[name][2]
text = re.sub(r':([^:]*):', colonrepl, text)
text = re.sub(r':([^:]*):', replace_symbolic, text)
buf = array('B')
cols = 0
for c in text:
@ -391,7 +390,7 @@ def bitmap_img(file):
def bitmap(arg):
""" 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):
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
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)] = lengths[i] % 256
cdate = datetime.now()
h[38 + 0] = cdate.year % 100
h[38 + 1] = cdate.month
h[38 + 2] = cdate.day
h[38 + 3] = cdate.hour
h[38 + 4] = cdate.minute
h[38 + 5] = cdate.second
h[38 + 0] = date.year % 100
h[38 + 1] = date.month
h[38 + 2] = date.day
h[38 + 3] = date.hour
h[38 + 4] = date.minute
h[38 + 5] = date.second
return h
if __name__ == '__main__':
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,
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))
bitmaps_preloaded_unused = True
msgs = []
for arg in args.message:
msgs.append(bitmap(arg))
msg_bitmaps = []
for msg_arg in args.message:
msg_bitmaps.append(bitmap(msg_arg))
if bitmaps_preloaded_unused == True:
print(
@ -538,23 +538,23 @@ if bitmaps_preloaded_unused == True:
if '12' in args.type or '12' in sys.argv[0]:
print("Type: 12x48")
for msg in msgs:
for msg_bitmap in msg_bitmaps:
# trivial hack to support 12x48 badges:
# patch extra empty lines into the message stream.
for o in reversed(range(1, int(len(msg[0]) / 11) + 1)):
msg[0][o * 11:o * 11] = array('B', [0])
for o in reversed(range(1, int(len(msg_bitmap[0]) / 11) + 1)):
msg_bitmap[0][o * 11:o * 11] = array('B', [0])
else:
print("Type: 11x44")
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:
buf.extend(msg[0])
for msg_bitmap in msg_bitmaps:
buf.extend(msg_bitmap[0])
needpadding = len(buf) % 64
if needpadding:
buf.extend((0,) * (64 - needpadding))
need_padding = len(buf) % 64
if need_padding:
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])

Loading…
Cancel
Save