#!/usr/bin/env python3 import serial import struct from itertools import takewhile def chunked(data, chunk_size): for i in range(0, len(data), chunk_size): yield data[i:i+chunk_size] def frame_packet(data): if len(data) > 254: raise ValueError('Input too long') out = b'' for run in data.split(b'\0'): out += bytes([len(run)+1]) out += run out += b'\0' return out def format_packet(data): out = b'' for a, b, c, d, e, f, g, h in chunked(data, 8): ah, bh, ch, dh = a>>8, b>>8, c>>8, d>>8 eh, fh, gh, hh = e>>8, f>>8, g>>8, h>>8 al, bl, cl, dl = a&0xff, b&0xff, c&0xff, d&0xff el, fl, gl, hl = e&0xff, f&0xff, g&0xff, h&0xff # FIXME check order of high bits out += bytes([al, bl, cl, dl, el, fl, gl, hl, (ah<<6 | bh<<4 | ch<<2 | dh<<0)&0xff, (eh<<6 | fh<<4 | gh<<2 | hh<<0)&0xff]) out += bytes([1, 0, 0, 0]) # global intensity return out def chariter(ser): while True: yield ser.read(1) def read_frame(ser): return b''.join(takewhile(lambda c: c and c[0], chariter(ser))) def unstuff(data): out = b'' while data: stuff = data[0] if out: out += b'\0' out += data[1:stuff] data = data[stuff:] return out def receive_frame(ser): return unstuff(read_frame(ser)) def mac_frame(mac): return frame_packet(struct.pack('