ported to CircuitPython

This commit is contained in:
Mikey Sklar 2018-02-16 20:05:50 -07:00
parent 53e0163a70
commit 52e4bec411
4 changed files with 273 additions and 0 deletions

View file

@ -0,0 +1,115 @@
#include <FastLED.h>
#define LED_PIN 1 // which pin your pixels are connected to
#define NUM_LEDS 78 // how many LEDs you have
#define BRIGHTNESS 200 // 0-255, higher number is brighter.
#define SATURATION 255 // 0-255, 0 is pure white, 255 is fully saturated color
#define SPEED 10 // How fast the colors move. Higher numbers = faster motion
#define STEPS 2 // How wide the bands of color are. 1 = more like a gradient, 10 = more like stripes
#define BUTTON_PIN 2 // button is connected to pin 2 and GND
#define COLOR_ORDER GRB // Try mixing up the letters (RGB, GBR, BRG, etc) for a whole new world of color combinations
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette;
CRGBPalette16 targetPalette( PartyColors_p );
TBlendType currentBlending;
int ledMode = 0;
//FastLED comes with several palettes pre-programmed. I like purple a LOT, and so I added a custom purple palette.
const TProgmemPalette16 PurpleColors_p PROGMEM =
{
CRGB::Purple,
CRGB::Purple,
CRGB::MidnightBlue,
CRGB::MidnightBlue,
CRGB::Purple,
CRGB::Purple,
CRGB::BlueViolet,
CRGB::BlueViolet,
CRGB::DarkTurquoise,
CRGB::DarkTurquoise,
CRGB::DarkBlue,
CRGB::DarkBlue,
CRGB::Purple,
CRGB::Purple,
CRGB::BlueViolet,
CRGB::BlueViolet
};
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
void setup() {
delay( 2000 ); // power-up safety delay
FastLED.addLeds<WS2812B, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentBlending =LINEARBLEND;
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
byte currKeyState = digitalRead(BUTTON_PIN);
if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
shortKeyPress();
}
prevKeyState = currKeyState;
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
switch (ledMode) {
case 0:
currentPalette = HeatColors_p; //Red & Yellow, Fire Colors
break;
case 1:
currentPalette = ForestColors_p; //Foresty greens and yellows
break;
case 2:
currentPalette = OceanColors_p; //Oceans are pretty and filled with mermaids
break;
case 3:
currentPalette = PurpleColors_p; //My custom palette from above
break;
case 4:
currentPalette = RainbowColors_p; //All the colors!
break;
case 5:
currentPalette = RainbowStripeColors_p; //Rainbow stripes
break;
case 6:
currentPalette = PartyColors_p; //All the colors except the greens, which make people look a bit washed out
break;
}
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / SPEED);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex) {
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, BRIGHTNESS, currentBlending);
colorIndex += STEPS;
}
}
void shortKeyPress() {
ledMode++;
if (ledMode > 6) {
ledMode=0;
}
}

View file

