forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.ts
335 lines (307 loc) · 10.7 KB
/
home.ts
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
325
326
327
328
329
330
331
332
333
334
335
namespace userconfig {
export const DISPLAY_CFG0 = 0x02000080 // don't wait for shield on startup
// TODO this should be only enabled on micro:bit
}
namespace microcode {
enum HomeView {
Console, // Show the console log
Plot, // Show the plot graph
}
const TOOLBAR_HEIGHT = 18
const LINE_HEIGHT = 9
const CONSOLE_MARGIN = 2
export enum LineJustification {
Left,
Center,
Right,
}
type LogLine = {
text: string
color: number
justification: LineJustification
}
type PlotEntry = {
value: number
t: number
}
type PlotLine = {
entries: PlotEntry[]
min: number
max: number
}
export class Home extends Scene {
currView: HomeView
logLines: LogLine[]
plotLines: { [color: number]: PlotLine }
paused: boolean
singleStep: boolean
constructor(app: App) {
super(app, "home")
this.currView = HomeView.Console
this.logLines = []
this.plotLines = {}
}
/**
* Write a line of text to the console log.
*/
public log(
text: string,
color: number,
justification = LineJustification.Left
) {
this.logLines.push({ text, color, justification })
// trim to last 15 entries
this.logLines = this.logLines.slice(
Math.max(this.logLines.length - 15, 0)
)
}
/**
* Plot a point on the graph.
*/
public plot(value: number, color: number) {
let plotLine = this.plotLines[color]
if (!plotLine) {
this.plotLines[color] = plotLine = {
entries: [],
min: 0,
max: 0,
}
}
plotLine.entries.push({
value,
t: control.millis(),
})
plotLine.entries = plotLine.entries.slice(
Math.max(plotLine.entries.length - 160, 0)
)
plotLine.min = plotLine.max = value
plotLine.entries.forEach(entry => {
plotLine.min = Math.min(plotLine.min, entry.value)
plotLine.max = Math.max(plotLine.max, entry.value)
})
}
public logBoolean(name: string, val: boolean, color: number) {
const text = `${name}: ${val ? "true" : "false"}`
this.log(text, color)
this.setView(HomeView.Console)
}
public logNumber(name: string, val: number, color: number) {
const text = `${name}: ${val.toString()}`
this.log(text, color)
this.setView(HomeView.Console)
}
public logString(name: string, val: string, color: number) {
const text = `${name}: ${val}`
this.log(val, color)
this.setView(HomeView.Console)
}
public plotBoolean(name: string, val: boolean, color: number) {
this.plot(val ? 1 : 0, color)
this.setView(HomeView.Plot)
}
public plotNumber(name: string, val: number, color: number) {
this.plot(val, color)
this.setView(HomeView.Plot)
}
/* override */ startup() {
super.startup()
control.onEvent(
ControllerButtonEvent.Released,
controller.left.id,
() => {
this.app.popScene()
this.app.pushScene(new Editor(this.app))
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.up.id,
() => {
this.paused = !this.paused
if (this.paused) {
this.log("program paused", 1)
} else {
this.log("program resumed", 1)
}
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.up.id,
() => {
console.log("singleStep")
this.singleStep = true
}
)
control.onEvent(
ControllerButtonEvent.Repeated,
controller.up.id,
() => {
this.singleStep = true
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.right.id,
() => {
const progdef = this.app.load(SAVESLOT_AUTO)
if (progdef) {
new jacs.TopWriter().emitProgram(progdef)
}
}
)
}
/* override */ shutdown() {
this.logLines = undefined
this.plotLines = undefined
super.shutdown()
}
/* override */ activate() {
super.activate()
this.color = 15
this.logLines = []
this.log(" _ ", 7, LineJustification.Center)
this.log(" /\\/\\ . _ _ _ / _ _| _", 7, LineJustification.Center)
this.log("/ \\|(_| (_)\\_(_)(_|(-", 7, LineJustification.Center)
this.log("", 7)
this.log(" Welcome to MicroCode!", 7, LineJustification.Center)
this.log("", 7)
this.log("", 7)
let progdef = this.app.load(SAVESLOT_AUTO)
if (!progdef) {
progdef = new ProgramDefn()
this.app.save(SAVESLOT_AUTO, progdef)
}
this.paused = false
this.log("program started", 1)
}
/* override */ deactivate() {
this.logLines = []
}
/* override */ update() {
super.update()
if (!this.paused || (this.paused && this.singleStep)) {
this.singleStep = false
}
}
private setView(view: HomeView) {
this.currView = view
}
/* override */ draw() {
this.drawToolbar()
if (this.currView === HomeView.Console) {
this.drawConsoleView()
} else if (this.currView === HomeView.Plot) {
this.drawPlotView()
}
super.draw()
}
private drawToolbar() {
// TODO this could all be prerendered/precalculated
const toolbarTop = Screen.BOTTOM_EDGE - TOOLBAR_HEIGHT
Screen.fillRect(
Screen.LEFT_EDGE,
toolbarTop,
Screen.WIDTH,
TOOLBAR_HEIGHT,
11
)
const icn_dpad_left = icons.get("dpad_left")
const dpadTop =
toolbarTop + (TOOLBAR_HEIGHT >> 1) - (icn_dpad_left.height >> 1)
Screen.print(
"Press to edit your code",
Screen.LEFT_EDGE + 2,
dpadTop + (LINE_HEIGHT >> 1)
)
Screen.drawTransparentImage(
icn_dpad_left,
Screen.LEFT_EDGE + 32,
dpadTop
)
}
private drawConsoleView() {
Screen.fillRect(
Screen.LEFT_EDGE,
Screen.TOP_EDGE,
Screen.WIDTH,
Screen.HEIGHT - TOOLBAR_HEIGHT,
15
)
let y = Screen.BOTTOM_EDGE - TOOLBAR_HEIGHT - LINE_HEIGHT
for (let i = this.logLines.length - 1; i >= 0; --i) {
const line = this.logLines[i]
switch (line.justification) {
case LineJustification.Left: {
const x = Screen.LEFT_EDGE + CONSOLE_MARGIN
Screen.print(line.text, x, y, line.color, image.font8)
break
}
case LineJustification.Center: {
const x =
Screen.LEFT_EDGE +
((Screen.WIDTH -
line.text.length * image.font8.charWidth) >>
1) -
image.font8.charWidth
Screen.print(line.text, x, y, line.color, image.font8)
break
}
case LineJustification.Right: {
const x =
Screen.RIGHT_EDGE -
CONSOLE_MARGIN -
line.text.length * image.font8.charWidth
Screen.print(line.text, x, y, line.color, image.font8)
break
}
}
y -= LINE_HEIGHT
}
}
private drawPlotView() {
screen.fillRect(
Screen.LEFT_EDGE,
Screen.TOP_EDGE,
Screen.WIDTH,
Screen.HEIGHT - TOOLBAR_HEIGHT,
6
)
const maxHeight = Screen.HEIGHT - TOOLBAR_HEIGHT - 10
for (let color = 1; color < 16; ++color) {
const plotLine = this.plotLines[color]
if (plotLine && plotLine.entries.length > 1) {
let currX = Screen.RIGHT_EDGE
const min = plotLine.min
const max = plotLine.max
const range = Math.max(Math.abs(max - min), 1)
const heightScale = maxHeight / range
for (
let i = plotLine.entries.length - 1, x = 0;
i > 0 && x < Screen.WIDTH;
--i, ++x
) {
const a = plotLine.entries[i]
const b = plotLine.entries[i - 1]
const av = a.value - min
const bv = b.value - min
const ax = Screen.RIGHT_EDGE - x
const ay =
Screen.TOP_EDGE +
maxHeight -
Math.floor(av * heightScale) +
5
const bx = Screen.RIGHT_EDGE - x - 1
const by =
Screen.TOP_EDGE +
maxHeight -
Math.floor(bv * heightScale) +
5
Screen.drawLine(ax, ay, bx, by, color)
}
}
}
}
}
const PIXELS_PER_MILLI = 1
}