def encodeFlags(bools): from bitstring import BitString bits=BitString() for bool in bools: if bool: bits.append(BitString('0b1')) else: bits.append(BitString('0b0')) return bits.bytes
def get_status(self): self.can_h.send_msg((0x40, 0x00, 0x21, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = self.can_h.read_msg() while not (msg[0]==0x4B and msg[1]==0x00 and msg[2]==0x21): msg, msgId, time = self.can_h.read_msg() status = BitString("0x%02x " % msg[5]) status.append("0x%02x " % msg[4]) return status.uint
def wait_till_error(self, timeout): # self.can_h.send_msg((0x40, 0x41, 0x60, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = None, None, None while not (msgId==0x081 and msg[0]==0x30 and msg[1]==0x81): msg, msgId, time = self.can_h.read_msg(timeout) error_code = BitString("0x%02x " % msg[1]) error_code.append("0x%02x " % msg[0]) return error_code
def get_voltage(self): self.can_h.send_msg((0x40, 0x00, 0x23, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = self.can_h.read_msg() while not (msg[0]==0x4B and msg[1]==0x00 and msg[2]==0x23): msg, msgId, time = self.can_h.read_msg() voltage = BitString("0x%02x " % msg[5]) voltage.append("0x%02x " % msg[4]) return voltage.uint
def get_temperature(self): self.can_h.send_msg((0x40, 0x02, 0x23, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = self.can_h.read_msg() while not (msg[0]==0x4B and msg[1]==0x02 and msg[2]==0x23): msg, msgId, time = self.can_h.read_msg() temp = BitString("0x%02x " % msg[5]) temp.append("0x%02x " % msg[4]) return temp.uint
def get_motor_current(self): '''returned in milliamperes ''' self.can_h.send_msg((0x40, 0x01, 0x23, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = self.can_h.read_msg() while not (msg[0]==0x4b and msg[1]==0x01 and msg[2]==0x23): msg, msgId, time = self.can_h.read_msg() current = BitString("0x%02x " % msg[5]) current.append("0x%02x " % msg[4]) return current.uint
def get_actual_speed(self): '''returned speed in shaft''' self.can_h.send_msg((0x40, 0x02, 0x22, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = self.can_h.read_msg() while not (msg[0]==0x43 and msg[1]==0x02 and msg[2]==0x22): msg, msgId, time = self.can_h.read_msg() speed = BitString("0x%02x " % msg[5]) speed.append("0x%02x " % msg[4]) return speed.uint/810.0
def get_actual_position(self): '''returned position in shaft''' self.can_h.send_msg((0x40, 0x64, 0x60, 0x00, 00, 00, 0x00, 0x00), self.node_id) msg, msgId, time = self.can_h.read_msg() while not (msg[0]==0x43 and msg[1]==0x64 and msg[2]==0x60): msg, msgId, time = self.can_h.read_msg() pos = BitString("0x%02x " % msg[5]) pos.append("0x%02x " % msg[4]) return pos.uint/81.0
def decode(s, storage=BIT_STORAGE, alpha=ALPHABET): """ Accepts any iterable object, you can override storage and alpha keywords to change to another language set and storage mechanism Returns a string/bytes """ n = [ord(a) for a in s if a != TWUUENC_START and a != TWUUENC_START_ZLIB] bs = BitString() for a in n: for pos,l in enumerate(alpha): if a == l: bs.append(BitString(uint=pos, length=storage)) bs.seekbyte(0) return bs.readbytes(len(bs)/8).data.rstrip('\0')
def get_swf(self): if not self.parsed: return "" b = BitString(bytes="") b.append("uint:5=%d" % self.sizelen) if self.sizelen > 0: b.append("uint:%d=%d" % (self.sizelen, self.xmin)) b.append("uint:%d=%d" % (self.sizelen, self.xmax)) b.append("uint:%d=%d" % (self.sizelen, self.ymin)) b.append("uint:%d=%d" % (self.sizelen, self.ymax)) taillen = (self.sizelen * 4 + 5) % 8 != 0 and 8 - (self.sizelen * 4 + 5) % 8 or 0 else: taillen = 3 b.append("uint:%d=0" % taillen) b.append("uint:16=%d" % self.framerate) b.append("uintle:16=%d" % self.framecount) for tagcode, tagdata in self.tags: taglen = len(tagdata) if taglen >= 0x3F: b.append("uintle:16=%d" % ((tagcode << 6) | 0x3F)) b.append("intle:32=%d" % len(tagdata)) else: b.append("uintle:16=%d" % ((tagcode << 6) | taglen)) b.append(BitString(bytes=tagdata)) data = b.tobytes() self.filelength = len(data) if self.signature == "CWS": data = zlib.compress(data) b = BitString(bytes="") b.append(BitString(bytes=self.signature)) b.append(BitString("uint:8=%d" % self.version)) b.append(BitString("uintle:32=%d" % self.filelength)) header = b.tobytes() return header + data
def generate(height, codepoints, font_file_path, output_file_name, packed, code, smoke): font = Font(font_file_path, height) if smoke: glyphs = [chr(x) for x in codepoints] f = sys.stdout.buffer if output_file_name is not None: f = open(output_file_name, 'wt') for cur_glyph in glyphs: ch = font.render_character(cur_glyph) f.write(cur_glyph + '\n') f.write(repr(ch)) f.write('\n\n') f.close() return ili9341_t3_font = OrderedDict() ili9341_t3_font['index'] = 0 ili9341_t3_font['unicode'] = 0 ili9341_t3_font['data'] = 0 ili9341_t3_font['version'] = 1 ili9341_t3_font['reserved'] = 0 ili9341_t3_font['index1_first'] = (len(codepoints) & 0xff00) >> 8 ili9341_t3_font['index1_last'] = len(codepoints) & 0xff ili9341_t3_font['index2_first'] = 0 ili9341_t3_font['index2_last'] = 0 ili9341_t3_font['bits_index'] = 0 ili9341_t3_font['bits_width'] = 0 ili9341_t3_font['bits_height'] = 0 ili9341_t3_font['bits_xoffset'] = 0 ili9341_t3_font['bits_yoffset'] = 0 ili9341_t3_font['bits_delta'] = 0 ili9341_t3_font['line_space'] = font.height e_cap = font.glyph_for_character('E') ili9341_t3_font['cap_height'] = e_cap.height - e_cap.descent max_width = 1 max_height = 1 max_xoffset = 1 max_yoffset = 1 max_delta = 1 glyph_data = dict() for codepoint in codepoints: cur_glyph = chr(codepoint) glyph_header = build_glyph(cur_glyph, font, glyph_data) max_width = max(max_width, glyph_header['width']) max_height = max(max_height, glyph_header['height']) max_xoffset = max(abs(max_xoffset), abs(glyph_header['xoffset'])) max_yoffset = max(abs(max_yoffset), abs(glyph_header['yoffset'])) max_delta = max(max_delta, glyph_header['delta']) ili9341_t3_font['bits_width'] = int(math.floor(math.log(max_width, 2))) + 1 ili9341_t3_font['bits_height'] = int(math.floor(math.log(max_height, 2))) + 1 ili9341_t3_font['bits_xoffset'] = int(math.floor(math.log(max_xoffset, 2))) + 2 ili9341_t3_font['bits_yoffset'] = int(math.floor(math.log(max_yoffset, 2))) + 2 ili9341_t3_font['bits_delta'] = int(math.floor(math.log(max_delta, 2))) + 1 output_data = bytearray() index = list() total_size = 0 for codepoint in codepoints: ch = chr(codepoint) index.append(total_size) glyph = glyph_data[ch] glyph_bytes = pack_glyph(glyph, ili9341_t3_font) output_data.extend(glyph_bytes) total_size += len(glyph_bytes) ili9341_t3_font['bits_index'] = int(math.floor(math.log(total_size, 2))) + 1 index_bits = BitString() for idx in index: index_bits.append(Bits(uint=idx, length=ili9341_t3_font['bits_index'])) codepoint_table = BitString() for codepoint in codepoints: codepoint_table.append(Bits(uint=codepoint, length=21)) if packed: f = sys.stdout.buffer if output_file_name is not None: f = open(output_file_name, 'wb') f.write(struct.pack('<3I14Bxx', *tuple(ili9341_t3_font.values()))) index_bits.tofile(f) codepoint_table.tofile(f) f.write(output_data) f.close() if code: f = sys.stdout.buffer if output_file_name is not None: f = open(output_file_name, 'wb') variable_name = os.path.splitext(os.path.basename(font_file_path))[0] + '_' + str(height) c = io.StringIO() c.write('// extern const ILI9341_t3_font_t {};\n\n'.format(variable_name)) c.write('static const unsigned char {}_data[] = {{\n'.format(variable_name)) data_byte_array = ['0x' + binascii.hexlify(bytes([x])).decode() for x in output_data] for i in range(0, len(data_byte_array), 10): c.write(','.join(data_byte_array[i:i + 10]) + ',\n') c.write('};\n') c.write('/* font data size: {} bytes */\n\n'.format(len(data_byte_array))) c.write('static const unsigned char {}_index[] = {{\n'.format(variable_name)) index_byte_array = ['0x' + binascii.hexlify(bytes([x])).decode() for x in index_bits.tobytes()] for i in range(0, len(index_byte_array), 10): c.write(','.join(index_byte_array[i:i + 10]) + ',\n') c.write('};\n') c.write('/* font index size: {} bytes */\n\n'.format(len(index_byte_array))) c.write('static const unsigned char {}_codepoints[] = {{\n'.format(variable_name)) codepoint_byte_array = ['0x' + binascii.hexlify(bytes([x])).decode() for x in codepoint_table.tobytes()] for i in range(0, len(codepoint_byte_array), 10): c.write(','.join(codepoint_byte_array[i:i + 10]) + ',\n') c.write('};\n') c.write('/* Unicode codepoint table size: {} bytes */\n\n'.format(len(codepoint_byte_array))) c.write('const ILI9341_t3_font_t {} = {{\n'.format(variable_name)) c.write(' {}_index,\n'.format(variable_name)) c.write(' {}_codepoints,\n'.format(variable_name)) c.write(' {}_data,\n'.format(variable_name)) c.write(' {},\n'.format(ili9341_t3_font['version'])) c.write(' {},\n'.format(ili9341_t3_font['reserved'])) c.write(' {},\n'.format(ili9341_t3_font['index1_first'])) c.write(' {},\n'.format(ili9341_t3_font['index1_last'])) c.write(' {},\n'.format(ili9341_t3_font['index2_first'])) c.write(' {},\n'.format(ili9341_t3_font['index2_last'])) c.write(' {},\n'.format(ili9341_t3_font['bits_index'])) c.write(' {},\n'.format(ili9341_t3_font['bits_width'])) c.write(' {},\n'.format(ili9341_t3_font['bits_height'])) c.write(' {},\n'.format(ili9341_t3_font['bits_xoffset'])) c.write(' {},\n'.format(ili9341_t3_font['bits_yoffset'])) c.write(' {},\n'.format(ili9341_t3_font['bits_delta'])) c.write(' {},\n'.format(ili9341_t3_font['line_space'])) c.write(' {}\n'.format(ili9341_t3_font['cap_height'])) c.write('};\n') f.write(c.getvalue().encode('ascii'))
def pack_glyph(glyph, ili9341_t3_font): glyph_bits = BitString() header = glyph['header'] glyph_bits.append(Bits(uint=header['encoding'], length=3)) glyph_bits.append(Bits(uint=header['width'], length=ili9341_t3_font['bits_width'])) glyph_bits.append(Bits(uint=header['height'], length=ili9341_t3_font['bits_height'])) glyph_bits.append(Bits(int=header['xoffset'], length=ili9341_t3_font['bits_xoffset'])) glyph_bits.append(Bits(int=header['yoffset'], length=ili9341_t3_font['bits_yoffset'])) glyph_bits.append(Bits(uint=header['delta'], length=ili9341_t3_font['bits_delta'])) for row in glyph['data']: if row['repeat'] == 0: glyph_bits.append(Bits(bool=False)) else: glyph_bits.append(Bits(bool=True)) glyph_bits.append(Bits(uint=row['repeat'] - 1, length=3)) for bit in row['bits']: glyph_bits.append(Bits(bool=bit == 1)) glyph_bytes = glyph_bits.tobytes() return glyph_bytes
def get_swf(self): if not self.parsed: return '' b = BitString(bytes='') b.append('uint:5=%d'%self.sizelen) if self.sizelen>0: b.append('uint:%d=%d'%(self.sizelen,self.xmin)) b.append('uint:%d=%d'%(self.sizelen,self.xmax)) b.append('uint:%d=%d'%(self.sizelen,self.ymin)) b.append('uint:%d=%d'%(self.sizelen,self.ymax)) taillen = (self.sizelen*4+5)%8 != 0 and 8-(self.sizelen*4+5)%8 or 0 else: taillen = 3 b.append('uint:%d=0'%taillen) b.append('uint:16=%d'%self.framerate) b.append('uintle:16=%d'%self.framecount) for tagcode,tagdata in self.tags: taglen = len(tagdata) if taglen >= 0x3f: b.append('uintle:16=%d'%((tagcode<<6)|0x3f)) b.append('intle:32=%d'%len(tagdata)) else: b.append('uintle:16=%d'%((tagcode<<6)|taglen)) b.append(BitString(bytes=tagdata)) data = b.tobytes() self.filelength = len(data) if self.signature == 'CWS': data = zlib.compress(data) b = BitString(bytes='') b.append(BitString(bytes=self.signature)) b.append(BitString('uint:8=%d'%self.version)) b.append(BitString('uintle:32=%d'%self.filelength)) header = b.tobytes() return header + data