diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 9b296a2d54b..e7cd982dd77 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -324,6 +324,8 @@ "control.raiseEvent|param|src": "ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.", "control.raiseEvent|param|value": "Component specific code indicating the cause of the event.", "control.ramSize": "Returns estimated size of memory in bytes.", + "control.rawOnEvent": "Registers an event handler.", + "control.rawUnregisterEvent": "Unregisters an event handler.", "control.reset": "Resets the BBC micro:bit.", "control.runInParallel": "Run other code in the parallel.", "control.runtimeWarning": "Display warning in the simulator.", diff --git a/libs/core/codal.cpp b/libs/core/codal.cpp index b1d3dc2b9df..b8ce34da6dd 100644 --- a/libs/core/codal.cpp +++ b/libs/core/codal.cpp @@ -102,6 +102,16 @@ void registerWithDal(int id, int event, Action a, int flags) { registerGCPtr(a); } +// relies on making sure Action a is kept alive in STS +void rawRegisterWithDal(int id, int event, Action a, int flags) { + uBit.messageBus.listen(id, event, dispatchForeground, a, (uint16_t) flags); +} + +void rawUnregisterWithDal(int id, int event) { + uBit.messageBus.ignore(id, event, dispatchForeground); +} + + void fiberDone(void *a) { decr((Action)a); unregisterGCPtr((Action)a); diff --git a/libs/core/control.cpp b/libs/core/control.cpp index 031c37e7b92..2d93c98c6d5 100644 --- a/libs/core/control.cpp +++ b/libs/core/control.cpp @@ -312,6 +312,23 @@ namespace control { registerWithDal(src, value, handler, (int)flags); } + /** + * Registers an event handler. + */ + //% + void rawOnEvent(int src, int value, Action handler, int flags = 0) { + if (!flags) flags = ::EventFlags::QueueIfBusy; + rawRegisterWithDal(src, value, handler, (int)flags); + } + + /** + * Unregisters an event handler. + */ + //% + void rawUnregisterEvent(int src, int value) { + rawUnregisterWithDal(src, value); + } + /** * Gets the value of the last event executed on the bus */ diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index 6d4cdaaa8ed..8baca0673ef 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -420,6 +420,18 @@ declare namespace control { //% blockExternalInputs=1 flags.defl=0 shim=control::onEvent function onEvent(src: int32, value: int32, handler: () => void, flags?: int32): void; + /** + * Registers an event handler. + */ + //% flags.defl=0 shim=control::rawOnEvent + function rawOnEvent(src: int32, value: int32, handler: () => void, flags?: int32): void; + + /** + * Unregisters an event handler. + */ + //% shim=control::rawUnregisterEvent + function rawUnregisterEvent(src: int32, value: int32): void; + /** * Gets the value of the last event executed on the bus */ diff --git a/sim/state/misc.ts b/sim/state/misc.ts index 83a9a662af4..128cb5da564 100644 --- a/sim/state/misc.ts +++ b/sim/state/misc.ts @@ -46,6 +46,21 @@ namespace pxsim.control { pxtcore.registerWithDal(id, evid, handler, flags) } + export function rawOnEvent(id: number, evid: number, handler: RefAction, flags: number) { + if (id == DAL.MICROBIT_ID_BUTTON_AB) { + const b = board().buttonPairState; + if (!b.usesButtonAB) { + b.usesButtonAB = true; + runtime.queueDisplayUpdate(); + } + } + pxtcore.registerWithDal(id, evid, handler, flags) + } + + export function rawUnregisterEvent(id: number, evid: number) { + // TODO + } + export function eventTimestamp() { return board().bus.getLastEventTime() } diff --git a/targetconfig.json b/targetconfig.json index 33ddcae1ed3..34b1dbd7702 100644 --- a/targetconfig.json +++ b/targetconfig.json @@ -9,7 +9,7 @@ "tags": [ "Lights and Display" ], "simx": { "sha": "0825d56f78528c57af100a96abdfe2162f59945a", - "devUrl": "http://localhost:3000" + "devUrl": "https://microbit-apps.github.io/display-shield/" } }, "microsoft/pxt-microturtle": {