ported to CircuitPython
This commit is contained in:
parent
334a27c36a
commit
68c7c42881
3 changed files with 184 additions and 0 deletions
95
Logans_Run_Hand_Jewel_LED/Logans_Run_Hand_Jewel_LED.ino
Normal file
95
Logans_Run_Hand_Jewel_LED/Logans_Run_Hand_Jewel_LED.ino
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#ifdef __AVR__
|
||||
#include <avr/power.h>
|
||||
#endif
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
#define PIXEL_PIN 4 // Pin connected to neo pixels
|
||||
#define PIXEL_COUNT 7 // Count of neo pixels
|
||||
|
||||
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
void setup() {
|
||||
|
||||
strip.begin();
|
||||
strip.show();
|
||||
|
||||
}
|
||||
|
||||
// Linear interpolation of y value given min/max x, min/max y, and x position.
|
||||
float lerp(float x, float x0, float x1, float y0, float y1)
|
||||
{
|
||||
// Clamp x within x0 and x1 bounds.
|
||||
x = x > x1 ? x1 : x;
|
||||
x = x < x0 ? x0 : x;
|
||||
// Calculate linear interpolation of y value.
|
||||
return y0 + (y1-y0)*((x-x0)/(x1-x0));
|
||||
}
|
||||
|
||||
// Set all pixels to the specified color.
|
||||
void fill_pixels(Adafruit_NeoPixel& pixels, uint32_t color)
|
||||
{
|
||||
for (int i=0; i < pixels.numPixels(); ++i) {
|
||||
pixels.setPixelColor(i, color);
|
||||
}
|
||||
strip.show();
|
||||
}
|
||||
|
||||
// Get the color of a pixel within a smooth gradient of two colors.
|
||||
uint32_t color_gradient(uint8_t start_r, // Starting R,G,B color
|
||||
uint8_t start_g,
|
||||
uint8_t start_b,
|
||||
uint8_t end_r, // Ending R,G,B color
|
||||
uint8_t end_g,
|
||||
uint8_t end_b,
|
||||
float pos) // Position along gradient, should be a value 0 to 1.0
|
||||
{
|
||||
// Interpolate R,G,B values and return them as a color.
|
||||
uint8_t red = (uint8_t) lerp(pos, 0.0, 1.0, start_r, end_r);
|
||||
uint8_t green = (uint8_t) lerp(pos, 0.0, 1.0, start_g, end_g);
|
||||
uint8_t blue = (uint8_t) lerp(pos, 0.0, 1.0, start_b, end_b);
|
||||
return Adafruit_NeoPixel::Color(red, green, blue);
|
||||
}
|
||||
|
||||
void animate_gradient_fill(Adafruit_NeoPixel& pixels, // NeoPixel strip/loop/etc.
|
||||
uint8_t start_r, // Starting R,G,B color
|
||||
uint8_t start_g,
|
||||
uint8_t start_b,
|
||||
uint8_t end_r, // Ending R,G,B color
|
||||
uint8_t end_g,
|
||||
uint8_t end_b,
|
||||
int duration_ms) // Total duration of animation, in milliseconds
|
||||
{
|
||||
unsigned long start = millis();
|
||||
// Display start color.
|
||||
fill_pixels(pixels, Adafruit_NeoPixel::Color(start_r, start_g, start_b));
|
||||
// Main animation loop.
|
||||
unsigned long delta = millis() - start;
|
||||
while (delta < duration_ms) {
|
||||
// Calculate how far along we are in the duration as a position 0...1.0
|
||||
float pos = (float)delta / (float)duration_ms;
|
||||
// Get the gradient color and fill all the pixels with it.
|
||||
uint32_t color = color_gradient(start_r, start_g, start_b, end_r, end_g, end_b, pos);
|
||||
fill_pixels(pixels, color);
|
||||
// Update delta and repeat.
|
||||
delta = millis() - start;
|
||||
}
|
||||
// Display end color.
|
||||
fill_pixels(pixels, Adafruit_NeoPixel::Color(end_r, end_g, end_b));
|
||||
}
|
||||
void loop() {
|
||||
|
||||
// Run It:
|
||||
|
||||
// Nice fade from dim red to full red for 1/2 of a second:
|
||||
animate_gradient_fill(strip,
|
||||
10, 0, 0,
|
||||
255, 0, 0,
|
||||
500);
|
||||
// Then fade from full red to dim red for 1/2 a second.
|
||||
animate_gradient_fill(strip,
|
||||
255, 0, 0,
|
||||
10, 0, 0,
|
||||
500);
|
||||
//delay(1000); Use this delay if using multiple color fades
|
||||
}
|
||||
85
Logans_Run_Hand_Jewel_LED/Logans_Run_Hand_Jewel_LED.py
Normal file
85
Logans_Run_Hand_Jewel_LED/Logans_Run_Hand_Jewel_LED.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import board
|
||||
import neopixel
|
||||
import time
|
||||
|
||||
pixpin = board.D1
|
||||
numpix = 7
|
||||
wait = .5 # 1/2 second color fade duration
|
||||
|
||||
# defaults to RGB|GRB Neopixels
|
||||
strip = neopixel.NeoPixel(pixpin, numpix, brightness=1, auto_write=False)
|
||||
|
||||
# uncomment the following line for RGBW Neopixels
|
||||
# strip = neopixel.NeoPixel(pixpin, numpix, bpp=4, brightness=.3, auto_write=True)
|
||||
|
||||
# Linear interpolation of y value given min/max x, min/max y, and x position.
|
||||
def lerp(x, x0, x1, y0, y1):
|
||||
|
||||
# Clamp x within x0 and x1 bounds.
|
||||
if ( x > x1 ):
|
||||
x = x1
|
||||
else:
|
||||
x = x
|
||||
|
||||
if ( x < x0 ):
|
||||
x = x0
|
||||
else:
|
||||
x = x
|
||||
|
||||
# Calculate linear interpolation of y value.
|
||||
return (y0 + (y1-y0) * ( (x-x0) / (x1-x0) ) )
|
||||
|
||||
# Set all pixels to the specified color.
|
||||
def fill_pixels(r, g, b):
|
||||
for i in range(0, numpix):
|
||||
strip[i] = (r, g, b)
|
||||
strip.write()
|
||||
|
||||
# Get the color of a pixel within a smooth gradient of two colors.
|
||||
# Starting R,G,B color
|
||||
# Ending R,G,B color
|
||||
# Position along gradient, should be a value 0 to 1.0
|
||||
def color_gradient(start_r, start_g, start_b, end_r,end_g, end_b, pos):
|
||||
|
||||
# Interpolate R,G,B values and return them as a color.
|
||||
red = lerp(pos, 0.0, 1.0, start_r, end_r)
|
||||
green = lerp(pos, 0.0, 1.0, start_g, end_g)
|
||||
blue = lerp(pos, 0.0, 1.0, start_b, end_b)
|
||||
|
||||
return (red, green, blue)
|
||||
|
||||
# Starting R,G,B color
|
||||
# Ending R,G,B color
|
||||
# Total duration of animation, in milliseconds
|
||||
def animate_gradient_fill(start_r, start_g, start_b, end_r, end_g, end_b, duration_ms):
|
||||
start = time.monotonic()
|
||||
|
||||
# Display start color.
|
||||
fill_pixels(start_r, start_g, start_b)
|
||||
|
||||
# Main animation loop.
|
||||
delta = time.monotonic() - start
|
||||
|
||||
while (delta < duration_ms):
|
||||
# Calculate how far along we are in the duration as a position 0...1.0
|
||||
pos = delta / duration_ms
|
||||
# Get the gradient color and fill all the pixels with it.
|
||||
color = color_gradient(start_r, start_g, start_b, end_r, end_g, end_b, pos)
|
||||
fill_pixels( int ( color[0]) , int ( color[1] ), int ( color[2] ) )
|
||||
# Update delta and repeat.
|
||||
delta = time.monotonic() - start
|
||||
|
||||
# Display end color.
|
||||
fill_pixels(end_r, end_g, end_b)
|
||||
|
||||
while True:
|
||||
|
||||
# Run It:
|
||||
|
||||
# Nice fade from dim red to full red for 1/2 of a second:
|
||||
animate_gradient_fill(10, 0, 0, 255, 0, 0, wait)
|
||||
|
||||
# Then fade from full red to dim red for 1/2 a second.
|
||||
animate_gradient_fill(255, 0, 0, 10, 0, 0, wait)
|
||||
|
||||
# time.sleep(1) # Use this delay if using multiple color fades
|
||||
4
Logans_Run_Hand_Jewel_LED/README.md
Normal file
4
Logans_Run_Hand_Jewel_LED/README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Logans_Run_Hand_Jewel_LED
|
||||
|
||||
Code to accompany this tutorial:
|
||||
https://learn.adafruit.com/led-in-you-hand-logans-run-life-clock
|
||||
Loading…
Reference in a new issue