diff --git a/libs/neopixel/README.md b/libs/neopixel/README.md new file mode 100644 index 00000000..a091dfa1 --- /dev/null +++ b/libs/neopixel/README.md @@ -0,0 +1,47 @@ +# 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 number of LEDs: +let strip = neopixel.create(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 + +// send the data to the strip +strip.display() +``` + +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. + +## 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(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.shift(1); + strip.display(); + basic.pause(100); +} +``` diff --git a/libs/neopixel/kind.json b/libs/neopixel/kind.json index 1192d770..2ed42160 100644 --- a/libs/neopixel/kind.json +++ b/libs/neopixel/kind.json @@ -1,5 +1,5 @@ { - "name": "neppixel", + "name": "neopixel", "description": "AdaFruit NeoPixel driver for micro:bit", "files": [ "neopixel.ts", diff --git a/libs/neopixel/neopixel.ts b/libs/neopixel/neopixel.ts index 96118f36..4d1b497d 100644 --- a/libs/neopixel/neopixel.ts +++ b/libs/neopixel/neopixel.ts @@ -39,7 +39,7 @@ namespace neopixel { /** * Shift LEDs forward. */ - shift(off: number): void { + shift(off: number = 1): void { this.buf.shift(-off * 3) } diff --git a/libs/neopixel/neotest.ts b/libs/neopixel/neotest.ts index bf3de988..96ac9307 100644 --- a/libs/neopixel/neotest.ts +++ b/libs/neopixel/neotest.ts @@ -1,92 +1,47 @@ -basic.showLeds(` - # . . . . - . . . . . - . . # . . - . . . . . - . . . . # - `) -console.log("Start") +let strip = neopixel.create(24); +let br = 100; +strip.setBrigthness(100); +input.onButtonPressed(Button.B, () => { + br = br + 20; + if (br > 255) { + br = 5; + } + strip.setBrigthness(br); +}); -// Create a NeoPixel driver - specify the number of LEDs: -let strip = neopixel.create(7); +let rotationMode = false; +input.onButtonPressed(Button.A, () => { + rotationMode = !rotationMode; + if (rotationMode) { + basic.showLeds(` + . # # # . + # . . . # + # . . . # + # . . . # + . # # # . + `); + } else { + basic.showLeds(` + . . # . . + . . . # . + # # # # # + . . . # . + . . # . . + `); -// If your strip is not at P0, specify the pin. -strip.setPin(DigitalPin.P0) + } +}); -// Brightness defaults to 255 (very bright); 0 is completely off. -strip.setBrigthness(20) - -// Set pixels - pixel number, followed by red, green and blue values, between 0 and 255. -strip.setPix(0, 255, 255, 255); -strip.setPix(1, 0, 255, 255); -strip.setPix(2, 255, 0, 255); -strip.setPix(3, 255, 255, 0); - -console.log("Send!") - -// Send the image to the strip. -strip.display(); -basic.pause(500); - -console.log("Sent!") - -// Green light travelling the strip: -for (let l = 0; l < strip.length(); l++) { - strip.clear(); - strip.setPix(l, 0, 255, 0); +while (true) { + let x = input.acceleration(Dimension.X) / 2 + let y = input.acceleration(Dimension.Y) / 2 + let z = input.acceleration(Dimension.Z) / 2 + if (rotationMode) { + strip.rotate(); + } else { + strip.setPix(0, x, y, -z); + strip.shift(1); + } strip.display(); basic.pause(100); } - -let special = false; -let numcol = 0; -input.onButtonPressed(Button.A, () => { - special = true; - let r = 0; - let g = 0; - let b = 0; - if (numcol == 0) { - r = 255; - g = 255; - b = 255; - } else if (numcol == 1) { - r = 255; - } else if (numcol == 2) { - b = 255; - } else if (numcol == 3) { - r = 255; - g = 255; - } else if (numcol == 4) { - r = 10; - g = 10; - b = 10; - } - numcol = (numcol + 1) % 5; - for (let k = 0; k < 7; k++) { - strip.setPix(k, r, g, b); - } - strip.display(); -}); - -control.inBackground(() => { - for (let j = 0; j < 2000000; j++) { - if (special) { - basic.pause(100); - continue; - } - let r1 = (0 + j * 2) & 63; - let g1 = (20 + j * 1) & 63; - let b1 = (30 + j * 3) & 63; - for (let i = 0; i < strip.length(); i++) { - strip.setPix(i, (r1 - i) * 20, (g1 - i) * 20, (b1 - i) * 20); - } - strip.display() - basic.pause(60); - } -}); - -control.inBackground(() => { - while (true) { - basic.showString("XMAS!", 150); - } -});