aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--host/matelight/font.py5
-rwxr-xr-xhost/matelight/listeners.py55
-rwxr-xr-xhost/matelight/scroll-test.py14
3 files changed, 56 insertions, 18 deletions
diff --git a/host/matelight/font.py b/host/matelight/font.py
new file mode 100644
index 0000000..a1c1dd6
--- /dev/null
+++ b/host/matelight/font.py
@@ -0,0 +1,5 @@
+from bdflib import reader as bdfreader # Used to read the bitmap font
+
+FONT = bdfreader.read_bdf(iter(open('fonts/unifont-6.3.20131020.bdf').readlines()))
+FONT_HEIGHT = 16
+
diff --git a/host/matelight/listeners.py b/host/matelight/listeners.py
index 91175cf..f390389 100755
--- a/host/matelight/listeners.py
+++ b/host/matelight/listeners.py
@@ -1,11 +1,15 @@
#!/usr/bin/env python3
from socketserver import *
+import socket
import threading
import zlib
+import random
import struct
import host
import numpy as np
import time
+import sys
+import traceback
import renderers
from PIL import Image, ImageSequence
from config import *
@@ -15,28 +19,40 @@ frame = np.array(img.convert('RGB').getdata(), dtype=np.uint8).reshape((DISPLAY_
host.sendframe(frame)
from font import *
+UDP_THRES = 1.0
+
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
-default_renderer = renderers.TextRenderer('\x1B[91mFeed \x1B[92mme via \x1B[93mTCP on \x1B[94;101mml.jaseg.net:1337\x1B[0;91m ! \x1B[95mI can\x1B[96m parse\x1B[97m ANSI\x1B[92m color\x1B[93m codes\x1B[91m!\x1B[38;5;207m http://github.com/jaseg/matelight')
+default_renderers = [renderers.TextRenderer('\x1B[92mMate Light\x1B[93m@\x1B[92mPlay store or \x1B[94;101mtcp://ml.jaseg.net:1337\x1B[0;91m ♥ '),
+ renderers.TextRenderer('\x1B[92mMate Light\x1B[0;91m ♥ \x1B[92mUnicode'),
+ renderers.TextRenderer('\x1B[92mMate Light\x1B[0m powered by \x1B[95mMicrosoft™ \x1B[96mMarquee Manager® Pro')]
global renderer, count
-renderer = default_renderer
+renderer = default_renderers[0]
count = 0
+lastudp = 0
class MateLightUDPHandler(BaseRequestHandler):
def handle(self):
- data = self.request[0].strip()
- if len(data) != FRAME_SIZE: #+4
- raise ValueError('Invalid frame size: Expected {}, got {}'.format(FRAME_SIZE, len(frame))) #+4
- frame = data[:-4]
- #crc1, = struct.unpack('!I', data[-4:])
- crc2 = zlib.crc32(frame),
- if crc1 != crc2:
- raise ValueError('Invalid frame CRC checksum: Expected {}, got {}'.format(crc2, crc1))
- #socket.sendto(b'ACK', self.client_address)
- a = np.array(frame, dtype=np.uint8)
- a.reshape((DISPLAY_HEIGHT, DISPLAY_WIDTH, 3))
- host.sendframe(a)
+ try:
+ global lastudp
+ data = self.request[0].strip()
+ if len(data) != FRAME_SIZE+4:
+ #raise ValueError('Invalid frame size: Expected {}, got {}'.format(FRAME_SIZE+4, len(data)))
+ return
+ frame = data[:-4]
+ crc1, = struct.unpack('!I', data[-4:])
+ crc2, = zlib.crc32(frame, 0),
+ #if crc1 != crc2:
+ # raise ValueError('Invalid frame CRC checksum: Expected {}, got {}'.format(crc2, crc1))
+ #socket.sendto(b'ACK', self.client_address)
+ a = np.array(list(frame), dtype=np.uint8)
+ lastudp = time.time()
+ host.sendframe(a.reshape((DISPLAY_HEIGHT, DISPLAY_WIDTH, 3)))
+ except Exception as e:
+ print('Error receiving UDP frame:', e)
+ ex_type, ex, tb = sys.exc_info()
+ traceback.print_tb(tb)
class MateLightTCPTextHandler(BaseRequestHandler):
def handle(self):
@@ -66,14 +82,17 @@ t.daemon = True
t.start()
while True:
- global renderer, count
+ global renderer, count, lastudp
foo = renderer
if count == 0:
- renderer = default_renderer
+ renderer = random.choice(default_renderers)
else:
count = count - 1
for frame, delay in foo.frames():
#print(list(frame.flatten()))
- host.sendframe(np.swapaxes(frame, 0, 1))
- #time.sleep(delay)
+ now = time.time()
+ if now-lastudp > UDP_THRES:
+ host.sendframe(np.swapaxes(frame, 0, 1))
+ else:
+ time.sleep(0.1)
diff --git a/host/matelight/scroll-test.py b/host/matelight/scroll-test.py
new file mode 100755
index 0000000..f962416
--- /dev/null
+++ b/host/matelight/scroll-test.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python3
+import host
+import numpy as np
+from config import *
+import time
+import renderers
+
+while True:
+ renderer = renderers.TextRenderer('\x1B[91mThe \x1B[92;105mquick\x1B[0m brown \x1B[96;5mfox jumps over\x1B[0m the lazy dog.')
+ for frame, delay in renderer.frames():
+ #print(list(frame.flatten()))
+ host.sendframe(np.swapaxes(frame, 0, 1))
+ #time.sleep(delay)
+