improve sender class and cleanup code. change bitorder
This commit is contained in:
parent
a953156059
commit
9519e9b876
|
@ -18,7 +18,6 @@ class MatrixSender(object):
|
|||
global threadrunning
|
||||
threadrunning=False
|
||||
|
||||
|
||||
def __init__(self, udphost, udpport, img_size=(160,48), bitsperpixel=1):
|
||||
|
||||
self._udphost = udphost
|
||||
|
@ -31,58 +30,21 @@ class MatrixSender(object):
|
|||
|
||||
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
|
||||
|
||||
def _list2byte(self, l):
|
||||
byte = 0
|
||||
i = 0
|
||||
for i in range(8):
|
||||
byte += 2**(7-i) if l[i] else 0
|
||||
return byte
|
||||
|
||||
def _array2packet(self, a):
|
||||
return [self._list2byte(a[i*8:i*8+8]) for i in range(int(len(a)/8))]
|
||||
|
||||
def _intToBitlist(self,number): #convert integer number to list of bits, exampe: 1-> [0,1], 2->[1,0]
|
||||
bitlistvaryinglength=[x for x in "{0:b}".format(number)]
|
||||
bitlist=np.zeros(self.bitsperpixel,dtype=int)
|
||||
bitlist[self.bitsperpixel-len(bitlistvaryinglength):]=bitlistvaryinglength
|
||||
return bitlist
|
||||
|
||||
|
||||
def send(self, image,invert=False): #changes slowly 'fadespeed'-pixels at a time
|
||||
def send(self, image,invert=False):
|
||||
global threadrunning
|
||||
imgmap = []
|
||||
sendbyte=0
|
||||
senddata=bytearray()
|
||||
pixelofbyte=0
|
||||
for pixel in image.getdata():
|
||||
r, g, b, a = pixel
|
||||
|
||||
pixelbrightness=int( (r+g+b)/3 *(pow(2,self.bitsperpixel)-1) /255 +0.5)
|
||||
sendbyte+=pixelbrightness<<(pixelofbyte*self.bitsperpixel)
|
||||
pixelofbyte+=1
|
||||
if pixelofbyte>=(8/self.bitsperpixel):
|
||||
pixelofbyte=0
|
||||
senddata.append(sendbyte)
|
||||
sendbyte=0
|
||||
|
||||
if invert:
|
||||
pixelbrightness=pow(2,self.bitsperpixel)-1-pixelbrightness
|
||||
|
||||
for b in self._intToBitlist(pixelbrightness):
|
||||
imgmap.append(b)
|
||||
|
||||
self.sendPacket(imgmap) #send packet and save last-imagemap
|
||||
|
||||
|
||||
def sendPacket(self, imgmap):
|
||||
packet = self._array2packet(imgmap)
|
||||
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))
|
||||
|
||||
|
||||
def send_bytes(self, img):
|
||||
imgmap = []
|
||||
for pixel in img:
|
||||
if pixel == "1":
|
||||
imgmap.append(1)
|
||||
else:
|
||||
imgmap.append(0)
|
||||
|
||||
if len(img) < 1280:
|
||||
imgmap = np.hstack((imgmap, np.zeros(1280-len(img), dtype=int)))
|
||||
|
||||
packet = self._array2packet(imgmap)
|
||||
|
||||
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))
|
||||
self._sock.sendto(bytes(senddata), (self._udphost, self._udpport))
|
||||
|
|
|
@ -94,7 +94,7 @@ class FlipdotMatrixSimulatorWidget():
|
|||
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)))]
|
||||
bitshifts=[x*self.bitsperpixel for x in range(int(8/self.bitsperpixel))]
|
||||
bitmask=int('00000011', 2)
|
||||
|
||||
for cbyte in rawData:
|
||||
|
|
|
@ -34,7 +34,10 @@ if __name__ == '__main__':
|
|||
#draw ball
|
||||
draw.ellipse((ball_pos[0]-ball_size/2,ball_pos[1]-ball_size/2,ball_pos[0]+ball_size/2,ball_pos[1]+ball_size/2,),fill=(255,255,255))
|
||||
|
||||
|
||||
_time_startsending=time.time()
|
||||
matrix.send(im) #construct udp packet and send to matrix
|
||||
_time_endsending=time.time()
|
||||
|
||||
print("Sendtime="+str(round(_time_endsending-_time_startsending,4))+"s, FPS="+str(round(1/(_time_endsending-_time_startsending),2)))
|
||||
|
||||
time.sleep(.1) #wait
|
||||
|
|
Loading…
Reference in New Issue