comparison lcd_simulator.py @ 4:e273cc60b389

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