Compare commits
2 commits
a8a8819307
...
f9db5ca520
Author | SHA1 | Date | |
---|---|---|---|
f9db5ca520 | |||
a0adc777c6 |
2 changed files with 294 additions and 0 deletions
172
start_stop_animation.py
Normal file
172
start_stop_animation.py
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Waffel-buzzer 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
|
||||||
|
ANIMATION_STEP = 0.042
|
||||||
|
GPIO_PIN = 16
|
||||||
|
MAX_BRIGHTNESS = 255
|
||||||
|
ANIMATION_TIME = 100
|
||||||
|
TIME_EXTENDED = 7
|
||||||
|
DEBUG_MSG = True
|
||||||
|
BRIGHTNESS = 255
|
||||||
|
|
||||||
|
# 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 rainbow(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 waffle_counter():
|
||||||
|
"""
|
||||||
|
Running iprogress animation untile time up,
|
||||||
|
then signaling time reached
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
for j in range(255):
|
||||||
|
for pxl in range(NUM_PIXELS):
|
||||||
|
color = Color(255, 255, 255)
|
||||||
|
NeoPixel.setPixelColor(pxl, color)
|
||||||
|
NeoPixel.show()
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
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:
|
||||||
|
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)
|
||||||
|
waffle_counter()
|
||||||
|
|
||||||
|
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()
|
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