Change out more pixel examples (#1085)
* Change out more pixel examples * random type fix
This commit is contained in:
Родитель
b9e4938012
Коммит
d16351eca8
|
@ -25,12 +25,11 @@ input.onGesture(Gesture.Shake,() => {
|
|||
|
||||
## Example: random number #example
|
||||
|
||||
Show a random color when you shake the @boardname@.
|
||||
Log a message when you shake the @boardname@.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
input.onGesture(Gesture.Shake,() => {
|
||||
pixels.setAll(light.hsv(Math.randomRange(0, 256), 255, 127));
|
||||
input.onGesture(Gesture.Shake, function() {}
|
||||
console.log("I'm shaking!")
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -26,22 +26,20 @@ check the ways that the @boardname@ is moving.
|
|||
## Example: @boardname@ leveler #example
|
||||
|
||||
This program helps you move the @boardname@ until it is level. When
|
||||
it is levelled, the @boardname@ shows turns blue.
|
||||
it is leveled, the message "LEVELED" appears at the console.
|
||||
|
||||
```blocks
|
||||
let roll = 0
|
||||
let pitch = 0
|
||||
let pixels = light.createStrip();
|
||||
forever(() => {
|
||||
pitch = input.rotation(Rotation.Pitch)
|
||||
roll = input.rotation(Rotation.Roll)
|
||||
if (Math.abs(pitch) < 10 && Math.abs(roll) < 10) {
|
||||
pixels.setAll(0x0000ff)
|
||||
} else {
|
||||
pixels.setAll(0xff0000)
|
||||
console.log("LEVELED")
|
||||
}
|
||||
});
|
||||
})
|
||||
```
|
||||
|
||||
### ~hint
|
||||
**Simulator**
|
||||
|
||||
|
|
|
@ -229,8 +229,8 @@ namespace Buffer {
|
|||
|
||||
|
||||
/**
|
||||
* Create a new buffer initalized to bytes from given array.
|
||||
* @param bytes data to initalize with
|
||||
* Create a new buffer initialized to bytes from given array.
|
||||
* @param bytes data to initialize with
|
||||
*/
|
||||
export function fromArray(bytes: number[]) {
|
||||
let buf = Buffer.create(bytes.length)
|
||||
|
|
|
@ -26,17 +26,14 @@ Read about [**touch sensors**](/reference/input/button/touch-sensors) and using
|
|||
|
||||
## Example #example
|
||||
|
||||
Set all the pixels to green when button `A` is pressed. When the button is not pressed, the pixels are red.
|
||||
Log a message when button `A` is pressed.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
|
||||
forever(function() {
|
||||
if (input.buttonA.isPressed()) {
|
||||
pixels.setAll(0x00ff00);
|
||||
} else {
|
||||
pixels.setAll(0xff0000);
|
||||
console.log("Button A is pressed")
|
||||
}
|
||||
pause(300)
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -9,19 +9,20 @@ input.buttonA.onEvent(ButtonEvent.Click, () => {
|
|||
```
|
||||
|
||||
## ~hint
|
||||
|
||||
**Touch**
|
||||
|
||||
If your board has pins or pads that work as touch inputs, then your code can use them just like buttons.
|
||||
Instead of saying `button A` or `button B` as the input source, use a pin name like `pin A1`.
|
||||
|
||||
```block
|
||||
let pixels = light.createStrip();
|
||||
input.pinA1.onEvent(ButtonEvent.Down, function() {
|
||||
pixels.setPixelColor(1, 0x0000ff)
|
||||
console.log("Press down")
|
||||
})
|
||||
```
|
||||
|
||||
Read about [**touch sensors**](/reference/input/button/touch-sensors) and using the pins as touch buttons.
|
||||
|
||||
## ~
|
||||
|
||||
## Parameters
|
||||
|
@ -37,54 +38,26 @@ Read about [**touch sensors**](/reference/input/button/touch-sensors) and using
|
|||
|
||||
## Examples #example
|
||||
|
||||
### Next light please #ex1
|
||||
### Button release #ex1
|
||||
|
||||
In this example, the lighted pixel moves to the next pixel spot each time you press the `A` button. The position of
|
||||
the light goes back to first pixel when the current position reaches the last pixel.
|
||||
Wnen the ``B`` button is released, log a message.
|
||||
|
||||
```blocks
|
||||
let position = 0;
|
||||
let pixels = light.createStrip();
|
||||
|
||||
input.buttonA.onEvent(ButtonEvent.Click, function() {
|
||||
if (position > -1) {
|
||||
pixels.setPixelColor(position - 1, 0x000000);
|
||||
}
|
||||
if (position == pixels.length()) {
|
||||
position = 0;
|
||||
}
|
||||
pixels.setPixelColor(position, 0xff0000);
|
||||
position += 1;
|
||||
})
|
||||
```
|
||||
|
||||
### Any color, any pixel #ex2
|
||||
|
||||
Wnen the ``B`` button is released, light up a random pixel with a random color.
|
||||
|
||||
```blocks
|
||||
let anyPixel = 0;
|
||||
let pixels = light.createStrip();
|
||||
input.buttonB.onEvent(ButtonEvent.Up, function() {
|
||||
pixels.clear();
|
||||
anyPixel = Math.randomRange(0, pixels.length());
|
||||
pixels.setPixelColor(anyPixel, Math.randomRange(0, 0xffffff));
|
||||
console.log("Release button")
|
||||
})
|
||||
```
|
||||
|
||||
### Touch down #ex3
|
||||
### Touch down #ex2
|
||||
|
||||
Make a pixel turn `pink` when you touch the capacitive pin `pin A1` on the board. The pixel then turns
|
||||
`green` when you lift your finger off of the pin.
|
||||
Log a message when you touch or release the capacitive pin `pin A1` on the board.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
|
||||
input.pinA1.onEvent(ButtonEvent.Down, () => {
|
||||
pixels.setPixelColor(5, 0xff007f);
|
||||
input.pinA1.onEvent(ButtonEvent.Down, function() {
|
||||
console.log("Touch pin")
|
||||
})
|
||||
input.pinA1.onEvent(ButtonEvent.Up, () => {
|
||||
pixels.setPixelColor(5, 0x00ff00);
|
||||
input.pinA1.onEvent(ButtonEvent.Up, function() {
|
||||
console.log("Release pin")
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -31,15 +31,14 @@ Read about [**touch sensors**](/reference/input/button/touch-sensors) and using
|
|||
|
||||
## Example #example
|
||||
|
||||
Set all the pixels to green if button `A` was pressed before button `B`. If not, turn all pixels off when button `B`is pressed.
|
||||
Log a message telling whether button `A` or `B` was pressed.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
input.buttonB.onEvent(ButtonEvent.Click, function() {
|
||||
if (input.buttonA.wasPressed()) {
|
||||
pixels.setAll(0x00ff00)
|
||||
console.log("Button A Pressed")
|
||||
} else {
|
||||
pixels.setAll(0x000000)
|
||||
console.log("Button B Pressed")
|
||||
}
|
||||
})
|
||||
```
|
||||
|
|
|
@ -21,13 +21,11 @@ This function takes 1 argument:
|
|||
|
||||
## Example #example
|
||||
|
||||
Show the value of a number received from an cable data message. The number is shown by lighting the same number of pixels on the pixel strip.
|
||||
Log the value of a number received from an cable data message to the console.
|
||||
|
||||
```blocks
|
||||
let strip = light.createStrip();
|
||||
|
||||
network.onCableReceivedNumber(function (num) {
|
||||
strip.graph(num, 9);
|
||||
console.logValue("cable-value", num)
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -17,16 +17,12 @@ is something between `0` and `1023`. A `0` is no signal and `1023` is a full si
|
|||
|
||||
## Example #example
|
||||
|
||||
Use the pixel strip as a signal meter. Read from pin `A2` and display the value as a graph on the pixel
|
||||
strip. Also, output the value to the serial port.
|
||||
Read from pin `A2` and write the value to the console.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
|
||||
forever(function() {
|
||||
let signal = pins.A2.analogRead()
|
||||
pixels.graph(signal, 1023)
|
||||
serial.writeValue("signal", signal)
|
||||
console.log("signal", signal)
|
||||
pause(1000)
|
||||
})
|
||||
```
|
||||
|
|
|
@ -16,17 +16,15 @@ connected to the pin is **on** or has a status of `true`.
|
|||
|
||||
## Example #example
|
||||
|
||||
See if a switch on your bread board is on or off. The switch is connected to pin `D4`. If
|
||||
the switch is on, change the pixel at position `2` on the pixel strip to `green`.
|
||||
See if a switch on your bread board is on or off. The switch is connected to pin `D4`. Write the position of the switch to the console.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
let mySwitchOn = pins.D4.digitalRead();
|
||||
|
||||
if (mySwitchOn) {
|
||||
pixels.setPixelColor(2, 0x00ff00);
|
||||
console.log("Switch at D4 is ON")
|
||||
} else {
|
||||
pixels.setPixelColor(2, 0xff0000);
|
||||
console.log("Switch at D4 is OFF")
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -35,20 +35,16 @@ is in microseconds (1 second = 1000000 microseconds).
|
|||
|
||||
## Example #example
|
||||
|
||||
Check for a `low` pulse on pin `D5` every one-half of a second. Make the first pixel on the pixel strip `red`
|
||||
if there was a pulse.
|
||||
Check for a `low` pulse on pin `D5` every one-half of a second. Write to the console if there was a pulse.
|
||||
|
||||
```blocks
|
||||
let pulseTime = 0;
|
||||
let pixels = light.createStrip();
|
||||
pins.D5.setPull(PinPullMode.PullUp)
|
||||
|
||||
forever(function() {
|
||||
pulseTime = pins.D5.pulseIn(PulseValue.Low)
|
||||
if (pulseTime > 0) {
|
||||
pixels.setPixelColor(0, 0xff0000)
|
||||
} else {
|
||||
pixels.setPixelColor(0, 0x000000)
|
||||
console.logValue("pulse-time", pulseTime)
|
||||
}
|
||||
pause(500)
|
||||
})
|
||||
|
|
|
@ -21,14 +21,12 @@ This function has one argument:
|
|||
|
||||
## Example #example
|
||||
|
||||
Show the value of a number received from an infrared data message. The number is shown by lighting the same number of pixels on the pixel strip.
|
||||
Show the value of a number received from an infrared data message in the console log.
|
||||
|
||||
```blocks
|
||||
let strip = light.createStrip();
|
||||
|
||||
network.onInfraredReceivedNumber(function (num) {
|
||||
if (num > 0) {
|
||||
strip.graph(num, 9);
|
||||
console.log("Infrared msg: " + num)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
Do something when the slide switch is moved left or right.
|
||||
|
||||
```sig
|
||||
input.onSwitchMoved(SwitchDirection.Left, () => {
|
||||
input.onSwitchMoved(SwitchDirection.Left, function() {
|
||||
|
||||
})
|
||||
```
|
||||
|
@ -14,26 +14,15 @@ input.onSwitchMoved(SwitchDirection.Left, () => {
|
|||
|
||||
## Example #example
|
||||
|
||||
Use two ``||input:on switch moved||`` events for `left` and `right`. Make a photon move in opposite directions
|
||||
when the switch is moves from one side to the other.
|
||||
Use two ``||input:on switch moved||`` events for `left` and `right`. Log a message
|
||||
telling which position the switch is in.
|
||||
|
||||
```blocks
|
||||
let pixels = light.createStrip();
|
||||
|
||||
pixels.setAll(0xff0000);
|
||||
input.onSwitchMoved(SwitchDirection.Right, () => {
|
||||
for (let i = 0; i < pixels.length(); i++) {
|
||||
pixels.photonForward(1);
|
||||
pause(50);
|
||||
}
|
||||
pixels.photonFlip();
|
||||
});
|
||||
input.onSwitchMoved(SwitchDirection.Left, () => {
|
||||
for (let i = 0; i < pixels.length(); i++) {
|
||||
pixels.photonForward(1);
|
||||
pause(50);
|
||||
}
|
||||
pixels.photonFlip();
|
||||
input.onSwitchMoved(SwitchDirection.Right, function() {
|
||||
console.log("Switch Right")
|
||||
})
|
||||
input.onSwitchMoved(SwitchDirection.Left, function() {
|
||||
console.log("Switch Left")
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -15,12 +15,12 @@ Setting a higher threshold value makes it take longer for a touch event to happe
|
|||
|
||||
## Example #example
|
||||
|
||||
Set the touch threshold to `1000`. Test touch sensitivity by making all the pixels on the ring turn red when pin **A1** detects a touch.
|
||||
Set the touch threshold to `1000`. Log a message when a touch is a detected.
|
||||
|
||||
```blocks
|
||||
input.touchA1.setThreshold(1000);
|
||||
input.touchA1.onEvent(ButtonEvent.Click, function () {
|
||||
light.setAll(0xff0000);
|
||||
console.log("Touch pin presssed hard")
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -13,15 +13,15 @@ A pin detects that it was touched by measuring some amount of electrical charge
|
|||
|
||||
## Example #example
|
||||
|
||||
Measure the touch values at pin **A1**. If they are greater than `512`, then flash green light on the pixels.
|
||||
Measure the touch values at pin **A1**. If they are greater than `512`, then log
|
||||
them to the console.
|
||||
|
||||
```blocks
|
||||
input.touchA1.setThreshold(100)
|
||||
let touchValue = 0
|
||||
forever(function () {
|
||||
if (input.touchA1.value() > 512) {
|
||||
light.setAll(0x00ff00)
|
||||
pause(100)
|
||||
light.setAll(0x000000)
|
||||
touchValue = input.touchA1.value()
|
||||
if ( touchValue> 512) {
|
||||
console.logValue("touch-value", touchValue) {
|
||||
}
|
||||
pause(500)
|
||||
})
|
||||
|
|
Загрузка…
Ссылка в новой задаче