Repository: briggySmalls/pyflipdot

Issue:
The script cannot support board like 128 * 32
the image will be too large, see below error  

File "/usr/local/lib/python3.6/dist-packages/pyflipdot/data.py", line 23, in _to_ascii_hex
    return _bytes_to_ascii_hex(value)
  File "/usr/local/lib/python3.6/dist-packages/pyflipdot/data.py", line 20, in _bytes_to_ascii_hex
    return val.hex().upper().encode('ASCII')

Comments:

Patch:
diff --git a/pyflipdot/data.py b/pyflipdot/data.py
index b26b05f..2326941 100644
--- a/pyflipdot/data.py
+++ b/pyflipdot/data.py
@@ -16,13 +16,7 @@ _COMMAND_CODES = {
 
 
 def _to_ascii_hex(value: bytes) -> bytes:
-    def _bytes_to_ascii_hex(val: bytes) -> bytes:
-        return val.hex().upper().encode('ASCII')
-
-    try:
-        return _bytes_to_ascii_hex(value)
-    except AttributeError:
-        return _bytes_to_ascii_hex(bytes([value]))
+    return value.hex().upper().encode('ASCII')
 
 
 def _bytes_to_int(data: bytes) -> int:
@@ -125,7 +119,11 @@ class ImagePacket(Packet):
         image_bytes = self.image_to_bytes(image)
 
         # Start with the resolution (image byte count)
-        payload = _to_ascii_hex(len(image_bytes))
+        # Note: we only ever send a single bytes-worth of info, even if the
+        # resolution is an integer bigger than 255
+        resolution_bytes = (len(image_bytes) & 0xFF).to_bytes(
+            1, byteorder='big')
+        payload = _to_ascii_hex(resolution_bytes)
         # Add the image bytes
         payload += _to_ascii_hex(image_bytes)
 


Test patch:
diff --git a/tests/test_data.py b/tests/test_data.py
index 011a49f..710d4ae 100644
--- a/tests/test_data.py
+++ b/tests/test_data.py
@@ -5,54 +5,68 @@ import numpy as np
 from pyflipdot.data import ImagePacket, Packet
 
 
-class TestPackets(object):
-    def test_no_payload(self):
-        packet = Packet(1, 2)
-        packet_data = packet.get_bytes()
-
-        assert packet_data == b'\x0212\x039A'
-
-    def test_with_payload(self):
-        payload = b'345'
-        packet = Packet(1, 2, payload)
-        packet_data = packet.get_bytes()
-
-        assert packet_data == b'\x0212345\x03FE'
-
-    def test_image(self):
-        # Send an image as below ('p' indicates byte alignment padding)
-        # (0) | 1, 0 |
-        # (1) | 0, 0 | -> [0x01, 0x00]
-        # (2) | 0, 0 |
-        # (3) | 0, 0 |
-        image = np.full((3, 2), False)
-        image[0, 0] = True
-
-        packet = ImagePacket(1, image)
-        packet_data = packet.get_bytes()
-        assert packet_data == b'\x0211020100\x0378'
-
-    def test_tall_image(self):
-        # Send an image as below ('p' indicates byte alignment padding)
-        # (0)  | 1, 0 |
-        # (1)  | 0, 0 |
-        # (2)  | 0, 0 |
-        # (3)  | 0, 0 |
-        # (4)  | 0, 0 |
-        # (5)  | 0, 0 |
-        # (6)  | 0, 0 |
-        # (7)  | 0, 0 | -> | 0x01, 0x00 | -> [0x01, 0x02, 0x00, 0x00]
-        # (8)  | 0, 0 |    | 0x02, 0x00 |
-        # (9)  | 1, 0 |
-        # (10) | 0, 0 |
-        # (11) | 0, 0 |
-        # (12) | 0, 0 |
-        # (13) | 0, 0 |
-        # (14) | 0, 0 |
-        image = np.full((15, 2), False)
-        image[0, 0] = True
-        image[9, 0] = True
-
-        packet = ImagePacket(1, image)
-        packet_data = packet.get_bytes()
-        assert packet_data == b'\x02110401020000\x03B4'
+def test_no_payload():
+    packet = Packet(1, 2)
+    packet_data = packet.get_bytes()
+
+    assert packet_data == b'\x0212\x039A'
+
+
+def test_with_payload():
+    payload = b'345'
+    packet = Packet(1, 2, payload)
+    packet_data = packet.get_bytes()
+
+    assert packet_data == b'\x0212345\x03FE'
+
+
+def test_simple_image():
+    # Send an image as below ('p' indicates byte alignment padding)
+    # (0) | 1, 0 |
+    # (1) | 0, 0 | -> [0x01, 0x00]
+    # (2) | 0, 0 |
+    # (3) | 0, 0 |
+    image = np.full((3, 2), False)
+    image[0, 0] = True
+
+    packet = ImagePacket(1, image)
+    packet_data = packet.get_bytes()
+    assert packet_data == b'\x0211020100\x0378'
+
+
+def test_tall_image():
+    # Send an image as below ('p' indicates byte alignment padding)
+    # (0)  | 1, 0 |
+    # (1)  | 0, 0 |
+    # (2)  | 0, 0 |
+    # (3)  | 0, 0 |
+    # (4)  | 0, 0 |
+    # (5)  | 0, 0 |
+    # (6)  | 0, 0 |
+    # (7)  | 0, 0 | -> | 0x01, 0x00 | -> [0x01, 0x02, 0x00, 0x00]
+    # (8)  | 0, 0 |    | 0x02, 0x00 |
+    # (9)  | 1, 0 |
+    # (10) | 0, 0 |
+    # (11) | 0, 0 |
+    # (12) | 0, 0 |
+    # (13) | 0, 0 |
+    # (14) | 0, 0 |
+    image = np.full((15, 2), False)
+    image[0, 0] = True
+    image[9, 0] = True
+
+    packet = ImagePacket(1, image)
+    packet_data = packet.get_bytes()
+    assert packet_data == b'\x02110401020000\x03B4'
+
+
+def test_large_image():
+    # Create an image that is 128x32 pixels
+    image = np.full((16, 128), True)
+
+    packet = ImagePacket(1, image)
+    packet_data = packet.get_bytes()
+    assert packet_data[:5] == b'\x021100'
+    for val in packet_data[7:-3]:
+        assert val.to_bytes(1, byteorder='big') == b'F'
+    assert packet_data[-3:] == b'\x033B'