Ported to Circuit Python, Tested on Gemma M0

This commit is contained in:
Mikey Sklar 2017-10-19 17:37:57 -06:00
parent 7f38c588df
commit 03ab613b7a
3 changed files with 213 additions and 0 deletions

View file

@ -0,0 +1,123 @@
#include <Adafruit_NeoPixel.h>
#define PIN 1
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
// Here is where you can put in your favorite colors that will appear!
// just add new {nnn, nnn, nnn}, lines. They will be picked out randomly
// R G B
uint8_t myColors[][3] = {{232, 100, 255}, // purple
{200, 200, 20}, // yellow
{30, 200, 200}, // blue
};
// don't edit the line below
#define FAVCOLORS sizeof(myColors) / 3
void setup() {
strip.begin();
strip.setBrightness(40);
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
flashRandom(5, 8); // first number is 'wait' delay, shorter num == shorter twinkle
flashRandom(5, 5); // second number is how many neopixels to simultaneously light up
flashRandom(5, 11);
colorWipe(strip.Color(232, 100, 255), 50); // Red
colorWipe(strip.Color(200, 200, 20), 50); // Green
colorWipe(strip.Color(30, 200, 200), 50); // Blue
rainbowCycle(20);
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void flashRandom(int wait, uint8_t howmany) {
for(uint16_t i=0; i<howmany; i++) {
// pick a random favorite color!
int c = random(FAVCOLORS);
int red = myColors[c][0];
int green = myColors[c][1];
int blue = myColors[c][2];
// get a random pixel from the list
int j = random(strip.numPixels());
// now we will 'fade' it in 5 steps
for (int x=0; x < 5; x++) {
int r = red * (x+1); r /= 5;
int g = green * (x+1); g /= 5;
int b = blue * (x+1); b /= 5;
strip.setPixelColor(j, strip.Color(r, g, b));
strip.show();
delay(wait);
}
// & fade out in 5 steps
for (int x=5; x >= 0; x--) {
int r = red * x; r /= 5;
int g = green * x; g /= 5;
int b = blue * x; b /= 5;
strip.setPixelColor(j, strip.Color(r, g, b));
strip.show();
delay(wait);
}
}
// LEDs will be off when done (they are faded to 0)
}

View file

@ -0,0 +1,90 @@
from digitalio import DigitalInOut, Direction
import board
import neopixel
import time
try:
import urandom as random
except ImportError:
import random
pixpin = board.D1
numpix = 16
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
strip = neopixel.NeoPixel(pixpin, numpix, brightness=.2, auto_write=True)
colors = [
[ 232, 100, 255 ], # Purple
[ 200, 200, 20 ], # Yellow
[ 30, 200, 200 ], # Blue
]
# Fill the dots one after the other with a color
def colorWipe(color, wait):
for j in range(len(strip)):
strip[j] = (color)
time.sleep(wait)
def rainbow(wait):
for j in range(255):
for i in range(len(strip)):
idx = int (i+j)
strip[i] = wheel(idx & 255)
# Slightly different, this makes the rainbow equally distributed throughout
def rainbow_cycle(wait):
for j in range(255*5):
for i in range(len(strip)):
idx = int ((i * 256 / len(strip)) + j)
strip[i] = wheel(idx & 255)
time.sleep(wait)
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
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 flash_random(wait,howmany):
for k in range(howmany):
c = random.randint(0, len(colors) - 1) # Choose random color index
j = random.randint(0, numpix - 1) # Choose random pixel
strip[j] = colors[c] # Set pixel to color
for i in range(1, 5):
strip.brightness = i / 5.0 # Ramp up brightness
time.sleep(wait)
for i in range(5, 0, -1):
strip.brightness = i / 5.0 # Ramp down brightness
strip[j] = [0,0,0] # Set pixel to 'off'
time.sleep(wait)
while True:
flash_random(.01, 8) # first number is 'wait' delay, shorter num == shorter twinkle
flash_random(.01, 5) # second number is how many neopixels to simultaneously light up
flash_random(.01, 11)
colorWipe( (232, 100, 255), .1 )
colorWipe( (200, 200, 20), .1 )
colorWipe( (30, 200, 200), .1)
rainbow_cycle(0.05)

Binary file not shown.