Switch to normal Python
This commit is contained in:
parent
f6d1f4c399
commit
fcca0c4972
1 changed files with 139 additions and 285 deletions
394
main.py
394
main.py
|
@ -1,386 +1,240 @@
|
|||
from neopixel import Neopixel
|
||||
import utime
|
||||
from machine import Pin, Timer
|
||||
import _thread
|
||||
|
||||
|
||||
import time
|
||||
import threading
|
||||
from rpi_ws281x import PixelStrip, Color
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
####################TO DO´s##########################
|
||||
# 1. Multithread für trennung von blinker und animation
|
||||
# 2. buttoninterrupt für längere animationen (rainbow/police)
|
||||
# - https://www.elektronik-kompendium.de/sites/raspberry-pi/2612181.htm
|
||||
# 3. animation Police optimieren
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
########################################################
|
||||
# 1. Multithread for separating blinker and animation
|
||||
# 2. Button interrupt for longer animations (rainbow/police)
|
||||
# 3. Optimize Police animation
|
||||
####################################################
|
||||
|
||||
# LED strip configuration:
|
||||
numpix = 20
|
||||
segment = 4
|
||||
delay = 0.15
|
||||
brightness = 100
|
||||
anzahl_animationen = 5
|
||||
LED_PIN = 18
|
||||
|
||||
#setup
|
||||
strip_blinker = Neopixel(numpix, 0, 28, "RGB")
|
||||
strip_animation = Neopixel(numpix, 0, 28, "RGB")
|
||||
strip_animation.brightness(brightness)
|
||||
strip_blinker.brightness(brightness)
|
||||
button = Pin(18, Pin.IN, Pin.PULL_UP)
|
||||
toggle_switch_left = Pin(15, Pin.IN, Pin.PULL_DOWN)
|
||||
toggle_switch_right = Pin(14, Pin.IN, Pin.PULL_DOWN)
|
||||
# Setup
|
||||
strip_blinker = PixelStrip(numpix, LED_PIN, brightness=brightness)
|
||||
strip_animation = PixelStrip(numpix, LED_PIN, brightness=brightness)
|
||||
strip_blinker.begin()
|
||||
strip_animation.begin()
|
||||
|
||||
# Button and switch GPIO setup
|
||||
button_pin = 18
|
||||
toggle_switch_left_pin = 15
|
||||
toggle_switch_right_pin = 14
|
||||
|
||||
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)
|
||||
|
||||
blinker_links = False
|
||||
blinker_rechts = False
|
||||
|
||||
animation_NR = 0
|
||||
# Define the debounce timer
|
||||
debounce_timer = Timer()
|
||||
|
||||
# Flag to indicate if the button is being debounced
|
||||
debouncing = False
|
||||
|
||||
reset_animation = False
|
||||
|
||||
|
||||
#Farben definnieren
|
||||
blank = (0, 0, 0)
|
||||
white = (255, 255, 255)
|
||||
|
||||
red = (0, 255, 0)
|
||||
orange = (127, 255, 0)
|
||||
yellow = (255, 255, 0)
|
||||
lime = (255, 127, 0)
|
||||
green = (255, 0, 0)
|
||||
teal = (255, 0, 127)
|
||||
cyan = (255, 0, 255)
|
||||
sky_blue = (127, 0, 255)
|
||||
blue = (0, 0, 255)
|
||||
indigo = (0, 127, 255)
|
||||
magenta = (0, 255, 255)
|
||||
pink = (0, 255, 127)
|
||||
# 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)
|
||||
|
||||
colors = [red, orange, yellow, lime, green, teal, cyan, sky_blue, blue, indigo, magenta, pink]
|
||||
|
||||
|
||||
############ Blinker Links ###################
|
||||
def blink_links():
|
||||
global blinker_links
|
||||
while True:
|
||||
if blinker_links == False:
|
||||
break
|
||||
strip_blinker.show()
|
||||
|
||||
while blinker_links:
|
||||
print('BLINKER LINKS')
|
||||
for x in reversed(range(segment)):
|
||||
if blinker_links == False:
|
||||
if not blinker_links:
|
||||
break
|
||||
strip_blinker.set_pixel(x, yellow)
|
||||
strip_blinker.setPixelColor(x, yellow)
|
||||
strip_blinker.show()
|
||||
utime.sleep(delay)
|
||||
|
||||
if blinker_links == False:
|
||||
time.sleep(delay)
|
||||
if not blinker_links:
|
||||
break
|
||||
utime.sleep(2 * delay)
|
||||
time.sleep(2 * delay)
|
||||
for y in range(segment):
|
||||
strip_blinker.set_pixel(y, blank)
|
||||
strip_blinker.setPixelColor(y, blank)
|
||||
strip_blinker.show()
|
||||
|
||||
for y in range(segment):
|
||||
strip_blinker.set_pixel(y, blank)
|
||||
strip_blinker.setPixelColor(y, blank)
|
||||
strip_blinker.show()
|
||||
return
|
||||
|
||||
##################################################
|
||||
|
||||
|
||||
############ Blinker Rechts ##################
|
||||
def blink_rechts():
|
||||
global blinker_rechts
|
||||
startpoint = numpix - segment
|
||||
while True:
|
||||
if blinker_rechts == False:
|
||||
break
|
||||
|
||||
strip_blinker.show()
|
||||
while blinker_rechts:
|
||||
print('BLINKER RECHTS')
|
||||
for x in range(segment):
|
||||
if blinker_rechts == False:
|
||||
if not blinker_rechts:
|
||||
break
|
||||
strip_blinker.set_pixel(startpoint + x, yellow)
|
||||
strip_blinker.setPixelColor(startpoint + x, yellow)
|
||||
strip_blinker.show()
|
||||
utime.sleep(delay)
|
||||
if blinker_rechts == False:
|
||||
time.sleep(delay)
|
||||
if not blinker_rechts:
|
||||
break
|
||||
utime.sleep(2 * delay)
|
||||
time.sleep(2 * delay)
|
||||
for y in range(segment):
|
||||
strip_blinker.set_pixel(startpoint + y, blank)
|
||||
strip_blinker.setPixelColor(startpoint + y, blank)
|
||||
strip_blinker.show()
|
||||
|
||||
for y in range(segment):
|
||||
strip_blinker.set_pixel(startpoint + y, blank)
|
||||
strip_blinker.setPixelColor(startpoint + y, blank)
|
||||
strip_blinker.show()
|
||||
return
|
||||
|
||||
|
||||
##################################################
|
||||
|
||||
# ----------------Animationen------------------#
|
||||
|
||||
############ 0.Animation Licht aus ################
|
||||
def licht_aus():
|
||||
global reset_animation
|
||||
for i in range(numpix - segment):
|
||||
if i >= segment:
|
||||
strip_animation.set_pixel(i, blank)
|
||||
for i in range(segment, numpix):
|
||||
strip_animation.setPixelColor(i, blank)
|
||||
strip_animation.show()
|
||||
while reset_animation == False:
|
||||
utime.sleep(delay)
|
||||
|
||||
while not reset_animation:
|
||||
time.sleep(delay)
|
||||
|
||||
############### 1.Animation Licht an ###############
|
||||
def fernlicht_an():
|
||||
global reset_animation
|
||||
for i in range(numpix - segment):
|
||||
if i >= segment:
|
||||
strip_animation.set_pixel(i, white)
|
||||
for i in range(segment, numpix):
|
||||
strip_animation.setPixelColor(i, white)
|
||||
strip_animation.show()
|
||||
while reset_animation == False:
|
||||
utime.sleep(delay)
|
||||
while not reset_animation:
|
||||
time.sleep(delay)
|
||||
licht_aus()
|
||||
|
||||
|
||||
############### 2.Animation Rainbow ###############
|
||||
def rainbow():
|
||||
global reset_animation
|
||||
for i in range(len(colors)):
|
||||
for j in range(numpix - segment):
|
||||
if j >= segment:
|
||||
strip_animation.set_pixel(j, colors[i])
|
||||
for j in range(segment, numpix):
|
||||
strip_animation.setPixelColor(j, colors[i])
|
||||
strip_animation.show()
|
||||
utime.sleep(delay)
|
||||
if reset_animation == True:
|
||||
time.sleep(delay)
|
||||
if reset_animation:
|
||||
licht_aus()
|
||||
return
|
||||
|
||||
|
||||
############### 3.Animation Police ###############
|
||||
def police():
|
||||
global reset_animation
|
||||
|
||||
strip_animation.set_pixel(4, blue)
|
||||
strip_animation.set_pixel(5, blue)
|
||||
strip_animation.set_pixel(6, blue)
|
||||
|
||||
strip_animation.set_pixel(7, red)
|
||||
strip_animation.set_pixel(8, red)
|
||||
strip_animation.set_pixel(9, red)
|
||||
|
||||
strip_animation.set_pixel(10, blue)
|
||||
strip_animation.set_pixel(11, blue)
|
||||
strip_animation.set_pixel(12, blue)
|
||||
|
||||
strip_animation.set_pixel(13, red)
|
||||
strip_animation.set_pixel(14, red)
|
||||
strip_animation.set_pixel(15, red)
|
||||
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()
|
||||
|
||||
utime.sleep(delay * 2)
|
||||
if reset_animation == True:
|
||||
time.sleep(delay * 2)
|
||||
if reset_animation:
|
||||
licht_aus()
|
||||
return
|
||||
|
||||
strip_animation.set_pixel(4, red)
|
||||
strip_animation.set_pixel(5, red)
|
||||
strip_animation.set_pixel(6, red)
|
||||
|
||||
strip_animation.set_pixel(7, blue)
|
||||
strip_animation.set_pixel(8, blue)
|
||||
strip_animation.set_pixel(9, blue)
|
||||
|
||||
strip_animation.set_pixel(10, red)
|
||||
strip_animation.set_pixel(11, red)
|
||||
strip_animation.set_pixel(12, red)
|
||||
|
||||
strip_animation.set_pixel(13, blue)
|
||||
strip_animation.set_pixel(14, blue)
|
||||
strip_animation.set_pixel(15, blue)
|
||||
strip_animation.show()
|
||||
|
||||
utime.sleep(delay * 2)
|
||||
|
||||
|
||||
############### 4.Animation Kit ###############
|
||||
def kit():
|
||||
global reset_animation
|
||||
delay = 0.01
|
||||
kit_delay = 0.01
|
||||
strip_animation.show()
|
||||
|
||||
# Left to Right
|
||||
for x in range(segment, numpix - 2):
|
||||
strip_animation.setPixelColor(x + 1, red)
|
||||
time.sleep(kit_delay)
|
||||
strip_animation.show()
|
||||
############### Left to Right ###############
|
||||
for x in range(numpix - segment - 2):
|
||||
if x >= segment:
|
||||
strip_animation.set_pixel(x + 1, red)
|
||||
utime.sleep(delay)
|
||||
strip_animation.setPixelColor(x, red)
|
||||
time.sleep(kit_delay)
|
||||
strip_animation.show()
|
||||
strip_animation.set_pixel(x, red)
|
||||
utime.sleep(delay)
|
||||
strip_animation.setPixelColor(x + 2, red)
|
||||
time.sleep(kit_delay)
|
||||
strip_animation.show()
|
||||
strip_animation.set_pixel(x + 2, red)
|
||||
utime.sleep(delay)
|
||||
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()
|
||||
strip_animation.set_pixel(x, blank)
|
||||
utime.sleep(delay)
|
||||
strip_animation.set_pixel(x + 1, blank)
|
||||
utime.sleep(delay)
|
||||
strip_animation.set_pixel(x + 2, blank)
|
||||
strip_animation.show()
|
||||
if reset_animation == True:
|
||||
if reset_animation:
|
||||
licht_aus()
|
||||
return
|
||||
|
||||
############### Left to Right ###############
|
||||
for x in reversed(range(numpix - segment - 2)):
|
||||
if x > segment:
|
||||
strip_animation.set_pixel(x + 1, red)
|
||||
utime.sleep(delay)
|
||||
# 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.set_pixel(x, red)
|
||||
utime.sleep(delay)
|
||||
strip_animation.setPixelColor(x, red)
|
||||
time.sleep(kit_delay)
|
||||
strip_animation.show()
|
||||
strip_animation.set_pixel(x + 2, red)
|
||||
utime.sleep(delay)
|
||||
strip_animation.setPixelColor(x + 2, red)
|
||||
time.sleep(kit_delay)
|
||||
strip_animation.show()
|
||||
strip_animation.set_pixel(x, blank)
|
||||
utime.sleep(delay)
|
||||
strip_animation.set_pixel(x + 1, blank)
|
||||
utime.sleep(delay)
|
||||
strip_animation.set_pixel(x + 2, blank)
|
||||
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 == True:
|
||||
if reset_animation:
|
||||
licht_aus()
|
||||
return
|
||||
|
||||
|
||||
##################################################
|
||||
# -----------------------------------------------#
|
||||
|
||||
|
||||
button = Pin(18, Pin.IN, Pin.PULL_UP)
|
||||
|
||||
|
||||
###########################################################################
|
||||
|
||||
# INTERRUPT HANDLER
|
||||
# INTERRUPT HANDLERS
|
||||
|
||||
# Blinker Links
|
||||
def switch_left_up(pin):
|
||||
def switch_left_up(channel):
|
||||
global blinker_links, blinker_rechts
|
||||
# stop blinker rechts if running
|
||||
if blinker_rechts == True:
|
||||
if blinker_rechts:
|
||||
blinker_rechts = False
|
||||
|
||||
# do nothing if blinker links is already running
|
||||
if blinker_links == True:
|
||||
if blinker_links:
|
||||
return
|
||||
|
||||
blinker_links = True
|
||||
threading.Thread(target=blink_links).start()
|
||||
|
||||
# start blinker links
|
||||
|
||||
_thread.start_new_thread(blink_links, ())
|
||||
|
||||
|
||||
def switch_left_down(pin):
|
||||
def switch_left_down(channel):
|
||||
global blinker_links
|
||||
blinker_links = False
|
||||
|
||||
toggle_switch_left.irq(handler=switch_left_up, trigger=Pin.IRQ_RISING)
|
||||
toggle_switch_left.irq(handler=switch_left_down, trigger=Pin.IRQ_FALLING)
|
||||
|
||||
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)
|
||||
|
||||
# Blinker Rechts
|
||||
def switch_right_up(pin):
|
||||
def switch_right_up(channel):
|
||||
global blinker_rechts, blinker_links
|
||||
# stop blinker links if running
|
||||
if blinker_links == True:
|
||||
if blinker_links:
|
||||
blinker_links = False
|
||||
|
||||
# do nothing if blinker rechts is already running
|
||||
if blinker_rechts == True:
|
||||
if blinker_rechts:
|
||||
return
|
||||
|
||||
blinker_rechts = True
|
||||
threading.Thread(target=blink_rechts).start()
|
||||
|
||||
# start blinker rechts
|
||||
|
||||
_thread.start_new_thread(blink_rechts, ())
|
||||
|
||||
|
||||
def switch_right_down(pin):
|
||||
def switch_right_down(channel):
|
||||
global blinker_rechts
|
||||
blinker_rechts = False
|
||||
|
||||
toggle_switch_right.irq(handler=switch_right_up, trigger=Pin.IRQ_RISING)
|
||||
toggle_switch_right.irq(handler=switch_right_down, trigger=Pin.IRQ_FALLING)
|
||||
|
||||
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)
|
||||
|
||||
# Button Interrupt
|
||||
def button_pressed(pin):
|
||||
def button_pressed(channel):
|
||||
global debouncing, animation_NR, reset_animation
|
||||
# Handle the button press event
|
||||
animation_NR = (animation_NR + 1) % anzahl_animationen
|
||||
# Reset the debouncing flag
|
||||
debouncing = False
|
||||
reset_animation = True
|
||||
|
||||
def button_debouncer(pin):
|
||||
global debouncing
|
||||
if not debouncing:
|
||||
debouncing = True
|
||||
debounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=lambda t: button_pressed(pin))
|
||||
|
||||
# Set up the button interrupt with the debouncer
|
||||
button.irq(handler=button_debouncer, trigger=Pin.IRQ_RISING)
|
||||
|
||||
|
||||
###########################################################################
|
||||
|
||||
# MAIN LOOPS
|
||||
def animation_loop():
|
||||
global animation_NR, reset_animation
|
||||
while True:
|
||||
reset_animation = False
|
||||
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()
|
||||
|
||||
|
||||
def test_loop():
|
||||
while True:
|
||||
|
||||
#rainbow()
|
||||
# kit()
|
||||
# fernlicht_an()
|
||||
utime.sleep(0.5)
|
||||
# licht_aus()
|
||||
# utime.sleep(0.5)
|
||||
# police()
|
||||
print('thread1')
|
||||
|
||||
blink_rechts()
|
||||
blink_links()
|
||||
|
||||
|
||||
# start blinker_loop on second core
|
||||
#_thread.start_new_thread(animation_loop, ())
|
||||
|
||||
#blinker_loop()
|
||||
animation_loop()
|
||||
#test_loop()
|
||||
GPIO.add_event_detect
|
||||
|
|
Loading…
Reference in a new issue