def mac_string(device_id): """ Converts a device id into a mac address hex string :param device_id: The device id :returns: str -- The mac address represented as a string """ return hexlify(byteswap('6', pack('u48', device_id)))
def make_frame_address(target, ack_required, res_required, sequence): target = target.split(':') target.reverse() target = ''.join(target) target = int(target, 16) unswapped_header = pack(FRAME_ADDRESS_FORMAT, target, 0, 0, ack_required, res_required, sequence) frame_address = byteswap(FRAME_ADDRESS_BYTESWAP, unswapped_header) return frame_address
def pack_section(section, *args): """ Packs bytes into a header including the swap to little-endian :param section: The definition of the format and byteswap for this section :param \*args: Values to include in the section in the order they are in the format :returns: bytearray -- The packed bytes of the section """ return byteswap(section['byteswap'], pack(section['format'], *args))
def __bytes__(self): return struct.pack(">B", self.led) + bs.byteswap( "3", bs.pack( "u3" * 8, self.color_mode, self.led_mode, *(hex_color_to_vkb_color(self.color2)[::-1]), *(hex_color_to_vkb_color(self.color1)[::-1]), ), )
def frombytes(cls, buf): """ Creates an :class:`LEDConfig` from a bytes object """ assert len(buf) == 4 led_id = int(buf[0]) buf = bs.byteswap("3", buf[1:]) clm, lem, b2, g2, r2, b1, g1, r1 = bs.unpack("u3" * 8, buf) return cls( led=led_id, color_mode=clm, led_mode=lem, color1=vkb_color_to_hex_color([r1, g1, b1]), color2=vkb_color_to_hex_color([r2, g2, b2]), )
def interpretPacket(bits): if type(bits) != bytearray: bits = bytearray(bits) frameFormat = 'u16u2u1u1u12u32' frameEndianSwap ='224' frameAddressFormat = 'u64u48u6u1u1u8' frameAddressEndianSwap = '8611' protocolHeaderFormat ='u64u16u16' # bit structures from LIFX packet protocol protocolHeaderEndianSwap = '822' frameLength = int(calcsize(frameFormat)/8) frameAddressLength = int(calcsize(frameAddressFormat)/8) protocolHeaderLength = int(calcsize(protocolHeaderFormat)/8) # all /8 since we want bytes not bits # parse through packet start = 0 end = frameLength frameFormatBits = bits[start:end] frameData = unpack(frameFormat,byteswap(frameEndianSwap,frameFormatBits)) #unpack bits into big-endian values start = end end = start + frameAddressLength frameAddressBits = bits[start:end] frameAddressData = unpack(frameAddressFormat,byteswap(frameAddressEndianSwap, frameAddressBits)) start = end end = start + protocolHeaderLength protocolHeaderBits = bits[start:end] protocolHeaderData = unpack(protocolHeaderFormat,byteswap(protocolHeaderEndianSwap,protocolHeaderBits)) #print('frame:',frameData) #print('frame address:', frameAddressData) #print('protocol header:',protocolHeaderData) return protocolHeaderData[1]
def ioctlCommandGenerate(): #ioctlCmd = bitstruct.byteswap("8", bitstruct.pack(">u2u14u8u8", 3, ctypes.sizeof(CSRBprotocolCMDmoduleExecute), 1, 1)) ioctlCmd = bitstruct.pack(">u3u13u8u8", 6, ctypes.sizeof(CSRBprotocolCMDmoduleExecute), 1, 1) #print("ioctlCmd initial assembly: " + ioctlCmd.hex()) ioctlCmd = bitstruct.byteswap("8", ioctlCmd) #print("ioctlCmd byte swapped: " + ioctlCmd.hex()) ioctlCmdInt = int.from_bytes(ioctlCmd, byteorder='little') #print("ioctlCmdInt LE int: " + str(hex(ioctlCmdInt))) return ioctlCmdInt
def _pack_bl(self, imm32): if imm32 < 0: imm32 += (1 << 24) s = (imm32 >> 23) i1 = ((imm32 >> 22) & 0x1) i2 = ((imm32 >> 21) & 0x1) j1 = -((i1 ^ s) - 1) j2 = -((i2 ^ s) - 1) imm10 = ((imm32 >> 11) & 0x3ff) imm11 = (imm32 & 0x7ff) value = self._CF_BL.pack(0b11110, s, imm10, 0b11, j1, 0b1, j2, imm11) return bitstruct.byteswap('22', value)
def unpack_section(section, data): """ Unpacks bytes into data, including the endian swap :param section: The definition of the format, byteswap and namedtuple for this section :param data: The bytes to unpack into the tuple :returns: namedtuple -- A namedtuple containing the data from the section """ # Bitstruct only takes byte arrays, some things give us strings if type(data) != bytearray: data = bytearray(data) unpacked = unpack(section['format'], byteswap(section['byteswap'], data)) return section['fields'](*unpacked)
def set_zone_color(average_screen_color, duration, mac_address, start, end): payload_format = 'u8u8u16u16u16u16u32u8' payload_byteswap = '11222241' packet_size = (sizeof(FRAME_HEADER_FORMAT + FRAME_ADDRESS_FORMAT + PROTOCOL_HEADER_FORMAT + payload_format)) / 8 frame_header = make_frame_header(packet_size, 0, 0, 1, 1024, 0) frame_address = make_frame_address(mac_address, 0, 0, 0) protocol_header = make_protocol_header(501) header = frame_header + frame_address + protocol_header unswapped_payload = pack(payload_format, start, end, *average_screen_color, duration, 1) payload = byteswap(payload_byteswap, unswapped_payload) packet = header + payload send_packet(packet)
def _pack_bw(self, value): if value < 0: value += (1 << 25) t = (value & 0x1) cond = ((value >> 1) & 0xf) imm32 = (value >> 5) s = (imm32 >> 19) j2 = ((imm32 >> 18) & 0x1) j1 = ((imm32 >> 17) & 0x1) imm6 = ((imm32 >> 11) & 0x3f) imm11 = (imm32 & 0x7ff) value = self._CF_BW.pack(0b11110, s, cond, imm6, 0b10, j1, t, j2, imm11) return bitstruct.byteswap('22', value)
def make_frame_header(size, origin, tagged, addressable, protocol, source): unswapped_header = pack(FRAME_HEADER_FORMAT, size, origin, 0, addressable, protocol, source) frame_header = byteswap(FRAME_HEADER_BYTESWAP, unswapped_header) return frame_header
def make_protocol_header(message_type): unswapped_header = pack(PROTOCOL_HEADER_FORMAT, 0, message_type, 0) protocol_header = byteswap(PROTOCOL_HEADER_BYTESWAP, unswapped_header) return protocol_header
(239, 227, 189), # white (63, 57, 45), # black ] hls_colors = [ colorsys.rgb_to_hls(x[0] / 256.0, x[1] / 256.0, x[2] / 256.0) for x in rgb_colors ] num_entries = 9 skip = 4 size = (num_entries - skip) * len(rgb_colors) * 2 with open("enemy_palette.bin", 'rb') as infile: rgb_1555 = [ bitstruct.unpack('u1u5u5u5', bitstruct.byteswap('21', infile.read(2))) for _ in range(0, num_entries) ][skip:] base_palette_rgb = [(r / 32.0, g / 32.0 + gl / 64.0, b / 32.0) for gl, r, g, b in rgb_1555] base_palette_hls = [ colorsys.rgb_to_hls(r, g, b) for r, g, b in base_palette_rgb ] with open("enemy_pal.c", 'w') as outfile: outfile.write("unsigned char enemy_pal[" + str(size) + "] = {\n") for target_hls in hls_colors: for base_hls in base_palette_hls: colorized_hls = colorize(base_hls, target_hls) h, l, s = colorized_hls colorized_rgb = colorsys.hls_to_rgb(h, l, s) r, g, b = colorized_rgb
def read_words(args, address, length): serial_connection = serial_open_ensure_connected(args.port) payload = struct.pack('>II', address, 4 * length) words = execute_command(serial_connection, COMMAND_TYPE_READ, payload) return bitstruct.byteswap(length * '4', words)