Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { schedule, setStatusLight } from "@devicescript/runtime" | ||
|
||
// counter increments on every invocation | ||
schedule( | ||
async ({ counter }) => { | ||
console.data({ counter }) | ||
// toggle between red and off | ||
await setStatusLight(counter % 2 === 0 ? 0xff0000 : 0x000000) | ||
}, | ||
{ | ||
// first delay | ||
timeout: 100, | ||
// repeated delay | ||
interval: 1000, | ||
} | ||
) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"label": "Runtime", | ||
"position": 12, | ||
"collapsible": true | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Runtime | ||
|
||
The `@devicescript/runtime` [builtin](/developer/packages) module provides additional runtime helpers. | ||
|
||
### `schedule` {#schedule} | ||
|
||
The schedule function combines `setTimeout` and `setInterval` to provide a single function that can be used to schedule a function to run once or repeatedly. | ||
The function also tracks the number of invocation, total elapsed time and time since last invocation. | ||
|
||
```ts | ||
import { schedule, setStatusLight } from "@devicescript/runtime" | ||
|
||
// counter increments on every invocation | ||
schedule( | ||
async ({ counter, elapsed, delta }) => { | ||
console.data({ counter }) | ||
// toggle between red and off | ||
await setStatusLight(counter % 2 === 0 ? 0xff0000 : 0x000000) | ||
}, | ||
{ | ||
// first delay | ||
timeout: 100, | ||
// repeated delay | ||
interval: 1000, | ||
} | ||
) | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
sidebar_position: 2.1 | ||
title: Schedule Blinky | ||
--- | ||
|
||
# Schedule Blinky | ||
|
||
This sample blinks the onboard LED using the [schedule](/api/runtime#schedule) | ||
|
||
```ts | ||
import { schedule, setStatusLight } from "@devicescript/runtime" | ||
|
||
// counter increments on every invocation | ||
schedule( | ||
async ({ counter, elapsed, delta }) => { | ||
console.data({ counter }) | ||
// toggle between red and off | ||
await setStatusLight(counter % 2 === 0 ? 0xff0000 : 0x000000) | ||
}, | ||
{ | ||
// first delay | ||
timeout: 100, | ||
// repeated delay | ||
interval: 1000, | ||
} | ||
) | ||
``` |