pxt-calliope/libs/neopixel/neopixel.ts

119 lines
3.6 KiB
TypeScript
Raw Normal View History

2016-04-13 07:09:03 +02:00
/**
* Functions to operate NeoPixel strips.
*/
//% weight=5 color=#2699BF
2016-04-04 02:38:50 +02:00
namespace neopixel {
//% shim=sendBufferAsm
function sendBuffer(buf: Buffer, pin: DigitalPin) {
}
2016-04-13 07:09:03 +02:00
/**
* A NeoPixel strip
*/
export class Strip {
2016-04-04 02:38:50 +02:00
buf: Buffer;
pin: DigitalPin;
brightness: number;
/**
2016-04-13 07:09:03 +02:00
* Set give LED to a given color (range 0-255 for r, g, b)
2016-04-04 02:38:50 +02:00
*/
2016-04-13 07:09:03 +02:00
//% 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);
2016-04-04 02:38:50 +02:00
}
/**
2016-04-13 07:09:03 +02:00
* Send all the changes to the strip.
2016-04-04 02:38:50 +02:00
*/
2016-04-13 07:09:03 +02:00
//% blockId="neopixel_show" block="%strip|show" blockGap=8
//% weight=79
show() {
basic.pause(1)
sendBuffer(this.buf, this.pin);
2016-04-04 02:38:50 +02:00
}
/**
* Turn off all LEDs.
*/
2016-04-13 07:09:03 +02:00
//% blockId="neopixel_clear" block="%strip|clear"
//% weight=76
2016-04-04 02:38:50 +02:00
clear(): void {
this.buf.fill(0);
}
/**
2016-04-13 07:09:03 +02:00
* Gets the number of pixels declared on the strip
2016-04-04 02:38:50 +02:00
*/
2016-04-13 07:09:03 +02:00
//% blockId="neopixel_length" block="%strip|length" blockGap=8
//% weight=60
length() {
return this.buf.length / 3
2016-04-04 02:38:50 +02:00
}
/**
2016-04-13 07:09:03 +02:00
* Set the brightness of the strip, 0-255. eg: 255
2016-04-04 02:38:50 +02:00
*/
2016-04-13 07:09:03 +02:00
//% blockId="neopixel_set_brightness" block="%strip|set brightness %brightness" blockGap=8
//% weight=59
setBrigthness(brightness: number): void {
this.brightness = brightness;
2016-04-04 02:38:50 +02:00
}
2016-04-13 07:09:03 +02:00
/**
* 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)
2016-04-04 02:38:50 +02:00
}
/**
2016-04-13 07:09:03 +02:00
* Rotate LEDs forward.
* @params off number of pixels to rotate forward, eg: 1
2016-04-04 02:38:50 +02:00
*/
2016-04-13 07:09:03 +02:00
//% 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.
*/
setPin(pin: DigitalPin): void {
this.pin = pin;
pins.digitalWritePin(this.pin, 0)
basic.pause(50)
2016-04-04 02:38:50 +02:00
}
}
/**
* Create a new NeoPixel driver for `numleds` LEDs.
2016-04-13 07:09:03 +02:00
* @params pin the pin where the neopixel is connected.
2016-04-04 02:38:50 +02:00
* @params numleds number of leds in the strip, eg: 24,30,60,64
*/
2016-04-13 07:09:03 +02:00
//% blockId="neopixel_create" block="neopixel create|at pin %pin|with %numleds leds"
//% weight=90
export function create(pin: DigitalPin, numleds: number): Strip {
2016-04-04 02:38:50 +02:00
let strip = new Strip();
strip.buf = pins.createBuffer(numleds * 3);
strip.setBrigthness(255)
2016-04-13 07:09:03 +02:00
strip.setPin(pin)
2016-04-04 02:38:50 +02:00
return strip;
}
2016-04-13 07:09:03 +02:00
}