From 9519e9b876eac7402f93bc54541e0443802320af Mon Sep 17 00:00:00 2001 From: Fisch Date: Sat, 13 Jan 2018 19:11:17 +0100 Subject: [PATCH] improve sender class and cleanup code. change bitorder --- client_sw/matrixSender.py | 60 ++++++--------------------------- client_sw/matrixsimulator.py | 2 +- client_sw/movingball_example.py | 5 ++- 3 files changed, 16 insertions(+), 51 deletions(-) diff --git a/client_sw/matrixSender.py b/client_sw/matrixSender.py index 8ad4689..d355440 100644 --- a/client_sw/matrixSender.py +++ b/client_sw/matrixSender.py @@ -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)) diff --git a/client_sw/matrixsimulator.py b/client_sw/matrixsimulator.py index 0d22179..ceace7f 100644 --- a/client_sw/matrixsimulator.py +++ b/client_sw/matrixsimulator.py @@ -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: diff --git a/client_sw/movingball_example.py b/client_sw/movingball_example.py index ed2bff4..d5d084d 100644 --- a/client_sw/movingball_example.py +++ b/client_sw/movingball_example.py @@ -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