Giter Site home page Giter Site logo

pxt-package-template's Introduction

NeoPixel driver

This library provides a driver for various Neo Pixel LED strips, see https://www.adafruit.com/category/168

NeoPixels consist of a number of RGB LEDs, every one of them controlled separately.

Basic usage

// Create a NeoPixel driver - specify the pin, number of LEDs, and the type of 
// the NeoPixel srip, either standard RGB (with GRB or RGB format) or RGB+White.
let strip = neopixel.create(DigitalPin.P0, 24, NeoPixelMode.RGB)

// set pixel colors
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.show()

Use strip.setBrigthness() to lower the brightness (it's maxed out by default).

Use strip.shift() or strip.rotate() to shift the lights around.

Use strip.setPixelWhiteLED() to set brightness of the white pixel for RGB+W strips.

HSL color format

Use neopixel.hsl() to create colors using hue, saturation, and lightness.

Use .toRGB() on an HSL color to convert it into RGB format. All colors passed to strip.setPixelColor() or strip.showColor must be in RGB format.

For performance reasons, it is best to avoid making too many .toRGB() calls per second. For color animations, this can be avoided by only computing the set of colors in the animation once. See the HSL example below.

Example: Using accelerometer to control colors

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(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.setPixelColor(0, x, y, -z);
    strip.shift(1);
    strip.show();
    basic.pause(100);
}

Example: Using HSL to produce a rainbow animation

This program uses HSL colors to display an animated rainbow pattern along the first 12 NeoPixels.

// setup
let stripLen = 12;
let np = neopixel.create(DigitalPin.P0, stripLen, NeoPixelMode.RGB)
np.setBrigthness(10)

// compute colors in HSL, store colors in RGB
let colors: number[] = [];
let hStep = 360 / stripLen;
for (let i = 0; i < stripLen; i++) {
    let h = i * hStep;
    let hsl = neopixel.hsl(h, 100, 50);
    let rgb = hsl.toRGB();
    colors.push(rgb);
}

// set each pixel to a different color on the rainbow
for (let i = 0; i < stripLen; i++) {
    let clr = colors[i];
    np.setPixelColor(i, clr)
}

// animate the rainbow
while (true) {
    np.rotate(1);
    np.show();
    basic.pause(30)
}

Supported targets

License

MIT

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

pxt-package-template's People

Contributors

firemansamm avatar nioq avatar

Watchers

YJ Soon avatar  avatar James Cloos avatar Lho Chen Yang avatar Hongyi Zhang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.