/ Animated_NeoPixel_Glow_Fur_Scarf / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import adafruit_fancyled.adafruit_fancyled as fancy 6 import board 7 import neopixel 8 from digitalio import DigitalInOut, Direction, Pull 9 10 led_pin = board.D1 # which pin your pixels are connected to 11 num_leds = 78 # how many LEDs you have 12 brightness = 1.0 # 0-1, higher number is brighter 13 saturation = 255 # 0-255, 0 is pure white, 255 is fully saturated color 14 steps = 0.01 # how wide the bands of color are. 15 offset = 0 # cummulative steps 16 fadeup = True # start with fading up - increase steps until offset reaches 1 17 index = 8 # midway color selection 18 blend = True # color blending between palette indices 19 20 # initialize list with all pixels off 21 palette = [0] * num_leds 22 23 # Declare a NeoPixel object on led_pin with num_leds as pixels 24 # No auto-write. 25 # Set brightness to max. 26 # We will be using FancyLED's brightness control. 27 strip = neopixel.NeoPixel(led_pin, num_leds, brightness=1, auto_write=False) 28 29 # button setup 30 button = DigitalInOut(board.D2) 31 button.direction = Direction.INPUT 32 button.pull = Pull.UP 33 prevkeystate = False 34 ledmode = 0 # button press counter, switch color palettes 35 36 # FancyLED allows for assigning a color palette using these formats: 37 # * The first (5) palettes here are mixing between 2-elements 38 # * The last (3) palettes use a format identical to the FastLED Arduino Library 39 # see FastLED - colorpalettes.cpp 40 forest = [fancy.CRGB(0, 255, 0), # green 41 fancy.CRGB(255, 255, 0)] # yellow 42 43 ocean = [fancy.CRGB(0, 0, 255), # blue 44 fancy.CRGB(0, 255, 0)] # green 45 46 purple = [fancy.CRGB(160, 32, 240), # purple 47 fancy.CRGB(238, 130, 238)] # violet 48 49 all_colors = [fancy.CRGB(0, 0, 0), # black 50 fancy.CRGB(255, 255, 255)] # white 51 52 washed_out = [fancy.CRGB(0, 0, 0), # black 53 fancy.CRGB(255, 0, 255)] # purple 54 55 rainbow = [0xFF0000, 0xD52A00, 0xAB5500, 0xAB7F00, 56 0xABAB00, 0x56D500, 0x00FF00, 0x00D52A, 57 0x00AB55, 0x0056AA, 0x0000FF, 0x2A00D5, 58 0x5500AB, 0x7F0081, 0xAB0055, 0xD5002B] 59 60 rainbow_stripe = [0xFF0000, 0x000000, 0xAB5500, 0x000000, 61 0xABAB00, 0x000000, 0x00FF00, 0x000000, 62 0x00AB55, 0x000000, 0x0000FF, 0x000000, 63 0x5500AB, 0x000000, 0xAB0055, 0x000000] 64 65 heat_colors = [0x330000, 0x660000, 0x990000, 0xCC0000, 0xFF0000, 66 0xFF3300, 0xFF6600, 0xFF9900, 0xFFCC00, 0xFFFF00, 67 0xFFFF33, 0xFFFF66, 0xFFFF99, 0xFFFFCC] 68 69 70 def remapRange(value, leftMin, leftMax, rightMin, rightMax): 71 # this remaps a value fromhere original (left) range to new (right) range 72 # Figure out how 'wide' each range is 73 leftSpan = leftMax - leftMin 74 rightSpan = rightMax - rightMin 75 76 # Convert the left range into a 0-1 range (int) 77 valueScaled = int(value - leftMin) / int(leftSpan) 78 79 # Convert the 0-1 range into a value in the right range. 80 return int(rightMin + (valueScaled * rightSpan)) 81 82 83 def shortkeypress(color_palette): 84 color_palette += 1 85 86 if color_palette > 6: 87 color_palette = 1 88 89 return color_palette 90 91 92 while True: 93 94 # check for button press 95 currkeystate = button.value 96 97 # button press, move to next pattern 98 if (prevkeystate is not True) and currkeystate: 99 ledmode = shortkeypress(ledmode) 100 101 # save button press state 102 prevkeystate = currkeystate 103 104 # Fire Colors [ HEAT ] 105 if ledmode == 1: 106 palette = heat_colors 107 108 # Forest 109 elif ledmode == 2: 110 palette = forest 111 112 # Ocean 113 elif ledmode == 3: 114 palette = ocean 115 116 # Purple Lovers 117 elif ledmode == 4: 118 palette = purple 119 120 # All the colors! 121 elif ledmode == 5: 122 palette = rainbow 123 124 # Rainbow stripes 125 elif ledmode == 6: 126 palette = rainbow_stripe 127 128 # All the colors except the greens, washed out 129 elif ledmode == 7: 130 palette = washed_out 131 132 for i in range(num_leds): 133 color = fancy.palette_lookup(palette, offset + i / num_leds) 134 color = fancy.gamma_adjust(color, brightness=brightness) 135 strip[i] = color.pack() 136 strip.show() 137 138 if fadeup: 139 offset += steps 140 if offset >= 1: 141 fadeup = False 142 else: 143 offset -= steps 144 if offset <= 0: 145 fadeup = True