working multitasking

This commit is contained in:
Yannik Enss
2020-02-04 19:40:28 +01:00
parent d4b935c60f
commit 8ff465aa04

View File

@@ -31,10 +31,12 @@ args = parser.parse_args()
class DrawThread(threading.Thread): class DrawThread(threading.Thread):
def run(self): def run(self):
xfrom = self.args["xfrom"] xfrom = self._kwargs["xfrom"]
yfrom = self.args["yfrom"] yfrom = self._kwargs["yfrom"]
xto = self.args["xto"] xto = self._kwargs["xto"]
yto = self.args["yto"] yto = self._kwargs["yto"]
print(xfrom, xto, yfrom, yto)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((args.host, args.port)) s.connect((args.host, args.port))
@@ -52,50 +54,43 @@ class DrawThread(threading.Thread):
s.close() s.close()
def draw_dumb():
for x in range(sizex):
for y in range(sizey):
value = "%02X%02X%02X%02X"%framebuffer.getpixel((x,y))
wf.write("PX {} {} {}\n".format(x,y+args.yoffset,value))
while args.repeat:
for x in range(sizex):
for y in range(sizey):
value = "%02X%02X%02X%02X"%framebuffer.getpixel((x,y))
wf.write("PX {} {} {}\n".format(x,y+args.yoffset,value))
def draw(): def draw():
buf = bytearray() chunkxsize = effectivex//XSPLIT
global framebuffer, old_framebuffer chunkysize = effectivey//YSPLIT
if framebuffer == old_framebuffer:
# nothing to draw remainderx = effectivex % XSPLIT
return remaindery = effectivey % YSPLIT
for x in range(effectivex):
for y in range(effectivey): xchunks = []
# if framebuffer.getpixel((x,y))[3] != 0 and framebuffer.getpixel((x,y)) != old_framebuffer.getpixel((x,y)): ychunks = []
value = b"%02X%02X%02X%02X"%framebuffer.getpixel((x,y)) remainingx = effectivex
buf += b"PX %b %b %b\n"%(bytes(str(x), "ascii"),bytes(str(y+args.yoffset), "ascii"),value) remainingy = effectivey
s.send(buf)
while args.repeat: while remainingx > remainderx:
s.send(buf) start = effectivex - remainingx
old_framebuffer = framebuffer remainingx -= chunkxsize
stop = effectivex - remainingx
xchunks.append((start, stop))
xchunks.append((remainingx-effectivex, effectivex))
while remainingy > remaindery:
start = effectivey - remainingy
remainingy -= chunkysize
stop = effectivey - remainingy
ychunks.append((start, stop))
ychunks.append((remainingy-effectivey, effectivey))
for xchunk in xchunks:
for ychunk in ychunks:
DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]}).start()
if args.color: if args.color:
framebuffer.paste(ImageColor.getrgb(args.color), (0,0,sizex,sizey)) framebuffer.paste(ImageColor.getrgb(args.color), (0,0,sizex,sizey))
if args.dumb: draw()
draw_dumb()
else:
draw()
elif args.image: elif args.image:
image = Image.open(str(args.image)) image = Image.open(str(args.image))
framebuffer.paste(image) framebuffer.paste(image)
effectivex = min(image.size[0], sizex) effectivex = min(image.size[0], sizex)
effectivey = min(image.size[1], sizey) effectivey = min(image.size[1], sizey)
if args.dumb: draw()
draw_dumb()
else:
draw()
wf.close()
s.close()