Fix hw---gdk simulator crash: add readP0Adc simulator fallback - #7641
Open
GarbhitSh wants to merge 4 commits into
Open
Fix hw---gdk simulator crash: add readP0Adc simulator fallback#7641GarbhitSh wants to merge 4 commits into
GarbhitSh wants to merge 4 commits into
Conversation
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.
Abhijith Chatra (abchatra)
requested review from
Joey Wunderlich (jwunderl) and
Richard Knoll (riknoll)
and
a lite review from Copilot
August 19, 2026 23:20
Copilot started reviewing on behalf of
Abhijith Chatra (abchatra)
August 19, 2026 23:21
View session
Contributor
There was a problem hiding this comment.
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.tsimplementinggdk.readP0Adc()with a simulator fallback body returning1023, while still mapping to the native shim via//% shim=gdk::readP0Adc. - Removed the declaration-only
libs/hw---gdk/shims.d.tsto avoid duplicate/conflicting declarations now that the shim is defined in TypeScript. - Updated
libs/hw---gdk/pxt.jsonto include the newanalog.tsand dropshims.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
Abhijith Chatra (abchatra)
approved these changes
Aug 24, 2026
Abhijith Chatra (abchatra)
enabled auto-merge
August 24, 2026 17:14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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-onlyshims.d.ts:The battery-low monitor in
batteryled.tscalls it from a background loop:Because the shim has no simulator implementation,
gdk.readP0Adcisundefinedin the browser, and the background loop throws the error above. This affects every project built forhw---gdkin 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 noshims.d.ts:libs/hw---gdk/analog.tswith//% shim=gdk::readP0Adcand a fallback body returning a full-battery reading (1023).libs/hw---gdk/shims.d.ts(it would otherwise duplicate the declaration).libs/hw---gdk/pxt.jsonfilesaccordingly.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.Test plan
hw---gdkselected: thereadP0Adcruntime error no longer occurs; battery LED indicator stays offpxt.jsonparses;git diff --checkcleanNotes
libs/storage/storage.ts+libs/storage/storage.cpp.