forked from code4fukui/PybricksHub.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.html
131 lines (120 loc) · 3.26 KB
/
js.html
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
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>PybricksHub test in JS</title>
</head><body>
<h1>PybricksHub test in JS</h1>
<button id=btnconnect>connect</button>
<button id=btndisconnect>disconnect</button>
<button id=btnrepl>REPL</button>
<br>
<textarea id=taout></textarea><br>
<input id=inp><button id=btnsend>send</button><button id=btnclear>clear</button><br>
<textarea id=taprog>import { PrimeHub } from "pybricks/hubs.js";
import { Motor, ColorSensor, UltrasonicSensor, ForceSensor } from "pybricks/pupdevices.js";
import { Button, Color, Direction, Port, Side, Stop } from "pybricks/parameters.js";
import { DriveBase } from "pybricks/robotics.js";
import { wait, StopWatch } from "pybricks/tools.js";
import { Matrix } from "pybricks/geometry.js";
import { randint } from "urandom.js";
const hub = PrimeHub();
const X = 100;
const field = [
[0, X, 0, 0, 0],
[0, 0, 0, X, X],
[0, X, 0, X, 0],
[0, X, 0, 0, X],
[X, X, 0, X, X],
];
hub.display.icon(field)
const m1 = Motor(Port.A);
const m2 = Motor(Port.B);
function forward(n) {
print("forward");
m1.run(n);
m2.run(-n);
}
function right(n) {
print("right");
m1.run(n);
m2.stop();
}
function left(n) {
print("left");
m2.stop();
m1.run(-n);
}
for (;;) {
forward(300);
wait(500);
right(300);
wait(500);
left(300);
wait(1000);
}
</textarea><br>
<button id=btnset>set program</button>
<button id=btnstart>start program</button>
<button id=btnstop>stop program</button>
<style>
* {
box-sizing: border-box;
}
body {
margin: 1em;
}
textarea {
width: 100%;
height: 10em;
margin: .5em 0;
padding: .2em;
}
#taprog {
height: 40em;
}
</style>
<script type="module">
import { PybricksHub } from "./PybricksHub.js";
import { JS2Py } from "https://code4fukui.github.io/js2py/src/JS2Py.js";
const hub = new PybricksHub();
hub.setReceiver(s => {
taout.value += s;
taout.scrollTop = taout.scrollHeight;
});
btnconnect.onclick = async () => {
await hub.connect();
await hub.startREPL();
};
btndisconnect.onclick = async () => {
await hub.disconnect();
};
btnsend.onclick = async () => {
const s = inp.value;
await hub.send(s + "\r\n");
inp.value = "";
};
btnclear.onclick = async () => {
taout.value = "";
};
btnrepl.onclick = async () => {
await hub.stopProgram();
await hub.startREPL();
};
btnstop.onclick = async () => {
await hub.stopProgram();
};
btnstart.onclick = async () => {
await hub.stopProgram();
await hub.startProgram();
};
btnset.onclick = async () => {
const js = taprog.value;
const py = new JS2Py().convert(js);
console.log(py);
await hub.stopProgram();
await hub.setProgram(py);
};
</script>
<hr>
docs: <a href=https://docs.pybricks.com/en/latest/>Pybricks Documentation — pybricks v3.2.0 documentation</a><br>
fimware: <a href=https://code4fukui.github.io/pybricks-firmware/>install Pybrics firmawre</a><br>
lib: <a href=https://github.com/code4fukui/js2py/>JS2Py</a><br>
<a href="https://github.com/code4fukui/PybricksHub.js/">src on GitHub</a> (forked from <a href=https://github.com/pybricks/pybricks-code>pybricks-code</a> in <a href=https://pybricks.com/>Pybricks</a>)<br>