comparison lcd_simulator.py @ 5:d97a5930db7e

closer
author drewp@bigasterisk.com
date Wed, 06 Mar 2024 16:38:58 -0800
parents e273cc60b389
children 47795c3121f1
comparison
equal deleted inserted replaced
4:e273cc60b389 5:d97a5930db7e
1 import asyncio 1 import asyncio
2 import pygame
3 import aiomqtt
4 import struct 2 import struct
5 3
6 WIDTH = 320 4 import aiomqtt
7 HEIGHT = 320 5 import pygame
8 6
9 pygame.init() 7 screen = pygame.display.set_mode((320, 320))
10 screen = pygame.display.set_mode((WIDTH, HEIGHT))
11 clock = pygame.time.Clock() 8 clock = pygame.time.Clock()
12 9
13 10
14 async def on_message(client, userdata, message): 11 async def on_message(client):
15 payload = bytes(message.payload) 12 await client.subscribe('display/squib/updates')
16 x, y, w, h = struct.unpack("HHHH", payload[:8]) 13 async for message in client.messages:
17 buf = payload[8:] 14 payload = bytes(message.payload)
18 for dy in range(h): 15 head, x, y, w, h = struct.unpack("HHHHH", payload[:10])
19 for dx in range(w): 16 buf = payload[10:]
20 off = w * dy + dx 17 pygame.draw.rect(screen, (255, 255, 0), (x, y, w, h))
21 r, g, b = buf[off * 3 : off * 3 + 3] 18 pygame.display.flip()
22 screen.set_at((x + dx, y + dy), (r, g, b)) 19 await asyncio.sleep(.02)
20 for dy in range(h):
21 for dx in range(w):
22 off = w * dy + dx
23 r, g, b = buf[off * 3:off * 3 + 3]
24 screen.set_at((x + dx, y + dy), (r, g, b))
25 pygame.display.flip()
23 26
24 27
25 async def main(): 28 async def main():
26 async with aiomqtt.Client("mqtt2.bigasterisk.com") as client: 29 async with aiomqtt.Client("mqtt2.bigasterisk.com") as client:
27 client.on_message = on_message 30 asyncio.create_task(on_message(client))
28 await client.subscribe('display/squib/updates')
29 31
30 while True: 32 while True:
31 for event in pygame.event.get(): 33 for event in pygame.event.get():
32 if event.type == pygame.QUIT: 34 pass
33 raise SystemExit 35 await asyncio.sleep(1 / 30)
34
35 await client.loop_read()
36 pygame.display.flip()
37 clock.tick(60)
38 36
39 37
40 asyncio.run(main()) 38 asyncio.run(main())