|
1 | | -# Simple FancyLED example for Circuit Playground Express |
| 1 | +""" Simple FancyLED example for Circuit Playground Express |
| 2 | +""" |
2 | 3 |
|
3 | 4 | from adafruit_circuitplayground.express import cpx |
4 | 5 | import adafruit_fancyled as fancy |
5 | 6 |
|
| 7 | +# Function names are kept the same as FastLED examples, which normally |
| 8 | +# upsets pylint. Disable name-checking so this passes muster. |
| 9 | +# pylint: disable=invalid-name |
| 10 | + |
6 | 11 | # A dynamic gradient palette is a compact representation of a color palette |
7 | 12 | # that lists only the key points (specific positions and colors), which are |
8 | 13 | # later interpolated to produce a full 'normal' color palette. |
9 | 14 | # This one happens to be a blackbody spectrum, it ranges from black to red |
10 | 15 | # to yellow to white... |
11 | | -blackbody = bytes([ |
12 | | - 0, 0, 0, 0, |
13 | | - 85, 255, 0, 0, |
14 | | - 170, 255, 255, 0, |
15 | | - 255, 255, 255, 255 ]) |
| 16 | +BLACKBODY = bytes([ |
| 17 | + 0, 0, 0, 0, |
| 18 | + 85, 255, 0, 0, |
| 19 | + 170, 255, 255, 0, |
| 20 | + 255, 255, 255, 255]) |
16 | 21 |
|
17 | 22 | # Here's where we convert the dynamic gradient palette to a full normal |
18 | 23 | # palette. First we need a list to hold the resulting palette...it can be |
|
21 | 26 | # palettes always have 16, 32 or 256 entries...we can actually use whatever |
22 | 27 | # length we want in CircuitPython, but for the sake of consistency, let's |
23 | 28 | # make it a 16-element palette... |
24 | | -palette = [0] * 16 |
25 | | -fancy.loadDynamicGradientPalette(blackbody, palette) |
| 29 | +PALETTE = [0] * 16 |
| 30 | +fancy.loadDynamicGradientPalette(BLACKBODY, PALETTE) |
26 | 31 |
|
27 | 32 | # The dynamic gradient step is optional...some projects will just specify |
28 | 33 | # a whole color palette directly on their own, not expand it from bytes. |
29 | 34 |
|
30 | | -# This function fills the Circuit Playground Express NeoPixels from a |
31 | | -# color palette plus an offset to allow us to 'spin' the colors. In |
32 | | -# FancyLED (a la FastLED), palette indices are multiples of 16 (e.g. |
33 | | -# first palette entry is index 0, second is index 16, third is 32, etc) |
34 | | -# and indices between these values will interpolate color between the |
35 | | -# two nearest palette entries. |
36 | 35 | def FillLEDsFromPaletteColors(palette, offset): |
37 | | - for i in range(10): |
38 | | - # This looks up the color in the palette, scaling from |
39 | | - # 'palette space' (16 colors * 16 interp position = 256) |
40 | | - # to 'pixel space' (10 NeoPixels): |
41 | | - c = fancy.ColorFromPalette(palette, |
42 | | - int(i * len(palette) * 16 / 10 + offset), 255, True) |
43 | | - # Gamma correction gives more sensible-looking colors |
44 | | - cpx.pixels[i] = fancy.applyGamma_video(c) |
| 36 | + """ This function fills the Circuit Playground Express NeoPixels from a |
| 37 | + color palette plus an offset to allow us to 'spin' the colors. In |
| 38 | + FancyLED (a la FastLED), palette indices are multiples of 16 (e.g. |
| 39 | + first palette entry is index 0, second is index 16, third is 32, etc) |
| 40 | + and indices between these values will interpolate color between the |
| 41 | + two nearest palette entries. |
| 42 | + """ |
| 43 | + |
| 44 | + for i in range(10): |
| 45 | + # This looks up the color in the palette, scaling from |
| 46 | + # 'palette space' (16 colors * 16 interp position = 256) |
| 47 | + # to 'pixel space' (10 NeoPixels): |
| 48 | + color = fancy.ColorFromPalette(palette, int(i * len(palette) * 16 / 10 + offset), 255, True) |
| 49 | + # Gamma correction gives more sensible-looking colors |
| 50 | + cpx.pixels[i] = fancy.applyGamma_video(color) |
45 | 51 |
|
46 | 52 | # This is an offset (0-255) into the color palette to get it to 'spin' |
47 | | -adjust = 0 |
| 53 | +ADJUST = 0 |
48 | 54 |
|
49 | 55 | while True: |
50 | | - FillLEDsFromPaletteColors(palette, adjust) |
51 | | - adjust += 4 # Bigger number = faster spin |
52 | | - if adjust >= 256: adjust -= 256 |
| 56 | + FillLEDsFromPaletteColors(PALETTE, ADJUST) |
| 57 | + ADJUST += 4 # Bigger number = faster spin |
| 58 | + if ADJUST >= 256: |
| 59 | + ADJUST -= 256 |
| 60 | + |
| 61 | + |
| 62 | +# pylint: enable=invalid-name |
0 commit comments