#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import _thread import socket import pygame import math import random import numpy as np from pygame.locals import * import time WIDTH=160 HEIGHT=24*6 PIXELSIZE=4 #For Simulator Display UDPPORT=2323 DEBUG=True BITSPERPIXEL=2 class FlipdotSim(): def __init__(self, imageSize = (160,48), #80,16 pixelSize = 10, udpPort = 2323, bitsperpixel=1): self.imageSize=imageSize self.udpPort = udpPort self.bitsperpixel=bitsperpixel self.flipdotMatrixSimulatorWidget = FlipdotMatrixSimulatorWidget(imageSize, pixelSize, bitsperpixel) #comment out if no simulator needed self.udpHostSocket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self.udpHostSocket.bind(("", self.udpPort)) self.time_receivedpacket=0 self.bitsneeded=self.imageSize[0]*self.imageSize[1]*self.bitsperpixel def run(self): self.RunServer() def RunServer(self): try: while True: rawData = self.udpHostSocket.recv(4096*2) _fps=1/(time.time()-self.time_receivedpacket) self.time_receivedpacket=time.time() self.flipdotMatrixSimulatorWidget.showFromRawData(rawData) #send to simulator display calctime=time.time()-self.time_receivedpacket print(str(round(calctime,4))+"s, maxFPS="+str(round(1/calctime,2))+", actual FPS="+str(round(_fps,2))) #calculate time it took for calculation and drawing finally: self.udpHostSocket.close() class FlipdotMatrixSimulatorWidget(): BLACKCOLOR = 0 WHITECOLOR = 1 def __init__(self, imageSize = (160,48), pixelSize = 4, bitsperpixel = 1): self.imageSize = imageSize self.pixelSize = pixelSize pygame.init() self.screen = pygame.display.set_mode((imageSize[0]*pixelSize, imageSize[1]*pixelSize)) self.screen.fill((0,0,0)) self.pixelsurface=None _thread.start_new_thread(self.watchCloseThread, ()) self.bitsperpixel=bitsperpixel minimumbrightness=32 self.colorTable=[] for i in range(pow(2,self.bitsperpixel)): brightness=i/pow(2,self.bitsperpixel) self.colorTable.append( ( int( (255-minimumbrightness)*brightness+minimumbrightness) , int(127*brightness+ minimumbrightness/2), int(minimumbrightness/4) ) ) def watchCloseThread(self): while True: for event in pygame.event.get(): if event.type in (QUIT, QUIT): import os os.kill(os.getpid(), 9) pygame.time.delay(500) def showFromRawData(self, rawData): x=0 #pixel x position y=0 #pixel y position bitshifts=[x*self.bitsperpixel for x in range(int(8/self.bitsperpixel))] bitmask=int('00000011', 2) for cbyte in rawData: for numbershifts in bitshifts: pixelbyte = ( cbyte & (bitmask<> numbershifts pygame.draw.circle(self.screen, self.colorTable[pixelbyte], (int(x*self.pixelSize+self.pixelSize/2), int(y*self.pixelSize+self.pixelSize/2)), int(self.pixelSize/2)) #Draw LED as filled circles x+=1 #next pixel y+=int(x/self.imageSize[0]) #next column if end of row x%=self.imageSize[0] #start at first column if end of row pygame.display.update() if __name__ == '__main__': FlipdotSim(imageSize=(WIDTH,HEIGHT), pixelSize = PIXELSIZE, udpPort=UDPPORT, bitsperpixel=BITSPERPIXEL).run()