2024-08-21 14:32:40 +02:00
|
|
|
|
import time
|
|
|
|
|
import threading
|
|
|
|
|
from rpi_ws281x import PixelStrip, Color
|
|
|
|
|
import RPi.GPIO as GPIO
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
####################TO DO´s##########################
|
2024-08-21 14:32:40 +02:00
|
|
|
|
# 1. Multithread for separating blinker and animation
|
|
|
|
|
# 2. Button interrupt for longer animations (rainbow/police)
|
|
|
|
|
# 3. Optimize Police animation
|
|
|
|
|
####################################################
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
# LED strip configuration:
|
2024-08-06 23:04:18 +02:00
|
|
|
|
numpix = 20
|
|
|
|
|
segment = 4
|
|
|
|
|
delay = 0.15
|
|
|
|
|
brightness = 100
|
|
|
|
|
anzahl_animationen = 5
|
2024-08-21 14:32:40 +02:00
|
|
|
|
LED_PIN = 18
|
|
|
|
|
|
|
|
|
|
# Setup
|
|
|
|
|
strip_blinker = PixelStrip(numpix, LED_PIN, brightness=brightness)
|
|
|
|
|
strip_animation = PixelStrip(numpix, LED_PIN, brightness=brightness)
|
|
|
|
|
strip_blinker.begin()
|
|
|
|
|
strip_animation.begin()
|
2024-08-09 00:46:46 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
# Button and switch GPIO setup
|
|
|
|
|
button_pin = 18
|
|
|
|
|
toggle_switch_left_pin = 15
|
|
|
|
|
toggle_switch_right_pin = 14
|
|
|
|
|
|
2024-08-21 14:36:59 +02:00
|
|
|
|
# Cleanup GPIO to avoid "channel already in use" errors
|
|
|
|
|
GPIO.cleanup()
|
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
|
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
|
|
|
GPIO.setup(toggle_switch_left_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
|
|
|
|
GPIO.setup(toggle_switch_right_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-09 00:46:46 +02:00
|
|
|
|
blinker_links = False
|
|
|
|
|
blinker_rechts = False
|
|
|
|
|
animation_NR = 0
|
|
|
|
|
reset_animation = False
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
# Define colors
|
|
|
|
|
blank = Color(0, 0, 0)
|
|
|
|
|
white = Color(255, 255, 255)
|
|
|
|
|
red = Color(255, 0, 0)
|
|
|
|
|
orange = Color(255, 127, 0)
|
|
|
|
|
yellow = Color(255, 255, 0)
|
|
|
|
|
lime = Color(127, 255, 0)
|
|
|
|
|
green = Color(0, 255, 0)
|
|
|
|
|
teal = Color(0, 255, 127)
|
|
|
|
|
cyan = Color(0, 255, 255)
|
|
|
|
|
sky_blue = Color(0, 127, 255)
|
|
|
|
|
blue = Color(0, 0, 255)
|
|
|
|
|
indigo = Color(127, 0, 255)
|
|
|
|
|
magenta = Color(255, 0, 255)
|
|
|
|
|
pink = Color(255, 0, 127)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
colors = [red, orange, yellow, lime, green, teal, cyan, sky_blue, blue, indigo, magenta, pink]
|
|
|
|
|
|
|
|
|
|
############ Blinker Links ###################
|
|
|
|
|
def blink_links():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global blinker_links
|
2024-08-21 14:32:40 +02:00
|
|
|
|
while blinker_links:
|
|
|
|
|
print('BLINKER LINKS')
|
2024-08-06 23:04:18 +02:00
|
|
|
|
for x in reversed(range(segment)):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
if not blinker_links:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
break
|
2024-08-21 14:32:40 +02:00
|
|
|
|
strip_blinker.setPixelColor(x, yellow)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
strip_blinker.show()
|
2024-08-21 14:32:40 +02:00
|
|
|
|
time.sleep(delay)
|
|
|
|
|
if not blinker_links:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
break
|
2024-08-21 14:32:40 +02:00
|
|
|
|
time.sleep(2 * delay)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
for y in range(segment):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
strip_blinker.setPixelColor(y, blank)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
strip_blinker.show()
|
2024-08-09 00:46:46 +02:00
|
|
|
|
|
|
|
|
|
for y in range(segment):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
strip_blinker.setPixelColor(y, blank)
|
2024-08-09 00:46:46 +02:00
|
|
|
|
strip_blinker.show()
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
############ Blinker Rechts ##################
|
|
|
|
|
def blink_rechts():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global blinker_rechts
|
2024-08-06 23:04:18 +02:00
|
|
|
|
startpoint = numpix - segment
|
2024-08-21 14:32:40 +02:00
|
|
|
|
while blinker_rechts:
|
|
|
|
|
print('BLINKER RECHTS')
|
2024-08-06 23:04:18 +02:00
|
|
|
|
for x in range(segment):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
if not blinker_rechts:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
break
|
2024-08-21 14:32:40 +02:00
|
|
|
|
strip_blinker.setPixelColor(startpoint + x, yellow)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
strip_blinker.show()
|
2024-08-21 14:32:40 +02:00
|
|
|
|
time.sleep(delay)
|
|
|
|
|
if not blinker_rechts:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
break
|
2024-08-21 14:32:40 +02:00
|
|
|
|
time.sleep(2 * delay)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
for y in range(segment):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
strip_blinker.setPixelColor(startpoint + y, blank)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
strip_blinker.show()
|
2024-08-09 00:46:46 +02:00
|
|
|
|
|
|
|
|
|
for y in range(segment):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
strip_blinker.setPixelColor(startpoint + y, blank)
|
|
|
|
|
strip_blinker.show()
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
############ 0.Animation Licht aus ################
|
|
|
|
|
def licht_aus():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global reset_animation
|
2024-08-21 14:32:40 +02:00
|
|
|
|
for i in range(segment, numpix):
|
|
|
|
|
strip_animation.setPixelColor(i, blank)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
while not reset_animation:
|
|
|
|
|
time.sleep(delay)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
############### 1.Animation Licht an ###############
|
|
|
|
|
def fernlicht_an():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global reset_animation
|
2024-08-21 14:32:40 +02:00
|
|
|
|
for i in range(segment, numpix):
|
|
|
|
|
strip_animation.setPixelColor(i, white)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
while not reset_animation:
|
|
|
|
|
time.sleep(delay)
|
2024-08-09 00:46:46 +02:00
|
|
|
|
licht_aus()
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
############### 2.Animation Rainbow ###############
|
|
|
|
|
def rainbow():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global reset_animation
|
2024-08-06 23:04:18 +02:00
|
|
|
|
for i in range(len(colors)):
|
2024-08-21 14:32:40 +02:00
|
|
|
|
for j in range(segment, numpix):
|
|
|
|
|
strip_animation.setPixelColor(j, colors[i])
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
time.sleep(delay)
|
|
|
|
|
if reset_animation:
|
|
|
|
|
licht_aus()
|
|
|
|
|
return
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
############### 3.Animation Police ###############
|
|
|
|
|
def police():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global reset_animation
|
2024-08-21 14:32:40 +02:00
|
|
|
|
blue_segment = [4, 5, 6, 10, 11, 12]
|
|
|
|
|
red_segment = [7, 8, 9, 13, 14, 15]
|
|
|
|
|
for segment, color in [(blue_segment, blue), (red_segment, red)]:
|
|
|
|
|
for i in segment:
|
|
|
|
|
strip_animation.setPixelColor(i, color)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
|
|
|
|
|
time.sleep(delay * 2)
|
|
|
|
|
if reset_animation:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
licht_aus()
|
|
|
|
|
return
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
|
|
|
|
############### 4.Animation Kit ###############
|
|
|
|
|
def kit():
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global reset_animation
|
2024-08-21 14:32:40 +02:00
|
|
|
|
kit_delay = 0.01
|
2024-08-09 00:46:46 +02:00
|
|
|
|
strip_animation.show()
|
2024-08-21 14:32:40 +02:00
|
|
|
|
|
|
|
|
|
# Left to Right
|
|
|
|
|
for x in range(segment, numpix - 2):
|
|
|
|
|
strip_animation.setPixelColor(x + 1, red)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
strip_animation.setPixelColor(x, red)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
strip_animation.setPixelColor(x + 2, red)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
strip_animation.setPixelColor(x, blank)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.setPixelColor(x + 1, blank)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.setPixelColor(x + 2, blank)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
if reset_animation:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
licht_aus()
|
|
|
|
|
return
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
# Right to Left
|
|
|
|
|
for x in reversed(range(segment + 1, numpix - 2)):
|
|
|
|
|
strip_animation.setPixelColor(x + 1, red)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
strip_animation.setPixelColor(x, red)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
strip_animation.setPixelColor(x + 2, red)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
strip_animation.setPixelColor(x, blank)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.setPixelColor(x + 1, blank)
|
|
|
|
|
time.sleep(kit_delay)
|
|
|
|
|
strip_animation.setPixelColor(x + 2, blank)
|
|
|
|
|
strip_animation.show()
|
|
|
|
|
if reset_animation:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
licht_aus()
|
|
|
|
|
return
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
# INTERRUPT HANDLERS
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-09 00:46:46 +02:00
|
|
|
|
# Blinker Links
|
2024-08-21 14:32:40 +02:00
|
|
|
|
def switch_left_up(channel):
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global blinker_links, blinker_rechts
|
2024-08-21 14:32:40 +02:00
|
|
|
|
if blinker_rechts:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
blinker_rechts = False
|
2024-08-21 14:32:40 +02:00
|
|
|
|
if blinker_links:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
return
|
|
|
|
|
blinker_links = True
|
2024-08-21 14:32:40 +02:00
|
|
|
|
threading.Thread(target=blink_links).start()
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
def switch_left_down(channel):
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global blinker_links
|
|
|
|
|
blinker_links = False
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
GPIO.add_event_detect(toggle_switch_left_pin, GPIO.RISING, callback=switch_left_up)
|
|
|
|
|
GPIO.add_event_detect(toggle_switch_left_pin, GPIO.FALLING, callback=switch_left_down)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-09 00:46:46 +02:00
|
|
|
|
# Blinker Rechts
|
2024-08-21 14:32:40 +02:00
|
|
|
|
def switch_right_up(channel):
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global blinker_rechts, blinker_links
|
2024-08-21 14:32:40 +02:00
|
|
|
|
if blinker_links:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
blinker_links = False
|
2024-08-21 14:32:40 +02:00
|
|
|
|
if blinker_rechts:
|
2024-08-09 00:46:46 +02:00
|
|
|
|
return
|
|
|
|
|
blinker_rechts = True
|
2024-08-21 14:32:40 +02:00
|
|
|
|
threading.Thread(target=blink_rechts).start()
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
def switch_right_down(channel):
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global blinker_rechts
|
|
|
|
|
blinker_rechts = False
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:32:40 +02:00
|
|
|
|
GPIO.add_event_detect(toggle_switch_right_pin, GPIO.RISING, callback=switch_right_up)
|
|
|
|
|
GPIO.add_event_detect(toggle_switch_right_pin, GPIO.FALLING, callback=switch_right_down)
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-09 00:46:46 +02:00
|
|
|
|
# Button Interrupt
|
2024-08-21 14:32:40 +02:00
|
|
|
|
def button_pressed(channel):
|
2024-08-09 00:46:46 +02:00
|
|
|
|
global debouncing, animation_NR, reset_animation
|
|
|
|
|
animation_NR = (animation_NR + 1) % anzahl_animationen
|
|
|
|
|
reset_animation = True
|
2024-08-06 23:04:18 +02:00
|
|
|
|
|
2024-08-21 14:36:59 +02:00
|
|
|
|
GPIO.add_event_detect(button_pin, GPIO.FALLING, callback=button_pressed, bouncetime=200)
|
|
|
|
|
|
|
|
|
|
# Ensure GPIO cleanup on exit
|
|
|
|
|
import atexit
|
|
|
|
|
atexit.register(GPIO.cleanup)
|
|
|
|
|
|
|
|
|
|
# Main loop
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
if animation_NR == 0:
|
|
|
|
|
licht_aus()
|
|
|
|
|
elif animation_NR == 1:
|
|
|
|
|
fernlicht_an()
|
|
|
|
|
elif animation_NR == 2:
|
|
|
|
|
rainbow()
|
|
|
|
|
elif animation_NR == 3:
|
|
|
|
|
police()
|
|
|
|
|
elif animation_NR == 4:
|
|
|
|
|
kit()
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
GPIO.cleanup()
|