Move processing of command line arguments from header() to main code, some clean code refactoring

rebase020124
Ben 1 year ago
parent 745398399f
commit e46aa5acce
  1. 52
      led-badge-11x44.py
  2. 22
      tests/test_led-badge-11x44.py

@ -397,32 +397,30 @@ def bitmap(arg):
return bitmap_text(arg) return bitmap_text(arg)
proto_header = ( def expand_tuple(l):
0x77, 0x61, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, l = l + (l[-1],) * (8 - len(l)) # repeat last element
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, return l
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
)
def header(lengths, speeds, modes, blink, ants, brightness=100, date=datetime.now()): def header(lengths, speeds, modes, blinks, 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.
""" """
a = [int(x) for x in re.split(r'[\s,]+', ants)] protocol_header_template = (
a = a + [a[-1]] * (8 - len(a)) # repeat last element 0x77, 0x61, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b = [int(x) for x in re.split(r'[\s,]+', blink)] 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b = b + [b[-1]] * (8 - len(b)) # repeat last element 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
)
s = [int(x) - 1 for x in re.split(r'[\s,]+', speeds)] ants = expand_tuple(ants)
s = s + [s[-1]] * (8 - len(s)) # repeat last element blinks = expand_tuple(blinks)
speeds = expand_tuple(speeds)
modes = expand_tuple(modes)
m = [int(x) for x in re.split(r'[\s,]+', modes)] speeds = [x - 1 for x in speeds]
m = m + [m[-1]] * (8 - len(m)) # repeat last element
h = list(proto_header) h = list(protocol_header_template)
if brightness <= 25: if brightness <= 25:
h[5] = 0x40 h[5] = 0x40
@ -432,11 +430,11 @@ def header(lengths, speeds, modes, blink, ants, brightness=100, date=datetime.no
h[5] = 0x10 h[5] = 0x10
for i in range(8): for i in range(8):
h[6] += b[i] << i h[6] += blinks[i] << i
h[7] += a[i] << i h[7] += ants[i] << i
for i in range(8): for i in range(8):
h[8 + i] = 16 * s[i] + m[i] h[8 + i] = 16 * speeds[i] + modes[i]
for i in range(len(lengths)): for i in range(len(lengths)):
h[17 + (2 * i) - 1] = lengths[i] // 256 h[17 + (2 * i) - 1] = lengths[i] // 256
@ -452,6 +450,9 @@ def header(lengths, speeds, modes, blink, ants, brightness=100, date=datetime.no
return h return h
def split_to_ints(list_str):
return [int(x) for x in re.split(r'[\s,]+', list_str)]
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
@ -546,8 +547,15 @@ if __name__ == '__main__':
else: else:
print("Type: 11x44") print("Type: 11x44")
lengths = [b[1] for b in msg_bitmaps]
speeds = split_to_ints(args.speeds)
modes = split_to_ints(args.mode)
blinks = split_to_ints(args.blink)
ants = split_to_ints(args.ants)
brightness = int(args.brightness)
buf = array('B') buf = array('B')
buf.extend(header(list(map(lambda x: x[1], msg_bitmaps)), args.speed, args.mode, args.blink, args.ants, int(args.brightness))) buf.extend(header(lengths, speeds, modes, blinks, ants, brightness))
for msg_bitmap in msg_bitmaps: for msg_bitmap in msg_bitmaps:
buf.extend(msg_bitmap[0]) buf.extend(msg_bitmap[0])

@ -11,17 +11,17 @@ class Test(TestCase):
self.test_date = datetime.datetime(2022, 11, 13, 17, 38, 24) self.test_date = datetime.datetime(2022, 11, 13, 17, 38, 24)
def test_header_2msgs(self): def test_header_2msgs(self):
buf = testee.header((6, 7), "5,3", "6,2", "0,1", "1,0", 75, self.test_date) buf = testee.header((6, 7), (5, 3), (6, 2), (0, 1), (1, 0), 75, self.test_date)
self.assertEqual([119, 97, 110, 103, 0, 16, 254, 1, 70, 34, 34, 34, 34, 34, 34, 34, 0, 6, 0, 7, 0, 0, 0, 0, 0, self.assertEqual([119, 97, 110, 103, 0, 16, 254, 1, 70, 34, 34, 34, 34, 34, 34, 34, 0, 6, 0, 7, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0], buf) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], buf)
def test_header_8msgs(self): def test_header_8msgs(self):
buf = testee.header((1, 2, 3, 4, 5, 6, 7, 8), buf = testee.header((1, 2, 3, 4, 5, 6, 7, 8),
"1,2,3,4,5,6,7,8", (1, 2, 3, 4, 5, 6, 7, 8),
"1,2,3,4,5,6,7,8", (1, 2, 3, 4, 5, 6, 7, 8),
"0,1,0,1,0,1,0,1", (0, 1, 0, 1, 0, 1, 0, 1),
"1,0,1,0,1,0,1,0", (1, 0, 1, 0, 1, 0, 1, 0),
25, 25,
self.test_date) self.test_date)
self.assertEqual([119, 97, 110, 103, 0, 64, 170, 85, 1, 18, 35, 52, 69, 86, 103, 120, 0, 1, 0, 2, 0, 3, 0, 4, 0, self.assertEqual([119, 97, 110, 103, 0, 64, 170, 85, 1, 18, 35, 52, 69, 86, 103, 120, 0, 1, 0, 2, 0, 3, 0, 4, 0,
@ -29,26 +29,26 @@ class Test(TestCase):
0, 0, 0, 0, 0, 0, 0, 0, 0, 0], buf) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], buf)
def test_header_brightness(self): def test_header_brightness(self):
buf = testee.header((6,), "4", "4", "0", "0", 25, self.test_date) buf = testee.header((6,), (4,), (4,), (0,), (0,), 25, self.test_date)
self.assertEqual([119, 97, 110, 103, 0, 64, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, self.assertEqual([119, 97, 110, 103, 0, 64, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], buf) 0, 0, 0, 0, 0, 0, 0, 0, 0], buf)
buf = testee.header((6,), "4", "4", "0", "0", 26, self.test_date) buf = testee.header((6,), (4,), (4,), (0,), (0,), 26, self.test_date)
self.assertEqual([119, 97, 110, 103, 0, 32, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, self.assertEqual([119, 97, 110, 103, 0, 32, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], buf) 0, 0, 0, 0, 0, 0, 0, 0, 0], buf)
buf = testee.header((6,), "4", "4", "0", "0", 60, self.test_date) buf = testee.header((6,), (4,), (4,), (0,), (0,), 60, self.test_date)
self.assertEqual([119, 97, 110, 103, 0, 16, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, self.assertEqual([119, 97, 110, 103, 0, 16, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], buf) 0, 0, 0, 0, 0, 0, 0, 0, 0], buf)
buf = testee.header((6,), "4", "4", "0", "0", 80, self.test_date) buf = testee.header((6,), (4,), (4,), (0,), (0,), 80, self.test_date)
self.assertEqual([119, 97, 110, 103, 0, 0, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, self.assertEqual([119, 97, 110, 103, 0, 0, 0, 0, 52, 52, 52, 52, 52, 52, 52, 52, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 11, 13, 17, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], buf) 0, 0, 0, 0, 0, 0, 0, 0, 0], buf)
def test_header_date(self): def test_header_date(self):
buf1 = testee.header((6,), "4", "4", "0", "0", 100, self.test_date) buf1 = testee.header((6,), (4,), (4,), (0,), (0,), 100, self.test_date)
buf2 = testee.header((6,), "4", "4", "0", "0", 100) buf2 = testee.header((6,), (4,), (4,), (0,), (0,), 100)
self.assertEqual(buf1[0:38], buf2[0:38]) self.assertEqual(buf1[0:38], buf2[0:38])
self.assertEqual(buf1[38 + 6:], buf2[38 + 6:]) self.assertEqual(buf1[38 + 6:], buf2[38 + 6:])
self.assertNotEqual(buf1[38:38 + 6], buf2[38:38 + 6]) self.assertNotEqual(buf1[38:38 + 6], buf2[38:38 + 6])

Loading…
Cancel
Save