]> git.aero2k.de Git - sirius.git/commitdiff
this is the first version that prints properly - although upside-down - to my test...
authorMatt Webb <matt@interconnected.org>
Mon, 22 Sep 2014 18:41:03 +0000 (19:41 +0100)
committerMatt Webb <matt@interconnected.org>
Mon, 22 Sep 2014 18:41:03 +0000 (19:41 +0100)
app/__init__.py
app/commands.py
app/image_encoding.py
tests/barcode-head.png [new file with mode: 0644]

index e8004231db1b519b8b83a5f0f23b5805b542afb1..70b996d220678190f4e9500c6de03f911aad03f2 100755 (executable)
@@ -19,7 +19,9 @@ from . import commands
 # for this to work, I have to put the following line at line 38 of
 # flask_sockets, to inject it into the environment
 # environment.environ['HTTP_SEC_WEBSOCKET_PROTOCOL'] = 'bergcloud-bridge-v1'
-
+def protocol_name():
+    print "asked for protocol"
+    return 'bergcloud-bridge-v1'
 
 def create_app(config_name):
     app = Flask(__name__)
@@ -29,7 +31,10 @@ def create_app(config_name):
     bootstrap.init_app(app)
     db.init_app(app)
     sockets.init_app(app)
-    
+
+    # maybe this sets the HTTP_SEC_WEBSOCKET_PROTOCOL ?
+    app.protocol_name = protocol_name
+
     # attach routes and custom error pages
     from .main import main as main_blueprint
     app.register_blueprint(main_blueprint)
@@ -58,7 +63,7 @@ def create_app(config_name):
                     #ws.send(j)
                 if event['type'] == 'DeviceEvent':
                     counting += 1
-                    if counting == 10:
+                    if counting == 4:
                         gevent.sleep(0.5)
                         j = commands.set_delivery_and_print().to_json()
                         ws.send(j)
index fc3cbb004ccdd474d3c3708505c855e5bb5943b1..8e14ded557faceaaa6a4199bdbfa4a51b6de4f60 100644 (file)
@@ -19,7 +19,8 @@ import json
 # firmware_update
 
 TEST_FILE_ID = 1
-TEST_PNG_FN = '/Users/matt/Documents/dev-unversioned/sirius2/iconrethink.png'
+#TEST_PNG_FN = '/Users/matt/Documents/dev-unversioned/sirius2/iconrethink.png'
+TEST_PNG_FN = '/Users/matt/Documents/dev/sirius/tests/iconrethink.png'
 
 # some constants
 DEVICE_TYPE = '\x01' # rBergCloud::Barringer::DEVICE_TYPE in weminuche-server
@@ -70,7 +71,7 @@ def set_delivery_and_print_payload(file_id, png_fn):
     # device type c, reserved byte c,
     # command_name <H (short), file_id <L (long),
     # unimplemented CRC (long is zero)
-    command_header = struct.pack("<BBHLL", 1, 0, 1, 1, 0)
+    command_header = struct.pack("<BBHII", 1, 0, 1, 123, 0)
 
     # get the encoded image now, because we'll need the data later
     pixel_count, encoded_image = rle_image(png_fn)
@@ -88,14 +89,14 @@ def set_delivery_and_print_payload(file_id, png_fn):
     n2 = n3_remainder / 256
     n1 = n3_remainder % 256
     printer_data = struct.pack("<8B", 0x1b, 0x2a, n1, n2, n3, 0, 0, 48)
-    header_region = struct.pack("<BL", 0, len(printer_control) + len(printer_data))
+    header_region = struct.pack("<BI", 0, len(printer_control) + len(printer_data))
     header_region += printer_control + printer_data
     
     # payload including the header
-    payload = struct.pack("<LB", len(header_region) + len(encoded_image) + 1, 0)
+    payload = struct.pack("<IB", len(header_region) + len(encoded_image) + 1, 0)
     payload += header_region + encoded_image
     
-    entire_payload = command_header + struct.pack("<L", len(payload)) + payload
+    entire_payload = command_header + struct.pack("<I", len(payload)) + payload
     
     return entire_payload
     
index da2a0d6d584b622064b40d2a22a86f6df2e4f263..b66c6084a993787fbabfeebd168134ffce88b5a2 100644 (file)
@@ -72,7 +72,36 @@ def rle_image(image_fn):
     # encode using our custom encoding
     lengths = [g[1] for g in groups]
     x = bytearray(rle(lengths))
-    output = struct.pack("<%ds" % len(x), str(x))
+    compressed_data = struct.pack("<%dB" % len(x), *x)
+    # first byte is compressed type, always 1
+    output = struct.pack("<BL", 0x01, len(compressed_data)) + compressed_data
 
     # return (number of pixels, output)
     return len(pixels), output
+
+# ~~~~
+# Test
+# ~~~~
+
+if __name__ == '__main__':
+    # just see whether the output results in the same number of
+    # pixels as the input
+    TEST_PNG_FN = '/Users/matt/Documents/dev-unversioned/sirius2/iconrethink.png'
+    pixel_count, rle_encoded = rle_image(TEST_PNG_FN)
+
+    output_length = 0
+    length_codes = {}
+    for length, code in TRANSLATE:
+        length_codes[code] = length
+    
+    for b in rle_encoded:
+        length = ord(b)
+        if length in length_codes.keys():
+            length = length_codes[length]
+        output_length += length
+    
+    # Good, it matches
+    print "Input length: %d" % pixel_count
+    print "Output length: %d" % output_length
+
+        
\ No newline at end of file
diff --git a/tests/barcode-head.png b/tests/barcode-head.png
new file mode 100644 (file)
index 0000000..a6677a5
Binary files /dev/null and b/tests/barcode-head.png differ