]
-def _encode_printer_message(command, pixel_count, rle_image, print_id):
+def _encode_pixels(pixel_count, rle_image):
"""
- :param command: See LittlePrinterCommand in protocol.proto.
:param pixel_count: total number of pixels
:param rle_image: Run-length encoded black and white image.
- :print_id: unique 32-bit number for ack-ing the print (sent back in didprint event)
+ :returns: The binary payload
"""
- LITTLE_PRINTER_DEVICE_ID = 1
-
- # device type c, reserved byte c,
- # command_name <H (short), file_id <L (long),
- # unimplemented CRC (long is zero)
- command_header = struct.pack(
- "<BBHII",
- LITTLE_PRINTER_DEVICE_ID, 0,
- command,
- print_id, 0,
- )
-
printer_control = struct.pack("<13B",
0x1d, 0x73, 0x03, 0xe8, # max printer speed
0x1d, 0x61, 0xd0, # printer acceleration
payload = struct.pack("<IB", len(header_region) + len(rle_image) + 1, 0)
payload += header_region + rle_image
+ return payload
+
+
+def _encode_printer_message(command, payload, print_id):
+ """
+ :param command: See LittlePrinterCommand in protocol.proto.
+ :param pixel_count: total number of pixels
+ :param rle_image: Run-length encoded black and white image.
+ :print_id: unique 32-bit number for ack-ing the print (sent back in didprint event)
+ :returns: The binary payload
+ """
+ LITTLE_PRINTER_DEVICE_ID = 1
+
+ # device type c, reserved byte c,
+ # command_name <H (short), file_id <L (long),
+ # unimplemented CRC (long is zero)
+ command_header = struct.pack(
+ "<BBHII",
+ LITTLE_PRINTER_DEVICE_ID, 0,
+ command,
+ print_id, 0,
+ )
entire_payload = command_header + struct.pack("<I", len(payload)) + payload
return entire_payload
+def _payload_from_pixels(pixels):
+ """
+ :returns: binary payload to be embedded in full message.
+ """
+ pixel_count, rle_pixels = image_encoding.rle_from_bw(pixels)
+ return _encode_pixels(pixel_count, rle_pixels)
+
+
def encode_bridge_command(bridge_address, command, command_id, timestamp):
"""Encodes a BridgeCommand into a dict that can be serialized as json.
})
elif type(command) == messages.SetDeliveryAndPrint:
- pixel_count, pixels = image_encoding.rle_from_bw(command.pixels)
return make({
'binary_payload': base64.b64encode(_encode_printer_message(
- 0x1, pixel_count, pixels, command_id)),
+ 0x1, _payload_from_pixels(command.pixels), command_id)),
'device_address': command.device_address,
'type': 'DeviceCommand',
})
elif type(command) == messages.SetDelivery:
- pixel_count, pixels = image_encoding.rle_from_bw(command.pixels)
return make({
'binary_payload': base64.b64encode(_encode_printer_message(
- 0x2, pixel_count, pixels, command_id)),
+ 0x2, _payload_from_pixels(command.pixels), command_id)),
+ 'device_address': command.device_address,
+ 'type': 'DeviceCommand',
+ })
+
+ elif type(command) == messages.SetPersonality:
+ payload = (
+ _payload_from_pixels(command.face_pixels)
+ + _payload_from_pixels(command.nothing_to_print_pixels)
+ + _payload_from_pixels(command.cannot_see_bridge_pixels)
+ + _payload_from_pixels(command.cannot_see_internet_pixels))
+
+ return make({
+ 'binary_payload': base64.b64encode(_encode_printer_message(
+ 0x2, payload, command_id)),
'device_address': command.device_address,
'type': 'DeviceCommand',
})