This repository was archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.ts
More file actions
135 lines (117 loc) · 3.51 KB
/
main.ts
File metadata and controls
135 lines (117 loc) · 3.51 KB
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
135
function identify(d: jacdac.Device) {
if (!d) return
if (d == jacdac.bus.selfDevice) {
control.runInParallel(() => {
jacdac.bus.emit(jacdac.IDENTIFY)
jacdac.bus.emit(jacdac.STATUS_EVENT, jacdac.StatusEvent.Identify)
})
} else {
d.sendCtrlCommand(jacdac.ControlCmd.Identify)
}
}
function describe(dev: jacdac.Device) {
let name = ""
if (dev == jacdac.bus.selfDevice)
name = "<self>"
/*
else if (dns) {
const bound = dns.remoteRequestedDevices.find(d => d.boundTo == dev)
if (bound) name = "(" + bound.name + ")"
}
*/
return `${dev.shortId} ${name}`
}
function selectDevice(fun: string, cond: (dev: jacdac.Device) => boolean) {
let res: jacdac.Device = undefined
let devs: jacdac.Device[]
menu.show({
title: "Function: " + fun,
footer: "A = select, -> = identify",
update: opts => {
devs = jacdac.bus.devices.filter(cond)
opts.elements = devs.map(d => menu.item(describe(d), opts => {
res = d
menu.exit(opts)
}, () => identify(d)))
}
})
return res
}
function resetAll() {
jacdac.JDPacket.onlyHeader(jacdac.ControlCmd.Reset)
.sendAsMultiCommand(jacdac.SRV_CONTROL)
}
let consoleClient: jacdac.LoggerClient
function showConsole() {
game.pushScene() // black bg
game.consoleOverlay.setVisible(true)
}
function hideConsole() {
console.log("B to exit...")
let done = false
controller.B.onEvent(ControllerButtonEvent.Pressed, () => {
done = true
})
pauseUntil(() => done)
game.consoleOverlay.setVisible(false)
game.popScene()
}
function startConsole() {
if (!consoleClient) {
consoleClient = new jacdac.LoggerClient()
consoleClient.minPriority = jacdac.LoggerPriority.Debug
consoleClient.start()
}
showConsole()
hideConsole()
}
function wifi() {
showConsole()
//net.updateAccessPoint("SSID", "pass")
console.log("WiFi starting...")
net.logPriority = ConsolePriority.Log
const n = net.instance()
const cl = n.controller
cl.connect()
pauseUntil(() => cl.isConnected)
console.log("connected; MAC:" + cl.MACaddress.toHex())
const resp = net.get("https://pxt.io/api/ping")
console.log("resp: " + resp.toString())
console.log("cont: " + resp.content)
hideConsole()
}
function deviceBrowser() {
let devs: jacdac.Device[] = []
menu.show({
title: "JACDAC browser",
footer: "A=view, > test, < ID",
update: opts => {
devs = jacdac.bus.devices.slice()
devs.sort((a, b) => a.shortId.compare(b.shortId))
opts.elements = devs.map(d => menu.item(describe(d), () => deviceView(d),
() => testDevice(d), () => identify(d)))
}
})
}
interface FnMap {
[index: string]: () => void;
}
function mainMenu() {
menu.show({
title: "JACDAC tool",
update: opts => {
// opts.elements = allDNSes().map(d => menu.item("DNS: " + describe(d), () => operateDNS(d)))
opts.elements = []
opts.elements.push(menu.item("Device browser", deviceBrowser))
opts.elements.push(menu.item("WiFi", wifi))
opts.elements.push(menu.item("Console", startConsole))
opts.elements.push(menu.item("Reset all devices", resetAll))
}
})
}
function main() {
menu.wait(1000, "Scanning...")
mainMenu()
}
jacdac.start({ disableRoleManager: true })
control.runInBackground(main)