comparison web_to_mqtt.py @ 6:e36abecb48a1

experimenting with block size & delay
author drewp@bigasterisk.com
date Fri, 08 Mar 2024 09:17:05 -0800
parents d97a5930db7e
children b46679798c51
comparison
equal deleted inserted replaced
5:d97a5930db7e 6:e36abecb48a1
28 stdout=subprocess.DEVNULL, 28 stdout=subprocess.DEVNULL,
29 stderr=subprocess.DEVNULL) 29 stderr=subprocess.DEVNULL)
30 return Image.open(out.name).convert('RGB') 30 return Image.open(out.name).convert('RGB')
31 31
32 32
33 block = 320 // 10 33 blockX = 16
34 34 blockY = 64
35 msgDelay = .12
35 dirtyQueue = {} 36 dirtyQueue = {}
36 37
37 38
38 async def check_for_changes(renderer, client, last_image): 39 async def check_for_changes(renderer, client, last_image):
39 current_image = await renderer.capture_screenshot( 40 current_image = await renderer.capture_screenshot(
40 "http://localhost:8002/front-door-display/scheduleLcd.html") 41 "http://localhost:8002/front-door-display/scheduleLcd.html")
41 diff_image = ImageChops.difference(last_image, current_image) 42 diff_image = ImageChops.difference(last_image, current_image)
42 for y in range(0, current_image.height, block): 43 for y in range(0, current_image.height, blockY):
43 for x in range(0, current_image.width, block): 44 for x in range(0, current_image.width, blockX):
44 box = (x, y, x + block, y + block) 45 box = (x, y, x + blockX, y + blockY)
45 region = diff_image.crop(box) 46 region = diff_image.crop(box)
46 if region.getbbox(): 47 if region.getbbox():
47 dirtyQueue[(x, y)] = current_image.crop(box) 48 dirtyQueue[(x, y)] = current_image.crop(box)
48 await asyncio.sleep(0) 49 await asyncio.sleep(0)
49 50
55 if dirtyQueue: 56 if dirtyQueue:
56 # pos = random.choice(list(dirtyQueue.keys())) 57 # pos = random.choice(list(dirtyQueue.keys()))
57 pos = min(list(dirtyQueue.keys())) 58 pos = min(list(dirtyQueue.keys()))
58 img = dirtyQueue.pop(pos) 59 img = dirtyQueue.pop(pos)
59 await tell_lcd(client, pos[0], pos[1], img) 60 await tell_lcd(client, pos[0], pos[1], img)
60 await asyncio.sleep(.15) 61 await asyncio.sleep(msgDelay) # too fast and esp restarts
61 62
62 framesSent = itertools.count() 63 framesSent = itertools.count()
63 64
64 65
65 async def tell_lcd(client: aiomqtt.Client, x: int, y: int, 66 async def tell_lcd(client: aiomqtt.Client, x: int, y: int,
66 region: Image.Image): 67 region: Image.Image):
67 seq = next(framesSent) 68 seq = next(framesSent) % 65535
68 msg = struct.pack('HHHHH', seq, x, y, region.width, region.height) + region.tobytes() 69 msg = struct.pack('HHHHH', seq, x, y, region.width, region.height) + region.tobytes()
69 print(f'send {seq=} {x=} {y=} {region.width=} {region.height=} ', end='\r', flush=True) 70 print(f'send {seq=} {x=} {y=} {region.width=} {region.height=} ', end='\r', flush=True)
70 await client.publish('display/squib/updates', msg, qos=0) 71 await client.publish('display/squib/updates', msg, qos=0)
71 72
72 73