Create start-stop animation

This commit is contained in:
L3D 2024-03-25 03:18:27 +01:00
parent a0adc777c6
commit f9db5ca520
No known key found for this signature in database
GPG key ID: AD65B920933B4B20
2 changed files with 200 additions and 28 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Example Rainbow Animation for Neopixel LED with GPIO Control Waffel-buzzer Animation for Neopixel LED with GPIO Control
""" """
import time import time
import sys import sys
@ -11,12 +11,13 @@ from rpi_ws281x import Adafruit_NeoPixel, Color
# NeoPixel configuration # NeoPixel configuration
PIN_NEO_PIXEL = 18 PIN_NEO_PIXEL = 18
NUM_PIXELS = 24 NUM_PIXELS = 24
DELAY_INTERVAL = 0.0042 ANIMATION_STEP = 0.042
GPIO_PIN = 16 GPIO_PIN = 16
MAX_BRIGHTNESS = 255 MAX_BRIGHTNESS = 255
ANIMATION_TIME = 23 ANIMATION_TIME = 100
TIME_EXTENDED = 10 TIME_EXTENDED = 7
DEBUG_MSG = True DEBUG_MSG = True
BRIGHTNESS = 255
# Setup GPIO # Setup GPIO
GPIO.setmode(GPIO.BCM) 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 = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL)
NeoPixel.begin() # Initialize NeoPixel strip object NeoPixel.begin() # Initialize NeoPixel strip object
def wheel(pos): def rainbow(pos):
"""Generate rainbow colors across 0-255 positions.""" """Generate rainbow colors across 0-255 positions."""
pos = pos % 256 # Ensure pos stays within 0-255 range pos = pos % 256 # Ensure pos stays within 0-255 range
if pos < 85: if pos < 85:
@ -37,48 +38,88 @@ def wheel(pos):
pos -= 85 pos -= 85
return Color(0, pos * 3, 255 - pos * 3) 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 print("Start Waffle Counter animation") if DEBUG_MSG else None
start_time = time.time() NeoPixel.setBrightness(BRIGHTNESS)
brightness = 0 # Initial brightness for pixel in range(NUM_PIXELS):
color = Color(255,255,0)
NeoPixel.setPixelColor(pixel, color)
NeoPixel.show()
start_time = time.time()
try: try:
while True: while True:
# Calculate elapsed time # Calculate elapsed time
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
print(f"Time: {int(elapsed_time)}") if DEBUG_MSG else None print(f"Time: {int(elapsed_time)}") if DEBUG_MSG else None
if elapsed_time >= ANIMATION_TIME: if elapsed_time >= ANIMATION_TIME:
"""
Running extended time animations
"""
# here we should inform our monitoring about a new waffle
# TODO
while True: while True:
"""
First make it bright white
"""
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
print(f"Time: {int(elapsed_time)} (extended)") if DEBUG_MSG else None 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 j in range(255):
for pxl in range(NUM_PIXELS): for pxl in range(NUM_PIXELS):
color = Color(255, 255, 255) color = Color(255, 255, 255)
NeoPixel.setPixelColor(pxl, color) NeoPixel.setPixelColor(pxl, color)
NeoPixel.show() 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: if GPIO.input(GPIO_PIN) == GPIO.LOW:
return 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 break # Exit if time elapsed
# Increase brightness gradually # Yellow to RED progress animation
brightness = int((elapsed_time / ANIMATION_TIME) * MAX_BRIGHTNESS) for position in range(int(NUM_PIXELS/2)):
NeoPixel.setBrightness(brightness) if GPIO.input(GPIO_PIN) == GPIO.LOW and int(elapsed_time) >= 5:
return
# Rainbow cycle animation # Check if GPIO is grounded to reset animation
for j in range(255): animation_time = time.time()
while time.time() <= animation_time + ANIMATION_STEP:
elapsed_time = time.time() - start_time
for pixel in range(NUM_PIXELS): for pixel in range(NUM_PIXELS):
color = wheel((pixel * 256 // NUM_PIXELS) + j) 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.setPixelColor(pixel, color)
NeoPixel.show() 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: if GPIO.input(GPIO_PIN) == GPIO.LOW and int(elapsed_time) >= 5:
return return
@ -101,9 +142,18 @@ def rainbow_animation():
try: try:
print("waiting input") if DEBUG_MSG else None print("waiting input") if DEBUG_MSG else None
while True: 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) GPIO.wait_for_edge(GPIO_PIN, GPIO.FALLING)
rainbow_animation() waffle_counter()
except KeyboardInterrupt: except KeyboardInterrupt:
print("exit waiting input") if DEBUG_MSG else None print("exit waiting input") if DEBUG_MSG else None

122
start_stop_rainbow.py Normal file
View 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()