diff --git a/docs/reference/control.md b/docs/reference/control.md index e0fe97ae..f546edc8 100644 --- a/docs/reference/control.md +++ b/docs/reference/control.md @@ -7,8 +7,9 @@ control.inBackground(() => { }); control.reset(); +control.waitMicros(4); ``` ### See Also -[inBackground](/reference/control/in-background), [reset](/reference/control/reset) +[inBackground](/reference/control/in-background), [reset](/reference/control/reset), [wait-micros](/reference/control/wait-micros) diff --git a/docs/reference/control/wait-micros.md b/docs/reference/control/wait-micros.md new file mode 100644 index 00000000..9e4404e2 --- /dev/null +++ b/docs/reference/control/wait-micros.md @@ -0,0 +1,32 @@ +# WaitMicros + +Blocks the current fiber for the given amount of micro-seconds. + +```sig +control.waitMicros(4) +``` + +### Example + +This program sends a 10 micro-second HIGH pulse through pin ``P0``. + +```blocks +// ensure pin is low to send a clean pulse +pins.digitalWritePin(DigitalPin.P0, 0) +control.waitMicros(2) +// set pin to 1 and wait 10 micros +pins.digitalWritePin(DigitalPin.P0, 1) +control.waitMicros(10) +// finish pulse +pins.digitalWritePin(DigitalPin.P0, 0) +``` + +#### ~hint + +This function is not supported in the simulator. + +#### ~ + +### See Also + +[pause](/reference/basic/pause) diff --git a/libs/microbit/control.cpp b/libs/microbit/control.cpp index ed8479a5..0869c2a2 100644 --- a/libs/microbit/control.cpp +++ b/libs/microbit/control.cpp @@ -129,12 +129,22 @@ namespace control { /** * Resets the BBC micro:bit. */ - //% weight=30 async help=control/reset + //% weight=30 async help=control/reset blockGap=8 //% blockId="control_reset" block="reset" void reset() { microbit_reset(); } + /** + * Blocks the current fiber for the given microseconds + * @param micros number of micro-seconds to wait. eg: 4 + */ + //% help=control/wait-micros weight=29 + //% blockId="control_wait_us" block="wait (µs)%micros" + void waitMicros(int micros) { + wait_us(micros); + } + /** * Raises an event in the event bus. * @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A. diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index 9046d51c..f5fc5727 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -333,10 +333,18 @@ declare namespace control { /** * Resets the BBC micro:bit. */ - //% weight=30 async help=control/reset + //% weight=30 async help=control/reset blockGap=8 //% blockId="control_reset" block="reset" shim=control::reset function reset(): void; + /** + * Blocks the current fiber for the given microseconds + * @param micros number of micro-seconds to wait. eg: 4 + */ + //% help=control/wait-micros weight=29 + //% blockId="control_wait_us" block="wait (µs)%micros" shim=control::waitMicros + function waitMicros(micros: number): void; + /** * Raises an event in the event bus. * @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A. diff --git a/sim/libmbit.ts b/sim/libmbit.ts index 64bae051..333f63f1 100644 --- a/sim/libmbit.ts +++ b/sim/libmbit.ts @@ -227,6 +227,10 @@ namespace pxsim.control { U.userError("reset not implemented in simulator yet") } + export function waitMicros(micros: number) { + // TODO + } + export function deviceName(): string { let b = board(); return b && b.id