From ba22224a22858eca7146e4ffd5dda019c326cc6f Mon Sep 17 00:00:00 2001 From: Lucas Magasweran Date: Sat, 13 Nov 2021 14:58:31 +0100 Subject: [PATCH 1/3] emulator: convert from Python 2 to 3 using 2to3 --- emulator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/emulator.py b/emulator.py index 6050ec4..7644735 100755 --- a/emulator.py +++ b/emulator.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @@ -69,7 +69,7 @@ def recv_data(self): Grab the next frame and put it on the matrix. """ data, addr = self.sock.recvfrom(self.packetsize) - matrix = map(ord, data.strip()) + matrix = list(map(ord, data.strip())) if len(matrix) == self.packetsize: self.matrix = matrix[:-4] From 2e566e6886bfb12a67c3b7f7133f7282aace670f Mon Sep 17 00:00:00 2001 From: Lucas Magasweran Date: Sat, 13 Nov 2021 20:57:53 +0100 Subject: [PATCH 2/3] emulator: drop CRC if present and fix the out of bounds issue --- emulator.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/emulator.py b/emulator.py index 7644735..7f5e43d 100755 --- a/emulator.py +++ b/emulator.py @@ -69,9 +69,8 @@ def recv_data(self): Grab the next frame and put it on the matrix. """ data, addr = self.sock.recvfrom(self.packetsize) - matrix = list(map(ord, data.strip())) - if len(matrix) == self.packetsize: - self.matrix = matrix[:-4] + self.matrix = list(data.strip())[:self.packetsize-4] + def update(self): """ @@ -81,8 +80,7 @@ def update(self): for x in range(self.width): for y in range(self.height): pixel = y * self.width * 3 + x * 3 - #TODO: sometimes the matrix is not as big as it should - if pixel < pixels: + if pixel < pixels - 2: pygame.draw.circle(self.screen, (self.matrix[pixel], self.matrix[pixel + 1], self.matrix[pixel + 2]), (x * self.dotsize + self.dotsize / 2, y * self.dotsize + self.dotsize / 2), self.dotsize / 2, 0) From f1138c802f8521998c974eae704127c364d1160e Mon Sep 17 00:00:00 2001 From: Lucas Magasweran Date: Sat, 13 Nov 2021 21:28:44 +0100 Subject: [PATCH 3/3] emulator: make UDP nonblocking and add a refresh rate (60 Hz) to reduce CPU usage --- emulator.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/emulator.py b/emulator.py index 7f5e43d..5973e0b 100755 --- a/emulator.py +++ b/emulator.py @@ -32,6 +32,7 @@ __email__ = 'email@ricardo.band' __status__ = 'Development' +import select import sys import socket @@ -60,6 +61,7 @@ def __init__(self, width=40, height=16, ip='127.0.0.1', port=1337, dotsize=10): self.matrix.append(0) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.setblocking(0) self.sock.bind((ip, port)) # size is width * height * 3 (rgb) + 4 (checksum) self.packetsize = self.width * self.height * 3 + 4 @@ -68,8 +70,13 @@ def recv_data(self): """ Grab the next frame and put it on the matrix. """ - data, addr = self.sock.recvfrom(self.packetsize) - self.matrix = list(data.strip())[:self.packetsize-4] + select.select([], [self.sock], []) + try: + data, addr = self.sock.recvfrom(self.packetsize) + except socket.error: + pass + else: + self.matrix = list(data.strip())[:self.packetsize-4] def update(self): @@ -91,6 +98,7 @@ def render(self): """ pygame.display.update() pygame.display.flip() + self.clock.tick(60) def gameloop(self): """