-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhost.ts
More file actions
324 lines (295 loc) · 11.7 KB
/
Copy pathhost.ts
File metadata and controls
324 lines (295 loc) · 11.7 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
namespace microcode {
// mapping of micro:bit and DAL namespace into MicroCode tiles
type IdMap = { [id: number]: number }
// see DAL for these values
const matchPressReleaseTable: IdMap = {
1: Tid.TID_FILTER_BUTTON_A, // DAL.DEVICE_ID_BUTTON_A
2: Tid.TID_FILTER_BUTTON_B, // DAL.DEVICE_ID_BUTTON_B
121: Tid.TID_FILTER_LOGO, // DAL.MICROBIT_ID_LOGO
100: Tid.TID_FILTER_PIN_0, // DAL.ID_PIN_P0
101: Tid.TID_FILTER_PIN_1, // DAL.ID_PIN_P1
102: Tid.TID_FILTER_PIN_2, // DAL.ID_PIN_P2
}
const gestures = [
Gesture.Shake,
Gesture.LogoUp,
Gesture.LogoDown,
Gesture.TiltLeft,
Gesture.TiltRight,
Gesture.ScreenUp,
Gesture.ScreenDown,
]
const gestures2tids = [
Tid.TID_FILTER_ACCEL_SHAKE,
Tid.TID_FILTER_ACCEL_TILT_UP,
Tid.TID_FILTER_ACCEL_TILT_DOWN,
Tid.TID_FILTER_ACCEL_TILT_LEFT,
Tid.TID_FILTER_ACCEL_TILT_RIGHT,
Tid.TID_FILTER_ACCEL_FACE_UP,
Tid.TID_FILTER_ACCEL_FACE_DOWN,
]
const buttons = [
DAL.DEVICE_ID_BUTTON_A,
DAL.DEVICE_ID_BUTTON_B,
DAL.MICROBIT_ID_LOGO,
]
const pins = [TouchPin.P0, TouchPin.P1, TouchPin.P2]
const pin2tid = [
Tid.TID_FILTER_PIN_0,
Tid.TID_FILTER_PIN_1,
Tid.TID_FILTER_PIN_2,
]
export class MicrobitHost implements RuntimeHost {
/** Monotonic token observed by cancellable LED display work. */
private displayVersion = 0
constructor() {
this._handler = (s: number, f: number) => {}
control.singleSimulator()
// make sure we have V2 simulator
input.onLogoEvent(TouchButtonEvent.Pressed, function () {})
buttons.forEach(b => {
control.onEvent(b, DAL.DEVICE_EVT_ANY, () => {
const ev = control.eventValue()
if (isProgramRunning()) {
const tid =
ev == DAL.DEVICE_BUTTON_EVT_DOWN
? Tid.TID_SENSOR_PRESS
: ev == DAL.DEVICE_BUTTON_EVT_UP
? Tid.TID_SENSOR_RELEASE
: undefined
const filter = matchPressReleaseTable[b]
this._handler(tid, filter)
} else {
// start up the program using the editor
if (ev == DAL.DEVICE_BUTTON_EVT_DOWN)
app.runFromEditor()
}
})
})
pins.forEach((p, index) => {
input.onPinPressed(p, () => {
this._handler(Tid.TID_SENSOR_PRESS, pin2tid[index])
})
input.onPinReleased(p, () => {
this._handler(Tid.TID_SENSOR_RELEASE, pin2tid[index])
})
})
gestures.forEach((g, index) => {
//TODO: This is a hack to fix the onGesture events not working.
// In GDB we see that the CPP functions onGesture, MicrobitAccelerometer constructor and the LSM303Accelerometer constructor are invoked.
// In spite of this the __handler is not invoked. For some reason this basic.pause(0) fixes this issue (possibly because of it briefly yielding?)
basic.pause(0)
input.onGesture(g, () => {
this._handler(
Tid.TID_SENSOR_ACCELEROMETER,
gestures2tids[index],
)
})
})
radio.onReceivedNumber(radioNum => {
this._handler(Tid.TID_SENSOR_RADIO_RECEIVE, radioNum)
})
input.onSound(DetectedSound.Loud, () => {
this._handler(Tid.TID_SENSOR_MICROPHONE, Tid.TID_FILTER_LOUD)
})
input.onSound(DetectedSound.Quiet, () => {
this._handler(Tid.TID_SENSOR_MICROPHONE, Tid.TID_FILTER_QUIET)
})
}
public getSensorValue(tid: number, normalized: boolean): number {
let value = 0
let min = 0
let max = 1
switch (tid) {
case Tid.TID_SENSOR_LED_LIGHT:
value = input.lightLevel()
max = 255
break
case Tid.TID_SENSOR_TEMP:
value = input.temperature()
min = -40
max = 100
break
case Tid.TID_SENSOR_MAGNET:
value = input.magneticForce(Dimension.Strength)
min = -5000
max = 5000
break
case Tid.TID_SENSOR_MICROPHONE:
value = input.soundLevel()
max = 255
break
}
if (!normalized) return value
return Math.abs(value) / (Math.abs(min) + max)
}
private _handler: (sensorTid: number, filter: number) => void
registerOnSensorEvent(
handler: (sensorTid: number, filter: number) => void,
) {
this._handler = (tid, filter) => {
if (tid >= 0 && filter) handler(tid, filter)
}
}
emitClearScreen() {
led.stopAnimation()
const displayVersion = this.beginDisplayOperation()
control.inBackground(() => {
const anim = hex`
0001000000
0000010000
0000000100
0000000002
0000000004
0000000008
0000001000
0000100000
0010000000
0800000000
0400000000
0200000000
0000000000
`
let pos = 0
while (
displayVersion == this.displayVersion &&
pos < anim.length
) {
for (let col = 0; col < 5; col++) {
for (let row = 0; row < 5; row++) {
if (displayVersion != this.displayVersion) return
const onOff =
anim[pos + col + (row >> 3)] & (1 << (row & 7))
if (onOff) led.plot(col, row)
else led.unplot(col, row)
}
}
basic.pause(20)
pos = pos + 5
}
})
}
public stopOngoingActions() {
this.cancelDisplayOperation()
music.stopAllSounds()
led.stopAnimation()
basic.clearScreen()
}
public execute(action: ActionTid, param: any) {
switch (action) {
case Tid.TID_ACTUATOR_PAINT:
const displayVersion = this.beginDisplayOperation()
led.stopAnimation()
this.showIcon(param, displayVersion)
return
case Tid.TID_ACTUATOR_SHOW_NUMBER:
this.beginDisplayOperation()
led.stopAnimation()
basic.showNumber(param)
return
case Tid.TID_ACTUATOR_RADIO_SET_GROUP:
radio.setGroup(param)
return
case Tid.TID_ACTUATOR_RADIO_SEND:
radio.sendNumber(param)
return
case Tid.TID_ACTUATOR_SPEAKER:
music.stopAllSounds()
music.play(
music.builtinPlayableSoundEffect(this.getSound(param)),
music.PlaybackMode.UntilDone,
)
return
case Tid.TID_ACTUATOR_MUSIC:
music.stopAllSounds()
music.play(
music.stringPlayable(param, 120),
music.PlaybackMode.UntilDone,
)
return
}
}
private beginDisplayOperation(): number {
this.displayVersion++
return this.displayVersion
}
private cancelDisplayOperation() {
this.displayVersion++
}
private pauseDisplayOperation(durationMs: number, version: number) {
const stepMs = 25
let remaining = durationMs
while (version == this.displayVersion && remaining > 0) {
const pauseMs = Math.min(stepMs, remaining)
basic.pause(pauseMs)
remaining -= pauseMs
}
}
private showIcon(img: Bitmap, displayVersion: number) {
let s: string[] = []
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 5; col++) {
if (img.getPixel(col, row)) led.plot(col, row)
else led.unplot(col, row)
}
}
// TODO: do want this here? do we really want to yield?
this.pauseDisplayOperation(400, displayVersion)
}
private getSound(sound: Tid) {
switch (sound) {
case Tid.TID_MODIFIER_EMOJI_GIGGLE:
return soundExpression.giggle
case Tid.TID_MODIFIER_EMOJI_HAPPY:
return soundExpression.happy
case Tid.TID_MODIFIER_EMOJI_HELLO:
return soundExpression.hello
case Tid.TID_MODIFIER_EMOJI_MYSTERIOUS:
return soundExpression.mysterious
case Tid.TID_MODIFIER_EMOJI_SAD:
return soundExpression.sad
case Tid.TID_MODIFIER_EMOJI_SLIDE:
return soundExpression.slide
case Tid.TID_MODIFIER_EMOJI_SOARING:
return soundExpression.soaring
case Tid.TID_MODIFIER_EMOJI_SPRING:
return soundExpression.spring
case Tid.TID_MODIFIER_EMOJI_TWINKLE:
return soundExpression.twinkle
case Tid.TID_MODIFIER_EMOJI_YAWN:
return soundExpression.yawn
}
return soundExpression.giggle
}
}
export const runtimeHost: RuntimeHost = new MicrobitHost()
}
/*
// TODO: convert this to external sensor with events and values
// TODO: and lift out
const radioVal = this.getRadioVal()
if (
sensor == Tid.TID_SENSOR_CAR_WALL ||
sensor == Tid.TID_SENSOR_LINE
) {
// this hack separates radio ranges used to communicate with robot car
if (
robot.robots.RobotCompactCommand.ObstacleState <
radioVal
)
if (sensor == Tid.TID_SENSOR_CAR_WALL)
return this.filterOnEvent(
radioVal -
robot.robots.RobotCompactCommand
.ObstacleState
)
else if (
robot.robots.RobotCompactCommand.LineState <=
radioVal
)
return this.filterOnEvent(radioVal)
} else if (
radioVal < robot.robots.RobotCompactCommand.ObstacleState
)
return this.filterViaCompare()
*/