Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build:
dist: build
mkdir -p $(BUILD)/build
cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(BUILD)
cp $(SRC)/build/micropython.js $(SRC)/build/firmware.wasm $(BUILD)/build/
cp $(SRC)/build/firmware.js $(SRC)/build/simulator.js $(SRC)/build/firmware.wasm $(BUILD)/build/

watch: dist
fswatch -o -e src/build src | while read _; do $(MAKE) dist; done
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ The following sections documents the messages supported via postMessage.
<td>Radio output (sent from the user's program) as bytes.
If you send string data from the program then it will be prepended with the three bytes 0x01, 0x00, 0x01.

<tr>
<td>internal_error
<td>

```javascript
{
"kind": "internal_error",
"error": new Error()
}
```

<td>A debug message sent for internal (unexpected) errors thrown by the simulator. Suitable for application-level logging. Please raise issues in this project as these indicate a bug in the simulator.

</table>

## Messages supported by the iframe
Expand Down
17 changes: 9 additions & 8 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Makefile to build micropython.js
# Makefile to build WASM and simulator JS.

# Build upon the codal_port code.
CODAL_PORT = $(abspath ../lib/micropython-microbit-v2/src/codal_port)
Expand Down Expand Up @@ -44,9 +44,11 @@ COPT += -O3 -DNDEBUG
endif

JSFLAGS += -s ASYNCIFY
JSFLAGS += -s EXPORTED_FUNCTIONS="['_mp_js_main','_mp_js_request_stop','_microbit_hal_audio_ready_callback','_microbit_hal_audio_speech_ready_callback','_microbit_hal_gesture_callback','_microbit_hal_level_detector_callback','_microbit_radio_rx_buffer']"
JSFLAGS += -s EXIT_RUNTIME
JSFLAGS += -s MODULARIZE=1
JSFLAGS += -s EXPORT_NAME=createModule
JSFLAGS += -s EXPORTED_FUNCTIONS="['_mp_js_main','_microbit_hal_audio_ready_callback','_microbit_hal_audio_speech_ready_callback','_microbit_hal_gesture_callback','_microbit_hal_level_detector_callback','_microbit_radio_rx_buffer','_mp_js_force_stop','_mp_js_request_stop']"
JSFLAGS += -s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" --js-library jshal.js
JSFLAGS += --pre-js $(BUILD)/pre.js

ifdef DEBUG
JSFLAGS += -g
Expand Down Expand Up @@ -136,14 +138,13 @@ $(MBIT_VER_FILE): FORCE
$(PYTHON) $(TOP)/py/makeversionhdr.py $(MBIT_VER_FILE).pre
$(CAT) $(MBIT_VER_FILE).pre | $(SED) s/MICROPY_/MICROBIT_/ > $(MBIT_VER_FILE)

$(BUILD)/micropython.js: $(OBJ) jshal.js main.js pre-js
$(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js
$(ECHO) "LINK $(BUILD)/firmware.js"
$(Q)emcc $(LDFLAGS) -o $(BUILD)/firmware.js $(OBJ) $(JSFLAGS)
cat main.js $(BUILD)/firmware.js > $@

pre-js:
npx esbuild ./pre.ts --bundle --outfile=$(BUILD)/pre.js --loader:.svg=text
simulator-js:
npx esbuild ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text

include $(TOP)/py/mkrules.mk

.PHONY: pre-js
.PHONY: simulator-js
10 changes: 4 additions & 6 deletions src/board/accelerometer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class Accelerometer {

setValue(id: StateKeys, value: any) {
this.state[id].setValue(value);
if (id === "gesture") {
this.gestureCallback!(
if (id === "gesture" && this.gestureCallback) {
this.gestureCallback(
convertAccelerometerStringToNumber(this.state.gesture.value)
);
}
Expand All @@ -71,11 +71,9 @@ export class Accelerometer {
});
}

initialize(gestureCallback: GestureCallback) {
initializeCallbacks(gestureCallback: GestureCallback) {
this.gestureCallback = gestureCallback;
}

dispose() {
this.gestureCallback = undefined;
}
dispose() {}
}
5 changes: 4 additions & 1 deletion src/board/audio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export class Audio {

constructor() {}

initialize({ defaultAudioCallback, speechAudioCallback }: AudioOptions) {
initializeCallbacks({
defaultAudioCallback,
speechAudioCallback,
}: AudioOptions) {
this.context = new AudioContext({
// The highest rate is the sound expression synth.
sampleRate: 44100,
Expand Down
2 changes: 0 additions & 2 deletions src/board/buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ export class Button {
return result;
}

initialize() {}

dispose() {
this._presses = 0;
}
Expand Down
2 changes: 0 additions & 2 deletions src/board/compass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ export class Compass {
return Math.sqrt(x * x + y * y + z * z);
}

initialize() {}

dispose() {}
}
25 changes: 13 additions & 12 deletions src/board/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,10 @@ import {
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_LEFT,
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_RIGHT,
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_UP,
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH,
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW,
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH,
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW,
} from "./constants";

export function convertAudioBuffer(source: number, target: AudioBuffer) {
const channel = target.getChannelData(0);
const heap = window.HEAPU8;
for (let i = 0; i < channel.length; ++i) {
// Convert from uint8 to -1..+1 float.
channel[i] = (heap[source + i] / 255) * 2 - 1;
}
return target;
}

export function convertSoundThresholdNumberToString(
value: number
): "low" | "high" {
Expand Down Expand Up @@ -106,3 +94,16 @@ export function convertAccelerometerNumberToString(value: number): string {
throw new Error(`Invalid value ${value}`);
}
}

export const convertAudioBuffer = (
heap: Uint8Array,
source: number,
target: AudioBuffer
) => {
const channel = target.getChannelData(0);
for (let i = 0; i < channel.length; ++i) {
// Convert from uint8 to -1..+1 float.
channel[i] = (heap[source + i] / 255) * 2 - 1;
}
return target;
};
2 changes: 0 additions & 2 deletions src/board/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ export class Display {
}
}

initialize() {}

dispose() {
this.clear();
}
Expand Down
Loading