comparison web_to_mqtt.py @ 8:47795c3121f1

bufferless updates! mqtt message is sent over SPI and everything works
author drewp@bigasterisk.com
date Mon, 11 Mar 2024 01:37:57 -0700
parents b46679798c51
children affb3c8f3f58
comparison
equal deleted inserted replaced
7:b46679798c51 8:47795c3121f1
18 async def capture_screenshot(self, url) -> Image.Image: 18 async def capture_screenshot(self, url) -> Image.Image:
19 out = tempfile.NamedTemporaryFile(suffix=".png", prefix='webrenderer_') 19 out = tempfile.NamedTemporaryFile(suffix=".png", prefix='webrenderer_')
20 screenshot_command = [ 20 screenshot_command = [
21 "google-chrome", 21 "google-chrome",
22 "--headless", 22 "--headless",
23 "--window-size=320,320", 23 "--window-size=320,480",
24 f"--screenshot={out.name}", 24 f"--screenshot={out.name}",
25 url, 25 url,
26 ] 26 ]
27 subprocess.run(screenshot_command, 27 subprocess.run(screenshot_command,
28 stdout=subprocess.DEVNULL, 28 stdout=subprocess.DEVNULL,
30 return Image.open(out.name).convert('RGB') 30 return Image.open(out.name).convert('RGB')
31 31
32 32
33 blockX = 32 33 blockX = 32
34 blockY = 32 34 blockY = 32
35 msgDelay = .12 35 msgDelay = .05
36 dirtyQueue = {} 36 dirtyQueue = {}
37 37
38 38
39 async def check_for_changes(renderer, client, last_image): 39 async def check_for_changes(renderer, client, last_image):
40 current_image = await renderer.capture_screenshot( 40 current_image = await renderer.capture_screenshot(
53 53
54 async def sendDirty(client): 54 async def sendDirty(client):
55 while True: 55 while True:
56 if dirtyQueue: 56 if dirtyQueue:
57 # pos = random.choice(list(dirtyQueue.keys())) 57 # pos = random.choice(list(dirtyQueue.keys()))
58 # pos = min(list(dirtyQueue.keys())) 58 pos = min(list(dirtyQueue.keys()))
59 pos = random.choice(list(dirtyQueue.keys()))
60 img = dirtyQueue.pop(pos) 59 img = dirtyQueue.pop(pos)
61 await tell_lcd(client, pos[0], pos[1], img) 60 await tell_lcd(client, pos[0], pos[1], img)
62 await asyncio.sleep(msgDelay) # too fast and esp restarts 61 await asyncio.sleep(msgDelay) # too fast and esp restarts
63 62
64 63
80 # also listen for dirty msgs from the web page; see ts. 79 # also listen for dirty msgs from the web page; see ts.
81 # also get notified of new mqtt listeners who need a full image refresh. 80 # also get notified of new mqtt listeners who need a full image refresh.
82 renderer = WebRenderer() 81 renderer = WebRenderer()
83 async with aiomqtt.Client("mqtt2") as client: 82 async with aiomqtt.Client("mqtt2") as client:
84 asyncio.create_task(sendDirty(client)) 83 asyncio.create_task(sendDirty(client))
85 last_image = Image.new('RGB', (320, 320)) 84 last_image = Image.new('RGB', (320, 480))
86 while True: 85 while True:
87 last_image = await check_for_changes(renderer, client, last_image) 86 last_image = await check_for_changes(renderer, client, last_image)
88 # we could get the web page to tell us when any dom changes 87 # we could get the web page to tell us when any dom changes
89 await asyncio.sleep(5) 88 await asyncio.sleep(1)
90 89
91 90
92 if __name__ == "__main__": 91 if __name__ == "__main__":
93 asyncio.run(main()) 92 asyncio.run(main())