-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
102 lines (76 loc) · 2.34 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import asyncio
import sys
from aiohttp import web
import pygame
from pygame.locals import QUIT
DEBUG = True
IMAGE = pygame.image.load('assets/closed.png')
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
SURFACE = None
async def display_image(app):
try:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
SURFACE.blit(
source=IMAGE,
dest=(
(SCREEN_WIDTH - IMAGE.get_rect().w)/2,
(SCREEN_HEIGHT - IMAGE.get_rect().h)/2,
),
)
pygame.display.flip()
await asyncio.sleep(0.1)
except asyncio.CancelledError:
pass
finally:
pass # cleanup
async def start_background_tasks(app):
app['display_image'] = app.loop.create_task(display_image(app))
async def cleanup_background_tasks(app):
app['display_image'].cancel()
await app['display_image']
async def serve_open(request):
global IMAGE
IMAGE = pygame.image.load('assets/open.png')
return web.Response()
async def process_next(request):
global IMAGE
PATTERN = [2, 1, 2, 1] # 'c' in morse
PAUSE = 0.3
image_open = pygame.image.load('assets/open.png')
image_inv = pygame.image.load('assets/open-inv.png')
IMAGE = image_open
await asyncio.sleep(PAUSE)
for part in PATTERN:
IMAGE = image_inv
await asyncio.sleep(part * PAUSE)
IMAGE = image_open
await asyncio.sleep(PAUSE)
async def serve_next(request):
asyncio.ensure_future(process_next(request))
return web.Response()
async def serve_close(request):
global IMAGE
IMAGE = pygame.image.load('assets/closed.png')
return web.Response()
def main():
global SURFACE
pygame.init()
SURFACE = pygame.display.set_mode(
(SCREEN_WIDTH, SCREEN_HEIGHT), # resolution=,
not pygame.FULLSCREEN if DEBUG else pygame.FULLSCREEN, # flags=
8, # depth=
)
app = web.Application()
app.router.add_post('/open', serve_open)
app.router.add_post('/next', serve_next)
app.router.add_post('/close', serve_close)
app.on_startup.append(start_background_tasks)
app.on_cleanup.append(cleanup_background_tasks)
web.run_app(app)
if __name__ == '__main__':
main()