-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
91 lines (81 loc) · 3.38 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
<title>Micro:bit WebUSB Console</title>
</head>
<body>
<div class="box">
<div class="row header">
<button id="connect" class="button">Connect</button>
</div>
<div class="row header">
<button id="disconnect" class="button">Disconnect All</button>
</div>
<div class="row header">
<button id="send" class="button">Send "Test" to All</button>
</div>
<div class="row header">
<button id="clear" class="button">Clear Console</button>
</div>
<div id="console" class="row content" style="overflow:auto;border:4px solid black;padding:2%">
Waiting for connection.
</div>
</div>
</body>
<!-- Include the DAP support library and WebUSB support -->
<script src="ubitwebusb.js"></script>
<script>
// Append a line to the console frame
function consolePrintln(message) {
var con = document.getElementById("console")
con.appendChild(document.createTextNode(message))
con.appendChild(document.createElement("br"))
con.scrollTop = con.scrollHeight
}
function consolePrintlnBold(message) {
var con = document.getElementById("console")
let boldItem = document.createElement("b")
boldItem.innerHTML = message
con.appendChild(boldItem)
con.appendChild(document.createElement("br"))
con.scrollTop = con.scrollHeight
}
// List of connected devices (a single value could be used if only connecting to one device)
var connectedDevices = []
// Example event call-back handler
function uBitEventHandler(reason, device, data) {
switch(reason) {
case "connected":
consolePrintlnBold("Connected!")
connectedDevices.push(device)
break
case "disconnected":
consolePrintlnBold("Disconnected")
connectedDevices = connectedDevices.filter( v => v != device)
break
case "connection failure":
consolePrintlnBold("Connection Failure")
break
case "error":
consolePrintlnBold("Error")
break
case "console":
consolePrintln("Console Data: " + data.data)
break
case "graph-event":
consolePrintln(`Graph Event: ${data.data} (for ${data.graph}${data.series.length?" / series "+data.series:""})`)
break
case "graph-data":
consolePrintln(`Graph Data: ${data.data} (for ${data.graph}${data.series.length?" / series "+data.series:""})`)
break
}
}
// Make the "go" button open the request devices box
document.getElementById("connect").addEventListener("click", () => uBitConnectDevice(uBitEventHandler))
document.getElementById("disconnect").addEventListener("click", () => { connectedDevices.forEach(d=>uBitDisconnect(d)); connectedDevices = [];})
document.getElementById("clear").addEventListener("click", () => { document.getElementById("console").innerHTML = "" })
document.getElementById("send").addEventListener("click", () => { connectedDevices.forEach( d=>uBitSend(d, "Test")) })
</script>
</html>