Skip to content

Commit 6c3eebf

Browse files
Panic UX. (#43)
Panic doesn't stop the sim from doing more work but we'll add that separately when revisiting "stop".
1 parent a5b2a69 commit 6c3eebf

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

src/board/display.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ export class Display {
3030
];
3131
}
3232

33+
/**
34+
* This is only used for panic. HAL interactions are via setPixel.
35+
*/
36+
show(image: Array<Array<number>>) {
37+
for (let y = 0; y < 5; ++y) {
38+
for (let x = 0; x < 5; ++x) {
39+
this.state[x][y] = clamp(image[y][x], 0, 9);
40+
}
41+
}
42+
this.render();
43+
}
44+
3345
clear() {
3446
this.state = this.initialState();
3547
this.render();

src/board/index.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export class Board {
4949
radio: Radio;
5050
dataLogging: DataLogging;
5151

52+
private panicTimeout: any;
53+
5254
public serialInputBuffer: number[] = [];
5355

5456
private stoppedOverlay: HTMLDivElement;
@@ -268,6 +270,11 @@ export class Board {
268270
}
269271

270272
async stop(): Promise<void> {
273+
if (this.panicTimeout) {
274+
clearTimeout(this.panicTimeout);
275+
this.panicTimeout = null;
276+
this.display.clear();
277+
}
271278
const interrupt = () => this.serialInputBuffer.push(3, 4); // Ctrl-C, Ctrl-D.
272279
await this.operations.stop(interrupt);
273280
this.displayStoppedState();
@@ -289,6 +296,110 @@ export class Board {
289296
return this.start();
290297
}
291298

299+
panic(code: number): void {
300+
// We should hang MicroPython here. I think ideally we'd stop it entirely so we do this without any WASM.
301+
// For now we just do the display animation.
302+
const sad = [
303+
[9, 9, 0, 9, 9],
304+
[9, 9, 0, 9, 9],
305+
[0, 0, 0, 0, 0],
306+
[0, 9, 9, 9, 0],
307+
[9, 0, 0, 0, 9],
308+
];
309+
// Extracted via display.get_pixel.
310+
const digitFont = [
311+
[
312+
[0, 9, 9, 0, 0],
313+
[9, 0, 0, 9, 0],
314+
[9, 0, 0, 9, 0],
315+
[9, 0, 0, 9, 0],
316+
[0, 9, 9, 0, 0],
317+
],
318+
[
319+
[0, 0, 9, 0, 0],
320+
[0, 9, 9, 0, 0],
321+
[0, 0, 9, 0, 0],
322+
[0, 0, 9, 0, 0],
323+
[0, 9, 9, 9, 0],
324+
],
325+
[
326+
[9, 9, 9, 0, 0],
327+
[0, 0, 0, 9, 0],
328+
[0, 9, 9, 0, 0],
329+
[9, 0, 0, 0, 0],
330+
[9, 9, 9, 9, 0],
331+
],
332+
[
333+
[9, 9, 9, 9, 0],
334+
[0, 0, 0, 9, 0],
335+
[0, 0, 9, 0, 0],
336+
[9, 0, 0, 9, 0],
337+
[0, 9, 9, 0, 0],
338+
],
339+
[
340+
[0, 0, 9, 9, 0],
341+
[0, 9, 0, 9, 0],
342+
[9, 0, 0, 9, 0],
343+
[9, 9, 9, 9, 9],
344+
[0, 0, 0, 9, 0],
345+
],
346+
[
347+
[9, 9, 9, 9, 9],
348+
[9, 0, 0, 0, 0],
349+
[9, 9, 9, 9, 0],
350+
[0, 0, 0, 0, 9],
351+
[9, 9, 9, 9, 0],
352+
],
353+
[
354+
[0, 0, 0, 9, 0],
355+
[0, 0, 9, 0, 0],
356+
[0, 9, 9, 9, 0],
357+
[9, 0, 0, 0, 9],
358+
[0, 9, 9, 9, 0],
359+
],
360+
[
361+
[9, 9, 9, 9, 9],
362+
[0, 0, 0, 9, 0],
363+
[0, 0, 9, 0, 0],
364+
[0, 9, 0, 0, 0],
365+
[9, 0, 0, 0, 0],
366+
],
367+
[
368+
[0, 9, 9, 9, 0],
369+
[9, 0, 0, 0, 9],
370+
[0, 9, 9, 9, 0],
371+
[9, 0, 0, 0, 9],
372+
[0, 9, 9, 9, 0],
373+
],
374+
[
375+
[0, 9, 9, 9, 0],
376+
[9, 0, 0, 0, 9],
377+
[0, 9, 9, 9, 0],
378+
[0, 0, 9, 0, 0],
379+
[0, 9, 0, 0, 0],
380+
],
381+
];
382+
// Three digit code with leading zero if required.
383+
// For larger values we just display the last three digits.
384+
let digits = code.toString();
385+
digits = digits.slice(-3);
386+
const prefix = "0".repeat(3 - digits.length);
387+
digits = prefix + digits;
388+
const frames = [
389+
sad,
390+
...Array.from(digits).map((d) => digitFont[parseInt(d, 10)]),
391+
];
392+
let nextFrameIndex = 0;
393+
const showNextFrame = () => {
394+
this.display.show(frames[nextFrameIndex++ % frames.length]);
395+
this.panicTimeout = setTimeout(() => {
396+
this.display.clear();
397+
this.panicTimeout = setTimeout(showNextFrame, 60);
398+
}, 600);
399+
};
400+
showNextFrame();
401+
}
402+
292403
mute() {
293404
this.audio.mute();
294405
}

src/jshal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void mp_js_hal_filesystem_remove(int idx);
3939
int mp_js_hal_filesystem_readbyte(int idx, size_t offset);
4040
int mp_js_hal_filesystem_write(int idx, const char *buf, size_t len);
4141

42+
void mp_js_hal_panic(int code);
4243
void mp_js_hal_reset(void);
4344

4445
int mp_js_hal_temperature(void);

src/jshal.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ mergeInto(LibraryManager.library, {
8585
return board.reset();
8686
},
8787

88+
mp_js_hal_panic: function (code) {
89+
return board.panic(code);
90+
},
91+
8892
mp_js_hal_temperature: function () {
8993
return board.temperature.value;
9094
},

src/microbithal_js.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void microbit_hal_reset(void) {
6969
}
7070

7171
void microbit_hal_panic(int code) {
72-
//microbit_panic(code);
72+
mp_js_hal_panic(code);
7373
}
7474

7575
int microbit_hal_temperature(void) {

0 commit comments

Comments
 (0)