|
| 1 | +""" Simple FancyLED example for NeoPixel strip |
| 2 | +""" |
| 3 | + |
| 4 | +import board |
| 5 | +import neopixel |
| 6 | +import adafruit_fancyled as fancy |
| 7 | + |
| 8 | +# Function names are kept the same as FastLED examples, which normally |
| 9 | +# upsets pylint. Disable name-checking so this passes muster. |
| 10 | +# pylint: disable=invalid-name |
| 11 | + |
| 12 | +NUM_LEDS = 23 |
| 13 | + |
| 14 | +# A dynamic gradient palette is a compact representation of a color palette |
| 15 | +# that lists only the key points (specific positions and colors), which are |
| 16 | +# later interpolated to produce a full 'normal' color palette. |
| 17 | +# This one is an RGB spectrum -- red to yellow, green, blue, ... back to red. |
| 18 | +RAINBOW = bytes([ |
| 19 | + 0, 255, 0, 0, |
| 20 | + 32, 171, 85, 0, |
| 21 | + 64, 171, 171, 0, |
| 22 | + 96, 0, 255, 0, |
| 23 | + 128, 0, 171, 85, |
| 24 | + 160, 0, 0, 255, |
| 25 | + 192, 85, 0, 171, |
| 26 | + 224, 171, 0, 85, |
| 27 | + 255, 255, 0, 0]) |
| 28 | + |
| 29 | +# Here the gradient palette is converted to a full normal palette. |
| 30 | +# First we need a list to hold the resulting palette...it can be |
| 31 | +# filled with nonsense but the list length needs to match the desired |
| 32 | +# palette length (in FastLED, after which FancyLED is modeled, color |
| 33 | +# palettes always have 16, 32 or 256 entries...we can actually use whatever |
| 34 | +# length we want in CircuitPython, but for the sake of consistency, let's |
| 35 | +# make it a 256-element palette... |
| 36 | +PALETTE = [0] * 256 |
| 37 | +fancy.loadDynamicGradientPalette(RAINBOW, PALETTE) |
| 38 | + |
| 39 | +# The dynamic gradient step is optional...some projects will just specify |
| 40 | +# a whole color palette directly on their own, not expand it from bytes. |
| 41 | + |
| 42 | +# Declare a NeoPixel object on pin D6 with NUM_LEDS pixels, no auto-write |
| 43 | +PIXELS = neopixel.NeoPixel(board.D6, NUM_LEDS, brightness=1.0, |
| 44 | + auto_write=False) |
| 45 | + |
| 46 | +def FillLEDsFromPaletteColors(palette, offset): |
| 47 | + """ This function fills the global PIXELS NeoPixel strip from a color |
| 48 | + palette plus an offset to allow us to 'spin' the colors. In FancyLED |
| 49 | + (a la FastLED), palette indices are multiples of 16 (e.g. first palette |
| 50 | + entry is index 0, second is index 16, third is 32, etc) and indices |
| 51 | + between these values will interpolate color between the two nearest |
| 52 | + palette entries. |
| 53 | + """ |
| 54 | + |
| 55 | + for i in range(NUM_LEDS): |
| 56 | + # This looks up the color in the palette, scaling from |
| 57 | + # 'palette space' to 'pixel space': |
| 58 | + color = fancy.ColorFromPalette( |
| 59 | + palette, int(i * len(palette) * 16 / NUM_LEDS + offset), |
| 60 | + 255, True) |
| 61 | + # Gamma correction gives more sensible-looking colors |
| 62 | + PIXELS[i] = fancy.applyGamma_video(color) |
| 63 | + |
| 64 | +# This is an offset (0-4095) into the color palette to get it to 'spin' |
| 65 | +ADJUST = 0 |
| 66 | + |
| 67 | +while True: |
| 68 | + FillLEDsFromPaletteColors(PALETTE, ADJUST) |
| 69 | + PIXELS.show() |
| 70 | + ADJUST += 50 # Bigger number = faster spin |
| 71 | + if ADJUST >= 4096: |
| 72 | + ADJUST -= 4096 |
0 commit comments