]> git.aero2k.de Git - sirius.git/commitdiff
Split printer encoding for a single image into a separate method.
authorTom Hunger <tehunger@gmail.com>
Tue, 3 Feb 2015 20:10:26 +0000 (20:10 +0000)
committerTom Hunger <tehunger@gmail.com>
Tue, 3 Feb 2015 20:12:30 +0000 (20:12 +0000)
This is in preparation of changing the personality which takes a list
of images.

sirius/coding/encoders.py

index 351c47b0b16127bd92859b7398796984d581ef0b..d88847cbfb6f2a7e4c954ec37d40914a266c7dfc 100644 (file)
@@ -13,25 +13,12 @@ __all__ = [
 ]
 
 
-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
@@ -49,11 +36,41 @@ def _encode_printer_message(command, pixel_count, rle_image, print_id):
     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.
 
@@ -85,19 +102,31 @@ def encode_bridge_command(bridge_address, command, command_id, timestamp):
         })
 
     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',
         })