Skip to content

Fix hw---gdk simulator crash: add readP0Adc simulator fallback - #7641

Open
GarbhitSh wants to merge 4 commits into
microsoft:masterfrom
GarbhitSh:fix/gdk-simulator-readp0adc
Open

Fix hw---gdk simulator crash: add readP0Adc simulator fallback#7641
GarbhitSh wants to merge 4 commits into
microsoft:masterfrom
GarbhitSh:fix/gdk-simulator-readp0adc

Conversation

@GarbhitSh

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #7600. Selecting Game Designer's Kit (hw---gdk) and running a project in the simulator crashes a couple of seconds after start with:

Cannot read properties of undefined (reading 'readP0Adc')

This PR adds a simulator fallback for the gdk.readP0Adc() shim so the board works in the simulator. No device behavior changes.

Root cause

readP0Adc() is a native SAADC shim implemented only in C++ (analog.cpp) and exposed via a declaration-only shims.d.ts:

//% shim=gdk::readP0Adc
function readP0Adc(): int32;

The battery-low monitor in batteryled.ts calls it from a background loop:

control.runInBackground(function () {
    pause(STARTUP_DELAY_MS);
    updateLed();
    while (true) {
        pause(REFRESH_MS);
        updateLed();       // -> readAdcAveraged() -> readP0Adc()
    }
});

Because the shim has no simulator implementation, gdk.readP0Adc is undefined in the browser, and the background loop throws the error above. This affects every project built for hw---gdk in the simulator, regardless of user code.

Fix

Give the shim a TypeScript fallback body, mirroring the existing pattern in libs/storage (storage::init), which uses a C++ shim with a TS body and no shims.d.ts:

  • Add libs/hw---gdk/analog.ts with //% shim=gdk::readP0Adc and a fallback body returning a full-battery reading (1023).
  • Remove the declaration-only libs/hw---gdk/shims.d.ts (it would otherwise duplicate the declaration).
  • Update libs/hw---gdk/pxt.json files accordingly.

On micro:bit V2 the native C++ SAADC read is still used and is unchanged; in the simulator the fallback runs, so updateLed() sees a healthy value (1023 > 700), the low-battery LED stays off, and nothing crashes.

namespace gdk {
    //% shim=gdk::readP0Adc
    export function readP0Adc(): number {
        return 1023;
    }
}

Test plan

  • Simulator with hw---gdk selected: the readP0Adc runtime error no longer occurs; battery LED indicator stays off
  • pxt.json parses; git diff --check clean
  • micro:bit V2 hardware: battery reading / low-battery LED behavior unchanged (C++ path untouched)

Notes

  • Device (C++) code path is untouched; this is additive for the simulator only.
  • Pattern reference: libs/storage/storage.ts + libs/storage/storage.cpp.

The Game Designer's Kit battery monitor (batteryled.ts) calls the native
gdk.readP0Adc() SAADC shim on a background loop. That shim has no
simulator implementation, so running any project with hw---gdk selected
throws 'Cannot read properties of undefined (reading readP0Adc)' in the
simulator a couple of seconds after start.

Provide a TypeScript fallback body for the shim (as libs/storage does for
storage::init): on micro:bit V2 the C++ SAADC read in analog.cpp is still
used; in the simulator the fallback returns a full-battery reading (1023)
so the battery monitor and games run without error. Replace the
declaration-only shims.d.ts with analog.ts and update pxt.json.

No device behavior change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a simulator-only crash for the hw---gdk hardware variant by providing a TypeScript fallback implementation for the native-only gdk.readP0Adc() SAADC shim, ensuring projects can run in the browser simulator without undefined shim errors while leaving device (C++) behavior unchanged.

Changes:

  • Added libs/hw---gdk/analog.ts implementing gdk.readP0Adc() with a simulator fallback body returning 1023, while still mapping to the native shim via //% shim=gdk::readP0Adc.
  • Removed the declaration-only libs/hw---gdk/shims.d.ts to avoid duplicate/conflicting declarations now that the shim is defined in TypeScript.
  • Updated libs/hw---gdk/pxt.json to include the new analog.ts and drop shims.d.ts.
Show a summary per file
File Description
libs/hw---gdk/shims.d.ts Removes the declaration-only shim definition so it doesn’t conflict with the new TS-backed shim implementation.
libs/hw---gdk/pxt.json Registers analog.ts in the package and removes shims.d.ts from the build inputs.
libs/hw---gdk/analog.ts Adds a TS shim definition with a simulator fallback body to prevent readP0Adc from being undefined in the browser.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants