From eb45a76928b1d43ed24e92105e410f36edadca0f Mon Sep 17 00:00:00 2001 From: Galen Nickel Date: Tue, 27 Feb 2018 15:12:23 -0800 Subject: [PATCH] Example overrides for 'control' api docs (#352) * Add 'playSound' api docs * Example overrides for 'control' api docs --- docs/reference/control/assert.md | 12 ++++ .../reference/control/device-serial-number.md | 9 +++ docs/reference/control/on-event.md | 25 ++++++++ docs/reference/control/panic.md | 13 ++++ docs/reference/control/raise-event.md | 25 ++++++++ docs/reference/control/run-in-parallel.md | 61 +++++++++++++++++++ docs/reference/control/wait-micros.md | 17 ++++++ 7 files changed, 162 insertions(+) create mode 100644 docs/reference/control/assert.md create mode 100644 docs/reference/control/device-serial-number.md create mode 100644 docs/reference/control/on-event.md create mode 100644 docs/reference/control/panic.md create mode 100644 docs/reference/control/raise-event.md create mode 100644 docs/reference/control/run-in-parallel.md create mode 100644 docs/reference/control/wait-micros.md diff --git a/docs/reference/control/assert.md b/docs/reference/control/assert.md new file mode 100644 index 00000000..d7315b26 --- /dev/null +++ b/docs/reference/control/assert.md @@ -0,0 +1,12 @@ +# @extends + +## Example #example + +Stop the program if the gyro dectects an angle greater than 45 degrees. + +```blocks +forever(function () { + control.assert(sensors.gyro2.angle() > 45, 15) + pause(300) +}) +``` \ No newline at end of file diff --git a/docs/reference/control/device-serial-number.md b/docs/reference/control/device-serial-number.md new file mode 100644 index 00000000..9a4e4024 --- /dev/null +++ b/docs/reference/control/device-serial-number.md @@ -0,0 +1,9 @@ +# @extends + +## Example #example + +Log the device serial number to the console. + +```blocks +console.logValue("serialnumber", control.deviceSerialNumber()); +``` \ No newline at end of file diff --git a/docs/reference/control/on-event.md b/docs/reference/control/on-event.md new file mode 100644 index 00000000..01d6c860 --- /dev/null +++ b/docs/reference/control/on-event.md @@ -0,0 +1,25 @@ +# @extends + +# Example #example + +Register two events coming from source `22`. Make the brick status light up when +the events of `0` and `1` are _raised_. + +```blocks +const statusLighter = 22; + +control.runInParallel(() => { + for (let i = 0; i < 2; i++) { + pause(1000); + control.raiseEvent(statusLighter, i); + } +}) + +control.onEvent(statusLighter, 0, () => { + brick.setStatusLight(StatusLight.OrangePulse) +}) + +control.onEvent(statusLighter, 1, () => { + brick.setStatusLight(StatusLight.GreenPulse) +}) +``` \ No newline at end of file diff --git a/docs/reference/control/panic.md b/docs/reference/control/panic.md new file mode 100644 index 00000000..1d65ffd2 --- /dev/null +++ b/docs/reference/control/panic.md @@ -0,0 +1,13 @@ +# @extends + +## Example #example + +Send a 'code red' error that you created to the error display if the brick crashes into a wall. + +```blocks +let codeRed = 1 +let codeBlue = 2 +sensors.touch1.onEvent(ButtonEvent.Pressed, function () { + control.panic(codeRed) +}) +``` \ No newline at end of file diff --git a/docs/reference/control/raise-event.md b/docs/reference/control/raise-event.md new file mode 100644 index 00000000..01d6c860 --- /dev/null +++ b/docs/reference/control/raise-event.md @@ -0,0 +1,25 @@ +# @extends + +# Example #example + +Register two events coming from source `22`. Make the brick status light up when +the events of `0` and `1` are _raised_. + +```blocks +const statusLighter = 22; + +control.runInParallel(() => { + for (let i = 0; i < 2; i++) { + pause(1000); + control.raiseEvent(statusLighter, i); + } +}) + +control.onEvent(statusLighter, 0, () => { + brick.setStatusLight(StatusLight.OrangePulse) +}) + +control.onEvent(statusLighter, 1, () => { + brick.setStatusLight(StatusLight.GreenPulse) +}) +``` \ No newline at end of file diff --git a/docs/reference/control/run-in-parallel.md b/docs/reference/control/run-in-parallel.md new file mode 100644 index 00000000..8364930a --- /dev/null +++ b/docs/reference/control/run-in-parallel.md @@ -0,0 +1,61 @@ +# @extends + +## Separate tasks #tasks + +As an example, you could have a small task that checks the battery level and gives a warning when it drops below 15 percent. This is placed inside a ``||control:run in parallel||`` block: + +```block +let powerCheck = false; + +control.runInParallel(() => { + while (!powerCheck) { + if (brick.batteryLevel() <= 15) { + brick.setStatusLight(StatusLight.RedFlash) + powerCheck = true; + } else { + pause(5000); + } + } +}) +``` + +The code the main program just drives the brick in a constant pattern until the battery check in the parallel task says that the battery level is too low. + +```block +let powerCheck = false; +while (!powerCheck) { + motors.largeBC.tank(50, 50, 5, MoveUnit.Seconds) + motors.largeBC.steer(5, 50, 6, MoveUnit.Rotations) +} +motors.stopAll() +``` + +## #example + +Tank the brick in a pattern until the battery warning variable is set. Have a separate task check the battery level and set a warning variable when the level is below `5` percent. + +```blocks +let powerCheck = false; + +control.runInParallel(() => { + while (!powerCheck) { + if (brick.batteryLevel() < 5) { + powerCheck = true; + } else { + pause(5000); + } + } +}) + +while (!powerCheck) { + motors.largeBC.tank(20, 20, 5, MoveUnit.Seconds) + motors.largeBC.steer(15, 20, 6, MoveUnit.Rotations) + motors.largeBC.tank(40, 40, 5, MoveUnit.Seconds) + motors.largeBC.steer(-10, 20, 3, MoveUnit.Rotations) +} +motors.stopAll() +``` + +## See also #seealso + +[forever](/reference/loops/forever) \ No newline at end of file diff --git a/docs/reference/control/wait-micros.md b/docs/reference/control/wait-micros.md new file mode 100644 index 00000000..9e2a0ad3 --- /dev/null +++ b/docs/reference/control/wait-micros.md @@ -0,0 +1,17 @@ +# @extends + +## Example #example + +Use the a wait and the timer to generate a crazy number. + +```blocks +let crazy = 0 +for (let i = 0; i < 100; i++) { + control.waitMicros(100) + crazy = control.millis() + crazy += control.deviceSerialNumber() + if (crazy != 0) { + crazy = crazy / 1000000 + } +} +``` \ No newline at end of file