Skip to content

Panic UX. #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/board/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export class Display {
];
}

/**
* This is only used for panic. HAL interactions are via setPixel.
*/
show(image: Array<Array<number>>) {
for (let y = 0; y < 5; ++y) {
for (let x = 0; x < 5; ++x) {
this.state[x][y] = clamp(image[y][x], 0, 9);
}
}
this.render();
}

clear() {
this.state = this.initialState();
this.render();
Expand Down
111 changes: 111 additions & 0 deletions src/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class Board {
radio: Radio;
dataLogging: DataLogging;

private panicTimeout: any;

public serialInputBuffer: number[] = [];

private stoppedOverlay: HTMLDivElement;
Expand Down Expand Up @@ -268,6 +270,11 @@ export class Board {
}

async stop(): Promise<void> {
if (this.panicTimeout) {
clearTimeout(this.panicTimeout);
this.panicTimeout = null;
this.display.clear();
}
const interrupt = () => this.serialInputBuffer.push(3, 4); // Ctrl-C, Ctrl-D.
await this.operations.stop(interrupt);
this.displayStoppedState();
Expand All @@ -289,6 +296,110 @@ export class Board {
return this.start();
}

panic(code: number): void {
// We should hang MicroPython here. I think ideally we'd stop it entirely so we do this without any WASM.
// For now we just do the display animation.
const sad = [
[9, 9, 0, 9, 9],
[9, 9, 0, 9, 9],
[0, 0, 0, 0, 0],
[0, 9, 9, 9, 0],
[9, 0, 0, 0, 9],
];
// Extracted via display.get_pixel.
const digitFont = [
[
[0, 9, 9, 0, 0],
[9, 0, 0, 9, 0],
[9, 0, 0, 9, 0],
[9, 0, 0, 9, 0],
[0, 9, 9, 0, 0],
],
[
[0, 0, 9, 0, 0],
[0, 9, 9, 0, 0],
[0, 0, 9, 0, 0],
[0, 0, 9, 0, 0],
[0, 9, 9, 9, 0],
],
[
[9, 9, 9, 0, 0],
[0, 0, 0, 9, 0],
[0, 9, 9, 0, 0],
[9, 0, 0, 0, 0],
[9, 9, 9, 9, 0],
],
[
[9, 9, 9, 9, 0],
[0, 0, 0, 9, 0],
[0, 0, 9, 0, 0],
[9, 0, 0, 9, 0],
[0, 9, 9, 0, 0],
],
[
[0, 0, 9, 9, 0],
[0, 9, 0, 9, 0],
[9, 0, 0, 9, 0],
[9, 9, 9, 9, 9],
[0, 0, 0, 9, 0],
],
[
[9, 9, 9, 9, 9],
[9, 0, 0, 0, 0],
[9, 9, 9, 9, 0],
[0, 0, 0, 0, 9],
[9, 9, 9, 9, 0],
],
[
[0, 0, 0, 9, 0],
[0, 0, 9, 0, 0],
[0, 9, 9, 9, 0],
[9, 0, 0, 0, 9],
[0, 9, 9, 9, 0],
],
[
[9, 9, 9, 9, 9],
[0, 0, 0, 9, 0],
[0, 0, 9, 0, 0],
[0, 9, 0, 0, 0],
[9, 0, 0, 0, 0],
],
[
[0, 9, 9, 9, 0],
[9, 0, 0, 0, 9],
[0, 9, 9, 9, 0],
[9, 0, 0, 0, 9],
[0, 9, 9, 9, 0],
],
[
[0, 9, 9, 9, 0],
[9, 0, 0, 0, 9],
[0, 9, 9, 9, 0],
[0, 0, 9, 0, 0],
[0, 9, 0, 0, 0],
],
];
// Three digit code with leading zero if required.
// For larger values we just display the last three digits.
let digits = code.toString();
digits = digits.slice(-3);
const prefix = "0".repeat(3 - digits.length);
digits = prefix + digits;
const frames = [
sad,
...Array.from(digits).map((d) => digitFont[parseInt(d, 10)]),
];
let nextFrameIndex = 0;
const showNextFrame = () => {
this.display.show(frames[nextFrameIndex++ % frames.length]);
this.panicTimeout = setTimeout(() => {
this.display.clear();
this.panicTimeout = setTimeout(showNextFrame, 60);
}, 600);
};
showNextFrame();
}

mute() {
this.audio.mute();
}
Expand Down
1 change: 1 addition & 0 deletions src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void mp_js_hal_filesystem_remove(int idx);
int mp_js_hal_filesystem_readbyte(int idx, size_t offset);
int mp_js_hal_filesystem_write(int idx, const char *buf, size_t len);

void mp_js_hal_panic(int code);
void mp_js_hal_reset(void);

int mp_js_hal_temperature(void);
Expand Down
4 changes: 4 additions & 0 deletions src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ mergeInto(LibraryManager.library, {
return board.reset();
},

mp_js_hal_panic: function (code) {
return board.panic(code);
},

mp_js_hal_temperature: function () {
return board.temperature.value;
},
Expand Down
2 changes: 1 addition & 1 deletion src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void microbit_hal_reset(void) {
}

void microbit_hal_panic(int code) {
//microbit_panic(code);
mp_js_hal_panic(code);
}

int microbit_hal_temperature(void) {
Expand Down