2018-01-11 22:46:46 +00:00
#!/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
2018-01-13 17:20:23 +00:00
2018-01-11 22:46:46 +00:00
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 . timesincelastpacket = time . time ( )
2018-01-13 12:11:20 +00:00
self . timelastcalculation = 0
2018-01-11 22:46:46 +00:00
2018-01-13 17:20:23 +00:00
self . bitsneeded = self . imageSize [ 0 ] * self . imageSize [ 1 ] * self . bitsperpixel
2018-01-11 22:46:46 +00:00
def run ( self ) :
self . RunServer ( )
def RunServer ( self ) :
try :
while True :
rawData = self . udpHostSocket . recv ( 4096 * 2 )
2018-01-13 17:20:23 +00:00
blafoo = time . time ( )
2018-01-11 22:46:46 +00:00
2018-01-13 17:20:23 +00:00
self . flipdotMatrixSimulatorWidget . showFromRawData ( rawData ) #send to simulator display
calctime = time . time ( ) - blafoo
print ( str ( round ( calctime , 4 ) ) + " s, FPS= " + str ( round ( 1 / calctime , 2 ) ) ) #calculate time it took for calculation and drawing
2018-01-13 12:11:20 +00:00
2018-01-11 22:46:46 +00:00
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 ) )
2018-01-13 17:20:23 +00:00
self . screen . fill ( ( 0 , 0 , 0 ) )
self . pixelsurface = None
2018-01-11 22:46:46 +00:00
_thread . start_new_thread ( self . watchCloseThread , ( ) )
self . bitsperpixel = bitsperpixel
2018-01-13 17:20:23 +00:00
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 ) ) )
2018-01-11 22:46:46 +00:00
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 )
2018-01-13 17:20:23 +00:00
def showFromRawData ( self , rawData ) :
x = 0 #pixel x position
y = 0 #pixel y position
bitshifts = [ x * self . bitsperpixel for x in reversed ( range ( int ( 8 / self . bitsperpixel ) ) ) ]
bitmask = int ( ' 00000011 ' , 2 )
2018-01-11 22:46:46 +00:00
2018-01-13 17:20:23 +00:00
for cbyte in rawData :
for numbershifts in bitshifts :
pixelbyte = ( cbyte & ( bitmask << numbershifts ) ) >> numbershifts
2018-01-11 22:46:46 +00:00
2018-01-13 17:20:23 +00:00
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
2018-01-11 22:46:46 +00:00
2018-01-13 17:20:23 +00:00
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
2018-01-11 22:46:46 +00:00
2018-01-13 17:20:23 +00:00
pygame . display . update ( )
2018-01-11 22:46:46 +00:00
if __name__ == ' __main__ ' :
FlipdotSim ( imageSize = ( WIDTH , HEIGHT ) , pixelSize = PIXELSIZE , udpPort = UDPPORT , bitsperpixel = BITSPERPIXEL ) . run ( )