From 03bf7d9d49956fc06744bb1898c492ba35d34367 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 17 Feb 2014 21:26:52 +0100 Subject: UDP streaming now working properly --- host/matelight.py | 7 ++++--- host/server.py | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/host/matelight.py b/host/matelight.py index 7c4f6c4..cf5eff5 100644 --- a/host/matelight.py +++ b/host/matelight.py @@ -10,7 +10,8 @@ CRATES_Y = 4 DISPLAY_WIDTH = CRATES_X*CRATE_WIDTH DISPLAY_HEIGHT = CRATES_Y*CRATE_HEIGHT -FRAME_SIZE = CRATE_WIDTH*CRATE_HEIGHT*3 +CRATE_SIZE = CRATE_WIDTH*CRATE_HEIGHT*3 +FRAME_SIZE = DISPLAY_WIDTH*DISPLAY_HEIGHT dev = usb.core.find(idVendor=0x1cbe, idProduct=0x0003) @@ -25,8 +26,8 @@ def sendframe(framedata): for cx, cy in product(range(CRATES_X), range(CRATES_Y)): datar = framedata[cy*CRATE_HEIGHT:(cy+1)*CRATE_HEIGHT, cx*CRATE_WIDTH:(cx+1)*CRATE_WIDTH, :3] data = datar.flat - if len(data) != FRAME_SIZE: - raise ValueError('Invalid frame data. Expected {} bytes, got {}.'.format(FRAME_SIZE, len(data))) + if len(data) != CRATE_SIZE: + raise ValueError('Invalid frame data. Expected {} bytes, got {}.'.format(CRATE_SIZE, len(data))) # Send framebuffer data dev.write(0x01, bytes([0, cx, cy])+bytes(data)) # Send latch command diff --git a/host/server.py b/host/server.py index ed3ae58..824328b 100755 --- a/host/server.py +++ b/host/server.py @@ -11,7 +11,7 @@ from ctypes import CDLL, POINTER, c_void_p, Structure, c_uint8, c_size_t, cast, import numpy as np -from matelight import sendframe, DISPLAY_WIDTH, DISPLAY_HEIGHT +from matelight import sendframe, DISPLAY_WIDTH, DISPLAY_HEIGHT, FRAME_SIZE UDP_TIMEOUT = 3.0 @@ -49,7 +49,7 @@ def printframe(fb): print('\0337\033[H', end='') print('Rendering frame @{}'.format(time())) bdf.console_render_buffer(fb.ctypes.data_as(POINTER(c_uint8)), w, h) - print('\033[0m\0338', end='') + print('\033[0mCurrently rendering', current_entry.entrytype, 'from', current_entry.remote, ':', current_entry.text, '\0338', end='') printlock.release() def scroll(text): @@ -89,9 +89,14 @@ def log(*args): class MateLightUDPHandler(BaseRequestHandler): def handle(self): try: + # Housekeeping - FIXME: This is *so* the wrong place for this. + for k,v in conns.items(): + if time() - v.timestamp > UDP_TIMEOUT: + del conns[k] + global current_entry, conns data = self.request[0].strip() - if len(data) != FRAME_SIZE+4: + if len(data) != FRAME_SIZE*3+4: #raise ValueError('Invalid frame size: Expected {}, got {}'.format(FRAME_SIZE+4, len(data))) return frame = data[:-4] @@ -103,20 +108,18 @@ class MateLightUDPHandler(BaseRequestHandler): a = np.array(list(frame), dtype=np.uint8) timestamp = time() addr = self.client_address[0] + conn = QueueEntry('udp', addr, timestamp, '') if addr not in conns: - current_entry = QueueEntry('udp', addr, timestamp, '') - conns[addr] = current_entry log('New UDP connection from', addr) - else: - conns[addr].timestamp = timestamp + current_entry = conn + conns[addr] = current_entry if current_entry.entrytype == 'udp' and current_entry.remote == addr: + current_entry = conn frame = a.reshape((DISPLAY_HEIGHT, DISPLAY_WIDTH, 3)) sendframe(frame) - printframe(frame) + printframe(np.pad(frame, ((0,0),(0,0),(0,1)), 'constant', constant_values=(0,0))) except Exception as e: - print('Error receiving UDP frame:', e) - ex_type, ex, tb = sys.exc_info() - traceback.print_tb(tb) + log('Error receiving UDP frame:', e) class MateLightTCPTextHandler(BaseRequestHandler): def handle(self): @@ -154,7 +157,7 @@ if __name__ == '__main__': current_entry = textqueue[0] else: if conns: - current_entry = random.choice(conns.values()) + current_entry = random.choice(list(conns.values())) else: current_entry = random.choice(defaulttexts) if current_entry.entrytype != 'udp' and textqueue: -- cgit