@ -0,0 +1,154 @@
import board
import neopixel
import time
from digitalio import DigitalInOut, Direction, Pull
import adafruit_fancyled.adafruit_fancyled as fancy
led_pin = board.D1 # which pin your pixels are connected to
num_leds = 78 # how many LEDs you have
brightness = 1.0 # 0-1, higher number is brighter
saturation = 255 # 0-255, 0 is pure white, 255 is fully saturated color
steps = 0.01 # how wide the bands of color are.
offset = 0 # cummulative steps
fadeup = True # start with fading up - increase steps until offset reaches 1
index = 8 # midway color selection
blend = True # color blending between palette indices
# initialize list with all pixels off
palette = [ 0 ] * num_leds
# Declare a NeoPixel object on led_pin with num_leds as pixels
# No auto-write.
# Set brightness to max.
# We will be using FancyLED's brightness control.
strip = neopixel.NeoPixel(led_pin, num_leds, brightness=1, auto_write=False)
# button setup
button = DigitalInOut(board.D2)
button.direction = Direction.INPUT
button.pull = Pull.UP
prevkeystate = False
ledmode = 0 # button press counter, switch color palettes
# FancyLED allows for assigning a color palette using these formats:
# * The first (5) palettes here are mixing between 2-elements
# * The last (3) palettes use a format identical to the FastLED Arduino Library
# see FastLED - colorpalettes.cpp
forest = [ fancy.CRGB( 0, 255, 0 ), # green
fancy.CRGB( 255, 255, 0) ] # yellow
ocean = [ fancy.CRGB( 0, 0, 255 ), # blue
fancy.CRGB( 0, 255, 0 ) ] # green
purple = [ fancy.CRGB( 160, 32, 240 ), # purple
fancy.CRGB( 238, 130, 238 ) ] # violet
all_colors = [ fancy.CRGB( 0, 0, 0 ), # black
fancy.CRGB( 255, 255, 255 ) ] # white
washed_out = [ fancy.CRGB( 0, 0, 0 ), # black
fancy.CRGB( 255, 0, 255 ) ] # purple
rainbow = [ 0xFF0000, 0xD52A00, 0xAB5500, 0xAB7F00,
0xABAB00, 0x56D500, 0x00FF00, 0x00D52A,
0x00AB55, 0x0056AA, 0x0000FF, 0x2A00D5,
0x5500AB, 0x7F0081, 0xAB0055, 0xD5002B ]
rainbow_stripe= [ 0xFF0000, 0x000000, 0xAB5500, 0x000000,
0xABAB00, 0x000000, 0x00FF00, 0x000000,
0x00AB55, 0x000000, 0x0000FF, 0x000000,
0x5500AB, 0x000000, 0xAB0055, 0x000000 ]
heat_colors = [ 0x330000, 0x660000, 0x990000, 0xCC0000, 0xFF0000,
0xFF3300, 0xFF6600, 0xFF9900, 0xFFCC00, 0xFFFF00,
0xFFFF33, 0xFFFF66, 0xFFFF99, 0xFFFFCC ]
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return (0, 0, 0)
if (pos < 85):
return (int(pos * 3), int(255 - (pos*3)), 0)
elif (pos < 170):
pos -= 85
return (int(255 - pos*3), 0, int(pos*3))
else:
pos -= 170
return (0, int(pos*3), int(255 - pos*3))
def remapRange(value, leftMin, leftMax, rightMin, rightMax):
# this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the 0-1 range into a value in the right range.
return int(rightMin + (valueScaled * rightSpan))
def shortkeypress(color_palette):
color_palette += 1
if ( color_palette > 6):
color_palette = 1
return ( color_palette )
while True:
# check for button press
currkeystate = button.value
# button press, move to next pattern
if ( ( prevkeystate == False ) and ( currkeystate == True ) ):
ledmode = shortkeypress(ledmode)
# save button press state
prevkeystate = currkeystate
# Fire Colors [ HEAT ]
if ( ledmode == 1 ):
palette = heat_colors
# Forest
elif ( ledmode == 2 ):
palette = forest
# Ocean
elif ( ledmode == 3 ):
palette = ocean
# Purple Lovers
elif ( ledmode == 4 ):
palette = purple
# All the colors!
elif ( ledmode == 5 ):
palette = rainbow
# Rainbow stripes
elif ( ledmode == 6 ):
palette = rainbow_stripe
# All the colors except the greens, washed out
elif ( ledmode == 7 ):
palette = washed_out
for i in range(num_leds):
color = fancy.palette_lookup(palette, offset + i / num_leds)
color = fancy.gamma_adjust(color, brightness=brightness)
strip[i] = color.pack()
strip.show()
if ( fadeup ):
offset += steps
if ( offset >= 1 ):
fadeup = False
else:
offset -= steps
if ( offset <= 0 ):
fadeup = True

View file

@ -0,0 +1,4 @@
# Animated_NeoPixel_Glow_Fur_Scarf
Code to accompany this tutorial:
https://learn.adafruit.com/animated-neopixel-gemma-glow-fur-scarf

Binary file not shown.