Create start-stop animation
This commit is contained in:
parent
a0adc777c6
commit
f9db5ca520
2 changed files with 200 additions and 28 deletions
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Example Rainbow Animation for Neopixel LED with GPIO Control
|
||||
Waffel-buzzer Animation for Neopixel LED with GPIO Control
|
||||
"""
|
||||
import time
|
||||
import sys
|
||||
|
@ -11,12 +11,13 @@ from rpi_ws281x import Adafruit_NeoPixel, Color
|
|||
# NeoPixel configuration
|
||||
PIN_NEO_PIXEL = 18
|
||||
NUM_PIXELS = 24
|
||||
DELAY_INTERVAL = 0.0042
|
||||
ANIMATION_STEP = 0.042
|
||||
GPIO_PIN = 16
|
||||
MAX_BRIGHTNESS = 255
|
||||
ANIMATION_TIME = 23
|
||||
TIME_EXTENDED = 10
|
||||
ANIMATION_TIME = 100
|
||||
TIME_EXTENDED = 7
|
||||
DEBUG_MSG = True
|
||||
BRIGHTNESS = 255
|
||||
|
||||
# Setup GPIO
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
@ -26,7 +27,7 @@ GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|||
NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL)
|
||||
NeoPixel.begin() # Initialize NeoPixel strip object
|
||||
|
||||
def wheel(pos):
|
||||
def rainbow(pos):
|
||||
"""Generate rainbow colors across 0-255 positions."""
|
||||
pos = pos % 256 # Ensure pos stays within 0-255 range
|
||||
if pos < 85:
|
||||
|
@ -37,51 +38,91 @@ def wheel(pos):
|
|||
pos -= 85
|
||||
return Color(0, pos * 3, 255 - pos * 3)
|
||||
|
||||
def rainbow_animation():
|
||||
def waffle_counter():
|
||||
"""
|
||||
Running Rainbow animation, defined in wheel
|
||||
Running iprogress animation untile time up,
|
||||
then signaling time reached
|
||||
"""
|
||||
print("Start animation") if DEBUG_MSG else None
|
||||
start_time = time.time()
|
||||
brightness = 0 # Initial brightness
|
||||
print("Start Waffle Counter animation") if DEBUG_MSG else None
|
||||
NeoPixel.setBrightness(BRIGHTNESS)
|
||||
for pixel in range(NUM_PIXELS):
|
||||
color = Color(255,255,0)
|
||||
NeoPixel.setPixelColor(pixel, color)
|
||||
NeoPixel.show()
|
||||
|
||||
start_time = time.time()
|
||||
try:
|
||||
while True:
|
||||
# Calculate elapsed time
|
||||
elapsed_time = time.time() - start_time
|
||||
print(f"Time: {int(elapsed_time)}") if DEBUG_MSG else None
|
||||
if elapsed_time >= ANIMATION_TIME:
|
||||
"""
|
||||
Running extended time animations
|
||||
"""
|
||||
# here we should inform our monitoring about a new waffle
|
||||
# TODO
|
||||
while True:
|
||||
"""
|
||||
First make it bright white
|
||||
"""
|
||||
elapsed_time = time.time() - start_time
|
||||
print(f"Time: {int(elapsed_time)} (extended)") if DEBUG_MSG else None
|
||||
if elapsed_time >= int(ANIMATION_TIME + TIME_EXTENDED):
|
||||
break
|
||||
for j in range(255):
|
||||
for pxl in range(NUM_PIXELS):
|
||||
color = Color(255, 255, 255)
|
||||
NeoPixel.setPixelColor(pxl, color)
|
||||
NeoPixel.show()
|
||||
# Check if GPIO16 is grounded to reset animation
|
||||
# Check if GPIO is grounded to reset animation
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW:
|
||||
return
|
||||
if elapsed_time >= int(ANIMATION_TIME + TIME_EXTENDED):
|
||||
"""
|
||||
Run super extended animation
|
||||
"""
|
||||
while True:
|
||||
elapsed_time = time.time() - start_time
|
||||
print(f"Time: {int(elapsed_time)} (extended++)") if DEBUG_MSG else None
|
||||
for nbr in range(255):
|
||||
for p1xl in range(NUM_PIXELS):
|
||||
color = rainbow((p1xl * 256 // NUM_PIXELS) + nbr)
|
||||
NeoPixel.setPixelColor(p1xl, color)
|
||||
NeoPixel.show()
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW:
|
||||
return
|
||||
time.sleep(0.0023)
|
||||
# Decrease brightness gradually
|
||||
progress_time=(elapsed_time - (ANIMATION_TIME + TIME_EXTENDED))
|
||||
progress_calc=((progress_time / (TIME_EXTENDED * 5) ) * BRIGHTNESS)
|
||||
brightness = int(BRIGHTNESS - progress_calc)
|
||||
NeoPixel.setBrightness(brightness)
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW:
|
||||
return
|
||||
if elapsed_time >= int(ANIMATION_TIME + int(TIME_EXTENDED * 5)):
|
||||
print("extended++ Animation ended") if DEBUG_MSG else None
|
||||
break
|
||||
break
|
||||
break # Exit if time elapsed
|
||||
|
||||
# Increase brightness gradually
|
||||
brightness = int((elapsed_time / ANIMATION_TIME) * MAX_BRIGHTNESS)
|
||||
NeoPixel.setBrightness(brightness)
|
||||
|
||||
# Rainbow cycle animation
|
||||
for j in range(255):
|
||||
for pixel in range(NUM_PIXELS):
|
||||
color = wheel((pixel * 256 // NUM_PIXELS) + j)
|
||||
NeoPixel.setPixelColor(pixel, color)
|
||||
NeoPixel.show()
|
||||
time.sleep(DELAY_INTERVAL)
|
||||
|
||||
# Check if GPIO16 is grounded to reset animation
|
||||
# Yellow to RED progress animation
|
||||
for position in range(int(NUM_PIXELS/2)):
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW and int(elapsed_time) >= 5:
|
||||
return
|
||||
|
||||
# Check if GPIO is grounded to reset animation
|
||||
animation_time = time.time()
|
||||
while time.time() <= animation_time + ANIMATION_STEP:
|
||||
elapsed_time = time.time() - start_time
|
||||
for pixel in range(NUM_PIXELS):
|
||||
if pixel == position or pixel == int(position + NUM_PIXELS/2):
|
||||
color = Color(0, 255, 0)
|
||||
else:
|
||||
color = Color(128, int(128-(int(elapsed_time)/ANIMATION_TIME*128)), 0)
|
||||
NeoPixel.setPixelColor(pixel, color)
|
||||
NeoPixel.show()
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW and int(elapsed_time) >= 5:
|
||||
return
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("exit animation") if DEBUG_MSG else None
|
||||
NeoPixel.setBrightness(0) # Reset brightness
|
||||
|
@ -101,9 +142,18 @@ def rainbow_animation():
|
|||
try:
|
||||
print("waiting input") if DEBUG_MSG else None
|
||||
while True:
|
||||
# Wait for GPIO16 to be grounded
|
||||
print("waiting for input...") if DEBUG_MSG else None
|
||||
NeoPixel.setBrightness(BRIGHTNESS) # Reset brightness
|
||||
for pixel in range(NUM_PIXELS):
|
||||
if pixel in (3,4,11,12,19,20):
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 0, 5))
|
||||
else:
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
|
||||
NeoPixel.show()
|
||||
|
||||
# Wait for GPIO to be grounded
|
||||
GPIO.wait_for_edge(GPIO_PIN, GPIO.FALLING)
|
||||
rainbow_animation()
|
||||
waffle_counter()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("exit waiting input") if DEBUG_MSG else None
|
||||
|
|
122
start_stop_rainbow.py
Normal file
122
start_stop_rainbow.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Example Rainbow Animation for Neopixel LED with GPIO Control
|
||||
"""
|
||||
import time
|
||||
import sys
|
||||
from RPi import GPIO
|
||||
from rpi_ws281x import Adafruit_NeoPixel, Color
|
||||
|
||||
# NeoPixel configuration
|
||||
PIN_NEO_PIXEL = 18
|
||||
NUM_PIXELS = 24
|
||||
DELAY_INTERVAL = 0.0042
|
||||
GPIO_PIN = 16
|
||||
MAX_BRIGHTNESS = 255
|
||||
ANIMATION_TIME = 23
|
||||
TIME_EXTENDED = 10
|
||||
DEBUG_MSG = True
|
||||
|
||||
# Setup GPIO
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
|
||||
# Create NeoPixel object
|
||||
NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL)
|
||||
NeoPixel.begin() # Initialize NeoPixel strip object
|
||||
|
||||
def wheel(pos):
|
||||
"""Generate rainbow colors across 0-255 positions."""
|
||||
pos = pos % 256 # Ensure pos stays within 0-255 range
|
||||
if pos < 85:
|
||||
return Color(pos * 3, 255 - pos * 3, 0)
|
||||
pos -= 85
|
||||
if pos < 85:
|
||||
return Color(255 - pos * 3, 0, pos * 3)
|
||||
pos -= 85
|
||||
return Color(0, pos * 3, 255 - pos * 3)
|
||||
|
||||
def rainbow_animation():
|
||||
"""
|
||||
Running Rainbow animation, defined in wheel
|
||||
"""
|
||||
print("Start animation") if DEBUG_MSG else None
|
||||
start_time = time.time()
|
||||
brightness = 0 # Initial brightness
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Calculate elapsed time
|
||||
elapsed_time = time.time() - start_time
|
||||
print(f"Time: {int(elapsed_time)}") if DEBUG_MSG else None
|
||||
if elapsed_time >= ANIMATION_TIME:
|
||||
while True:
|
||||
elapsed_time = time.time() - start_time
|
||||
print(f"Time: {int(elapsed_time)} (extended)") if DEBUG_MSG else None
|
||||
if elapsed_time >= int(ANIMATION_TIME + TIME_EXTENDED):
|
||||
break
|
||||
for j in range(255):
|
||||
for pxl in range(NUM_PIXELS):
|
||||
color = Color(255, 255, 255)
|
||||
NeoPixel.setPixelColor(pxl, color)
|
||||
NeoPixel.show()
|
||||
# Check if GPIO16 is grounded to reset animation
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW:
|
||||
return
|
||||
break # Exit if time elapsed
|
||||
|
||||
# Increase brightness gradually
|
||||
brightness = int((elapsed_time / ANIMATION_TIME) * MAX_BRIGHTNESS)
|
||||
NeoPixel.setBrightness(brightness)
|
||||
|
||||
# Rainbow cycle animation
|
||||
for j in range(255):
|
||||
for pixel in range(NUM_PIXELS):
|
||||
color = wheel((pixel * 256 // NUM_PIXELS) + j)
|
||||
NeoPixel.setPixelColor(pixel, color)
|
||||
NeoPixel.show()
|
||||
time.sleep(DELAY_INTERVAL)
|
||||
|
||||
# Check if GPIO16 is grounded to reset animation
|
||||
if GPIO.input(GPIO_PIN) == GPIO.LOW and int(elapsed_time) >= 5:
|
||||
return
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("exit animation") if DEBUG_MSG else None
|
||||
NeoPixel.setBrightness(0) # Reset brightness
|
||||
for pixel in range(NUM_PIXELS):
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
|
||||
NeoPixel.show()
|
||||
sys.exit()
|
||||
|
||||
finally:
|
||||
print("end animation") if DEBUG_MSG else None
|
||||
# Clean up code before exiting the script
|
||||
NeoPixel.setBrightness(0) # Reset brightness
|
||||
for pixel in range(NUM_PIXELS):
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
|
||||
NeoPixel.show()
|
||||
|
||||
try:
|
||||
print("waiting input") if DEBUG_MSG else None
|
||||
while True:
|
||||
# Wait for GPIO16 to be grounded
|
||||
GPIO.wait_for_edge(GPIO_PIN, GPIO.FALLING)
|
||||
rainbow_animation()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("exit waiting input") if DEBUG_MSG else None
|
||||
NeoPixel.setBrightness(0) # Reset brightness
|
||||
for pixel in range(NUM_PIXELS):
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
|
||||
NeoPixel.show()
|
||||
sys.exit()
|
||||
|
||||
finally:
|
||||
NeoPixel.setBrightness(0) # Reset brightness
|
||||
for pixel in range(NUM_PIXELS):
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
|
||||
NeoPixel.show()
|
||||
GPIO.cleanup()
|
||||
sys.exit()
|
Loading…
Reference in a new issue