/
scroll-phat-hd-dotter.py
134 lines (122 loc) · 3.03 KB
/
scroll-phat-hd-dotter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Scroll pHAT HD Dotter
# You'll need:
# Raspberry Pi Pico W
# Pimoroni Scroll pHAT HD
import config
import utime
import socket
import ujson
from machine import I2C, Pin
import is31fl3731
html = """<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Dot Editor for Scroll pHAT HD and Pico W</title>
<script>
function toggle_pixel(e) {
let x = e.cellIndex;
let y = e.parentElement.rowIndex;
var b, color;
if (e.style.backgroundColor == '') {
color = 'blue';
b = 50;
} else {
color = '';
b = 0;
}
fetch(`/led/${x}/${y}/${b}`)
.then(function(r){ return r.text(); })
.then(function(t){ e.style.backgroundColor = color; });
}
function scrollPhatTable() {
var canvas = document.getElementById("canvas");
let y = 7;
let x = 17;
for (var i = 0; i < y; i++) {
var row = canvas.insertRow(-1);
for (var j = 0; j < x; j++) {
var cell = row.insertCell(-1);
cell.onclick = function() { toggle_pixel(this); };
}
}
}
window.onload = function() { scrollPhatTable(); }
</script>
<style>
table {
border-collapse: collapse;
margin-bottom: 16px;
}
td {
border: 1px solid gray;
width: 32px;
height: 32px;
cursor: pointer;
}
</style>
</head>
<body>
<table id="canvas"></table>
<p>GET /led/[x(0-16)]/[y(0-6)]/[brightness(0-255)]</p>
</body>
</html>
"""
def scrollphathd_pixel(x, y, b):
py = 6 - y
if (x < 9):
px = 8 - x
else:
px = x - 9
py = y + 8
display.pixel(py, px, b)
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(config.ssid, config.password)
while not sta_if.isconnected():
print(".")
utime.sleep(1)
pass
print('network config:', sta_if.ifconfig())
i2c = I2C(0, scl=Pin(5), sda=Pin(4))
display = is31fl3731.Matrix(i2c)
print("sleep for 5 seconds.")
utime.sleep(5)
do_connect()
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
while True:
cl, addr = s.accept()
req = str(cl.recv(1024), "utf-8").split("\r\n")
for l in req:
if "GET" in l:
path = l.split(" ")[1]
break
code = "200 OK"
ct = "text/html"
if path == "/":
response = html
elif path.startswith("/led"):
path_split = path.split('/')
x = int(path_split[2])
y = int(path_split[3])
b = int(path_split[4])
if x < 0 or y < 0 or b < 0 or 16 < x or 6 < y or 255 < b:
code = "400 Bad request"
response = "any parameter out of range"
else:
scrollphathd_pixel(x, y, b)
response = "%s(%s, %s)" % (b, x, y)
else:
code = "404 Not Found"
response = "Not Found"
cl.send('HTTP/1.0 %s\r\nContent-type: %s\r\n\r\n' % (code,ct))
cl.send(response)
cl.close()