From e41bbeeaa6562ed12559ade5bc415e146e594954 Mon Sep 17 00:00:00 2001 From: garbhitsh Date: Mon, 10 Aug 2026 18:34:41 +0530 Subject: [PATCH] Fix hw---gdk simulator crash: add readP0Adc simulator fallback 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. --- libs/hw---gdk/analog.ts | 15 +++++++++++++++ libs/hw---gdk/pxt.json | 2 +- libs/hw---gdk/shims.d.ts | 22 ---------------------- 3 files changed, 16 insertions(+), 23 deletions(-) create mode 100644 libs/hw---gdk/analog.ts delete mode 100644 libs/hw---gdk/shims.d.ts diff --git a/libs/hw---gdk/analog.ts b/libs/hw---gdk/analog.ts new file mode 100644 index 0000000000..9a5ea0408e --- /dev/null +++ b/libs/hw---gdk/analog.ts @@ -0,0 +1,15 @@ +namespace gdk { + /** + * Blocking single-shot ADC read of P0 (P0.02 / AIN0). Returns 0..1023 (same + * scale as pins.analogReadPin), or -1 on hardware timeout. + * + * On micro:bit V2 the native SAADC implementation in analog.cpp is used. + * The body below is the simulator fallback: there is no SAADC in the + * simulator, so it reports a full battery (1023) to keep the battery + * monitor and any game running without a runtime error. + */ + //% shim=gdk::readP0Adc + export function readP0Adc(): number { + return 1023; + } +} diff --git a/libs/hw---gdk/pxt.json b/libs/hw---gdk/pxt.json index 4e69a8afbd..d49b48e17c 100644 --- a/libs/hw---gdk/pxt.json +++ b/libs/hw---gdk/pxt.json @@ -4,9 +4,9 @@ "files": [ "config.ts", "init.ts", + "analog.ts", "batteryled.ts", "device.d.ts", - "shims.d.ts", "analog.cpp" ], "card": { diff --git a/libs/hw---gdk/shims.d.ts b/libs/hw---gdk/shims.d.ts deleted file mode 100644 index 99c7b5341c..0000000000 --- a/libs/hw---gdk/shims.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -// Auto-generated. Do not edit. -declare namespace gdk { - - /** - * Blocking single-shot ADC read of P0 (P0.02 / AIN0). Returns 0..1023 (same - * scale as pins.analogReadPin), or -1 on hardware timeout. - * - * Absolute internal 0.6V reference + gain 1/6 => full scale 3.6V (VDD-independent, - * so the reading stays correct as the battery/VDD drains). 40us acquisition suits - * the on-board ~82k divider. - * - * Owns the SAADC exclusively for the ~50us the conversion takes, then fully - * disables it. It does NOT touch PPI/timers/IRQs, so it cannot disturb the - * display or audio. The complete manual sequence is: - * ENABLE -> START -> (STARTED) -> SAMPLE -> (END) -> STOP -> (STOPPED) -> DISABLE - * The SAMPLE task is the critical step the previous version was missing. - */ - //% shim=gdk::readP0Adc - function readP0Adc(): int32; -} - -// Auto-generated. Do not edit. Really.