making neopixels block friendly

This commit is contained in:
Peli de Halleux 2016-04-12 22:09:03 -07:00
parent 9b46145391
commit 8e27d596aa
3 changed files with 101 additions and 72 deletions

View File

@ -8,22 +8,20 @@ separately.
## Basic usage
```
```blocks
// Create a NeoPixel driver - specify the number of LEDs:
let strip = neopixel.create(24)
let strip = neopixel.create(DigitalPin.P0, 24)
// set pixel colors
strip.setPix(0, 255, 255, 255) // white
strip.setPix(1, 255, 0, 0) // red
strip.setPix(2, 0, 255, 0) // green
strip.setPix(3, 0, 0, 255) // blue
strip.setPixelColor(0, 255, 255, 255) // white
strip.setPixelColor(1, 255, 0, 0) // red
strip.setPixelColor(2, 0, 255, 0) // green
strip.setPixelColor(3, 0, 0, 255) // blue
// send the data to the strip
strip.display()
strip.show()
```
Use `strip.setPin()` if your strip is not at `P0`.
Use `strip.setBrigthness()` to lower the brightness (it's maxed out by default).
Use `strip.shift()` or `strip.rotate()` to shift the lights around.
@ -33,15 +31,15 @@ Use `strip.shift()` or `strip.rotate()` to shift the lights around.
This little program will let the position of the microbit control the color of the first LED.
This first LED will then get shifted further away every 100ms.
```
let strip = neopixel.create(24)
```blocks
let strip = neopixel.create(DigitalPin.P0, 24)
while (true) {
let x = input.acceleration(Dimension.X) / 2
let y = input.acceleration(Dimension.Y) / 2
let z = input.acceleration(Dimension.Z) / 2
strip.setPix(0, x, y, -z);
strip.setPixelColor(0, x, y, -z);
strip.shift(1);
strip.display();
strip.show();
basic.pause(100);
}
```

View File

@ -1,25 +1,97 @@
/**
* Functions to operate NeoPixel strips.
*/
//% weight=5 color=#2699BF
namespace neopixel {
//% shim=sendBufferAsm
function sendBuffer(buf: Buffer, pin: DigitalPin) {
}
class Strip {
/**
* A NeoPixel strip
*/
export class Strip {
buf: Buffer;
pin: DigitalPin;
brightness: number;
/**
* Set give LED to a given color (range 0-255 for r, g, b)
*/
//% blockId="neopixel_set_pixel_color" block="%strip|set pixel color at %ledoff|red: %red|green: %green|blue: %blue" blockGap=8
//% weight=80
setPixelColor(ledoff: number, red: number, green: number, blue: number): void {
ledoff = ledoff * 3;
let br = this.brightness;
if (br < 255) {
red = (Math.clamp(0, 255, red) * br) >> 8;
green = (Math.clamp(0, 255, blue) * br) >> 8;
blue = (Math.clamp(0, 255, blue) * br) >> 8;
}
let buf = this.buf;
buf[ledoff + 0] = Math.clamp(0, 255, green);
buf[ledoff + 1] = Math.clamp(0, 255, red);
buf[ledoff + 2] = Math.clamp(0, 255, blue);
}
/**
* Send all the changes to the strip.
*/
//% blockId="neopixel_show" block="%strip|show" blockGap=8
//% weight=79
show() {
basic.pause(1)
sendBuffer(this.buf, this.pin);
}
/**
* Turn off all LEDs.
*/
//% blockId="neopixel_clear" block="%strip|clear"
//% weight=76
clear(): void {
this.buf.fill(0);
}
/**
* Gets the number of pixels declared on the strip
*/
//% blockId="neopixel_length" block="%strip|length" blockGap=8
//% weight=60
length() {
return this.buf.length / 3
}
/**
* Set the brightness of the strip, 0-255.
* Set the brightness of the strip, 0-255. eg: 255
*/
//% blockId="neopixel_set_brightness" block="%strip|set brightness %brightness" blockGap=8
//% weight=59
setBrigthness(brightness: number): void {
this.brightness = brightness;
}
/**
* Shift LEDs forward and clear with zeros.
* @params off number of pixels to shift forward, eg: 1
*/
//% blockId="neopixel_shift" block="%strip|shift pixels forward by %off" blockGap=8
//% weight=40
shift(off: number = 1): void {
this.buf.shift(-off * 3)
}
/**
* Rotate LEDs forward.
* @params off number of pixels to rotate forward, eg: 1
*/
//% blockId="neopixel_rotate" block="%strip|rotate pixels forward by %off" blockGap=8
//% weight=39
rotate(off:number = 1): void {
this.buf.rotate(-off * 3)
}
/**
* Set the pin where the neopixel is connected, defaults to P0.
*/
@ -28,61 +100,20 @@ namespace neopixel {
pins.digitalWritePin(this.pin, 0)
basic.pause(50)
}
/**
* Turn off all LEDs.
*/
clear(): void {
this.buf.fill(0);
}
/**
* Shift LEDs forward.
*/
shift(off: number = 1): void {
this.buf.shift(-off * 3)
}
/**
* Shift LEDs forward.
*/
rotate(): void {
this.buf.rotate(-3)
}
display() {
basic.pause(1)
sendBuffer(this.buf, this.pin);
}
/**
* Set give LED to a given color (range 0-255 for r, g, b)
*/
setPix(ledoff: number, r: number, g: number, b: number): void {
ledoff = ledoff * 3;
let br = this.brightness;
if (br < 255) {
r = (Math.clamp(0, 255, r) * br) >> 8;
g = (Math.clamp(0, 255, b) * br) >> 8;
b = (Math.clamp(0, 255, b) * br) >> 8;
}
let buf = this.buf;
buf[ledoff + 0] = Math.clamp(0, 255, g);
buf[ledoff + 1] = Math.clamp(0, 255, r);
buf[ledoff + 2] = Math.clamp(0, 255, b);
}
}
/**
* Create a new NeoPixel driver for `numleds` LEDs.
* @params pin the pin where the neopixel is connected.
* @params numleds number of leds in the strip, eg: 24,30,60,64
*/
export function create(numleds: number): Strip {
//% blockId="neopixel_create" block="neopixel create|at pin %pin|with %numleds leds"
//% weight=90
export function create(pin: DigitalPin, numleds: number): Strip {
let strip = new Strip();
strip.buf = pins.createBuffer(numleds * 3);
strip.setBrigthness(255)
strip.setPin(DigitalPin.P0)
strip.setPin(pin)
return strip;
}
}

View File

@ -1,4 +1,4 @@
let strip = neopixel.create(24);
let strip = neopixel.create(DigitalPin.P0, 24);
let br = 100;
strip.setBrigthness(100);
input.onButtonPressed(Button.B, () => {
@ -39,9 +39,9 @@ while (true) {
if (rotationMode) {
strip.rotate();
} else {
strip.setPix(0, x, y, -z);
strip.setPixelColor(0, x, y, -z);
strip.shift(1);
}
strip.display();
strip.show();
basic.pause(100);
}