-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcosmic-paste.py
111 lines (87 loc) · 3 KB
/
cosmic-paste.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
103
104
105
106
107
108
109
110
111
#
# cosmic paste
#
# - FEATURE fix brigtness to change on the fly
# - HOUSEKEEPING rename emoji_paint.py to cosmic-paste.py (and build folders, and releases)
# - HOUSEKEEPING readme remove requirement network_manager.py, its not needed
# - OPTIMIZE do not send rgba to pico, send rgb only (do rgba->rgb in the browser)
#
import os
from microdot_asyncio import Microdot, Request, Response, send_file
from phew import connect_to_wifi
from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
from WIFI_CONFIG import SSID, PSK
cu = CosmicUnicorn()
graphics = PicoGraphics(DISPLAY)
mv_graphics = memoryview(graphics)
cu.set_brightness(0.5)
WIDTH, HEIGHT = graphics.get_bounds()
ip = connect_to_wifi(SSID, PSK)
print(f"Start painting at: http://{ip}")
last_pixels = ""
server = Microdot()
@server.route("/", methods=["GET"])
def route_index(request):
return send_file("cosmic-paste/cosmic-paste.html")
@server.route("/config-unicorns.json", methods=["GET"])
def route_index(request):
return send_file("cosmic-paste/config-unicorns.json")
# @server.route("/static/<path:path>", methods=["GET"])
# def route_static(request, path):
# return send_file(f"cosmic-paste/static/{path}")
@server.route('/get_pixels', methods=["GET"])
def route_get_pixels(req):
global last_pixels
#print("get_pixels")
res = Response()
res.headers["Access-Control-Allow-Origin"] = '*'
res.body = last_pixels
return res
@server.post('/set_pixels')
def set_pixels(req):
global last_pixels
#print("got data")
data = req.body
# save for later
last_pixels = data
arr = list(data)
for j in range(HEIGHT):
for i in range(WIDTH):
index = (j * 32 + i) * 4
#convert rgba to rgb
r = int(data[index])
g = int(data[index+1])
b = int(data[index+2])
a = int(data[index+3]) / 255
r = round(a * r)
g = round(a * g)
b = round(a * b)
# set the pixel
graphics.set_pen(graphics.create_pen(r, g, b))
graphics.pixel(i, j)
cu.update(graphics)
res = Response()
res.headers["Access-Control-Allow-Origin"] = '*'
res.body = "success"
return res
@server.route("/get_brightness", methods=["GET"])
def get_brightness(req):
res = Response()
res.headers["Access-Control-Allow-Origin"] = '*'
res.body = str(cu.get_brightness())
return res
@server.post("/set_brightness")
def set_brightness(req):
res = Response()
res.headers["Access-Control-Allow-Origin"] = '*'
res.body = "failure"
brightness = req.body
if brightness:
brightnessFloat = float(brightness)
if brightnessFloat >= 0 and brightnessFloat <= 10:
cu.set_brightness(brightnessFloat)
res.body = "success"
return res
# start the web server on all ips, port 80
server.run(host="0.0.0.0", port=80)