diff lcd_simulator.py @ 5:d97a5930db7e

closer
author drewp@bigasterisk.com
date Wed, 06 Mar 2024 16:38:58 -0800
parents e273cc60b389
children 47795c3121f1
line wrap: on
line diff
--- a/lcd_simulator.py	Tue Mar 05 18:12:15 2024 -0800
+++ b/lcd_simulator.py	Wed Mar 06 16:38:58 2024 -0800
@@ -1,40 +1,38 @@
 import asyncio
-import pygame
-import aiomqtt
 import struct
 
-WIDTH = 320
-HEIGHT = 320
+import aiomqtt
+import pygame
 
-pygame.init()
-screen = pygame.display.set_mode((WIDTH, HEIGHT))
+screen = pygame.display.set_mode((320, 320))
 clock = pygame.time.Clock()
 
 
-async def on_message(client, userdata, message):
-    payload = bytes(message.payload)
-    x, y, w, h = struct.unpack("HHHH", payload[:8])
-    buf = payload[8:]
-    for dy in range(h):
-        for dx in range(w):
-            off = w * dy + dx
-            r, g, b = buf[off * 3 : off * 3 + 3]
-            screen.set_at((x + dx, y + dy), (r, g, b))
+async def on_message(client):
+    await client.subscribe('display/squib/updates')
+    async for message in client.messages:
+        payload = bytes(message.payload)
+        head, x, y, w, h = struct.unpack("HHHHH", payload[:10])
+        buf = payload[10:]
+        pygame.draw.rect(screen, (255, 255, 0), (x, y, w, h))
+        pygame.display.flip()
+        await asyncio.sleep(.02)
+        for dy in range(h):
+            for dx in range(w):
+                off = w * dy + dx
+                r, g, b = buf[off * 3:off * 3 + 3]
+                screen.set_at((x + dx, y + dy), (r, g, b))
+        pygame.display.flip()
 
 
 async def main():
     async with aiomqtt.Client("mqtt2.bigasterisk.com") as client:
-        client.on_message = on_message
-        await client.subscribe('display/squib/updates')
+        asyncio.create_task(on_message(client))
 
         while True:
             for event in pygame.event.get():
-                if event.type == pygame.QUIT:
-                    raise SystemExit
-
-            await client.loop_read()
-            pygame.display.flip()
-            clock.tick(60)
+                pass
+            await asyncio.sleep(1 / 30)
 
 
 asyncio.run(main())