Button interrupt and parallelize blinker

This commit is contained in:
JonOfUs 2024-08-09 00:46:46 +02:00
parent d616963256
commit f6d1f4c399

205
main.py
View file

@ -22,7 +22,7 @@ segment = 4
delay = 0.15 delay = 0.15
brightness = 100 brightness = 100
anzahl_animationen = 5 anzahl_animationen = 5
global animation_NR
#setup #setup
strip_blinker = Neopixel(numpix, 0, 28, "RGB") strip_blinker = Neopixel(numpix, 0, 28, "RGB")
strip_animation = Neopixel(numpix, 0, 28, "RGB") strip_animation = Neopixel(numpix, 0, 28, "RGB")
@ -32,6 +32,17 @@ button = Pin(18, Pin.IN, Pin.PULL_UP)
toggle_switch_left = Pin(15, Pin.IN, Pin.PULL_DOWN) toggle_switch_left = Pin(15, Pin.IN, Pin.PULL_DOWN)
toggle_switch_right = Pin(14, Pin.IN, Pin.PULL_DOWN) toggle_switch_right = Pin(14, Pin.IN, Pin.PULL_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 #Farben definnieren
@ -56,17 +67,29 @@ colors = [red, orange, yellow, lime, green, teal, cyan, sky_blue, blue, indigo,
############ Blinker Links ################### ############ Blinker Links ###################
def blink_links(): def blink_links():
global blinker_links
while True: while True:
if blinker_links == False:
break
strip_blinker.show() strip_blinker.show()
for x in reversed(range(segment)): for x in reversed(range(segment)):
if blinker_links == False:
break
strip_blinker.set_pixel(x, yellow) strip_blinker.set_pixel(x, yellow)
strip_blinker.show() strip_blinker.show()
utime.sleep(delay) utime.sleep(delay)
if blinker_links == False:
break
utime.sleep(2 * delay) utime.sleep(2 * delay)
for y in range(segment): for y in range(segment):
strip_blinker.set_pixel(y, blank) strip_blinker.set_pixel(y, blank)
strip_blinker.show() strip_blinker.show()
for y in range(segment):
strip_blinker.set_pixel(y, blank)
strip_blinker.show()
return return
################################################## ##################################################
@ -74,19 +97,29 @@ def blink_links():
############ Blinker Rechts ################## ############ Blinker Rechts ##################
def blink_rechts(): def blink_rechts():
global blinker_rechts
startpoint = numpix - segment startpoint = numpix - segment
while True: while True:
if blinker_rechts == False:
break
strip_blinker.show() strip_blinker.show()
for x in range(segment): for x in range(segment):
if blinker_rechts == False:
break
strip_blinker.set_pixel(startpoint + x, yellow) strip_blinker.set_pixel(startpoint + x, yellow)
strip_blinker.show() strip_blinker.show()
utime.sleep(delay) utime.sleep(delay)
if blinker_rechts == False:
break
utime.sleep(2 * delay) utime.sleep(2 * delay)
for y in range(segment): for y in range(segment):
strip_blinker.set_pixel(startpoint + y, blank) strip_blinker.set_pixel(startpoint + y, blank)
strip_blinker.show() strip_blinker.show()
for y in range(segment):
strip_blinker.set_pixel(startpoint + y, blank)
strip_blinker.show()
return return
@ -95,42 +128,45 @@ def blink_rechts():
# ----------------Animationen------------------# # ----------------Animationen------------------#
############ 0.Animation Licht aus ################ ############ 0.Animation Licht aus ################
def licht_aus(): def licht_aus():
global reset_animation
for i in range(numpix - segment): for i in range(numpix - segment):
if i >= segment: if i >= segment:
strip_animation.set_pixel(i, blank) strip_animation.set_pixel(i, blank)
strip_animation.show() strip_animation.show()
while reset_animation == False:
utime.sleep(delay)
##################################################
############### 1.Animation Licht an ############### ############### 1.Animation Licht an ###############
def fernlicht_an(): def fernlicht_an():
global reset_animation
for i in range(numpix - segment): for i in range(numpix - segment):
if i >= segment: if i >= segment:
strip_animation.set_pixel(i, white) strip_animation.set_pixel(i, white)
strip_animation.show() strip_animation.show()
while reset_animation == False:
utime.sleep(delay)
licht_aus()
############### 2.Animation Rainbow ############### ############### 2.Animation Rainbow ###############
def rainbow(): def rainbow():
global reset_animation
for i in range(len(colors)): for i in range(len(colors)):
for j in range(numpix - segment): for j in range(numpix - segment):
if j >= segment: if j >= segment:
strip_animation.set_pixel(j, colors[i]) strip_animation.set_pixel(j, colors[i])
strip_animation.show() strip_animation.show()
utime.sleep(delay) utime.sleep(delay)
if reset_animation == True:
licht_aus()
return
##################################################
############### 3.Animation Police ############### ############### 3.Animation Police ###############
def police(): def police():
while True: global reset_animation
strip_animation.set_pixel(4, blue) strip_animation.set_pixel(4, blue)
strip_animation.set_pixel(5, blue) strip_animation.set_pixel(5, blue)
@ -150,6 +186,10 @@ def police():
strip_animation.show() strip_animation.show()
utime.sleep(delay * 2) utime.sleep(delay * 2)
if reset_animation == True:
licht_aus()
return
strip_animation.set_pixel(4, red) strip_animation.set_pixel(4, red)
strip_animation.set_pixel(5, red) strip_animation.set_pixel(5, red)
strip_animation.set_pixel(6, red) strip_animation.set_pixel(6, red)
@ -168,15 +208,12 @@ def police():
strip_animation.show() strip_animation.show()
utime.sleep(delay * 2) utime.sleep(delay * 2)
return
##################################################
############### 4.Animation Kit ############### ############### 4.Animation Kit ###############
def kit(): def kit():
global reset_animation
delay = 0.01 delay = 0.01
while True:
strip_animation.show() strip_animation.show()
############### Left to Right ############### ############### Left to Right ###############
@ -197,6 +234,9 @@ def kit():
utime.sleep(delay) utime.sleep(delay)
strip_animation.set_pixel(x + 2, blank) strip_animation.set_pixel(x + 2, blank)
strip_animation.show() strip_animation.show()
if reset_animation == True:
licht_aus()
return
############### Left to Right ############### ############### Left to Right ###############
for x in reversed(range(numpix - segment - 2)): for x in reversed(range(numpix - segment - 2)):
@ -216,6 +256,8 @@ def kit():
utime.sleep(delay) utime.sleep(delay)
strip_animation.set_pixel(x + 2, blank) strip_animation.set_pixel(x + 2, blank)
strip_animation.show() strip_animation.show()
if reset_animation == True:
licht_aus()
return return
@ -225,92 +267,89 @@ def kit():
button = Pin(18, Pin.IN, Pin.PULL_UP) button = Pin(18, Pin.IN, Pin.PULL_UP)
############### Schalterstellung ###############
def get_switch_left():
global status_links
if toggle_switch_left.value() == 1:
status_links = 1
elif toggle_switch_left.value() == 0:
status_links = 0
return status_links
def get_switch_right():
global status_rechts
if toggle_switch_right.value() == 1:
status_rechts = 1
elif toggle_switch_right.value() == 0:
status_rechts = 0
return
########################################################################### ###########################################################################
# INTERRUPT HANDLER # INTERRUPT HANDLER
def switch_left(pin): # Blinker Links
pass def switch_left_up(pin):
global blinker_links, blinker_rechts
# stop blinker rechts if running
if blinker_rechts == True:
blinker_rechts = False
def switch_left_debouncer(pin): # do nothing if blinker links is already running
Timer().init(mode=Timer.ONE_SHOT, period=200, callback=switch_left) if blinker_links == True:
return
toggle_switch_left.irq(handler=switch_left_debouncer, trigger=Pin.IRQ_RISING) blinker_links = True
# start blinker links
_thread.start_new_thread(blink_links, ())
def switch_right(pin): def switch_left_down(pin):
pass global blinker_links
blinker_links = False
def switch_right_debouncer(pin): toggle_switch_left.irq(handler=switch_left_up, trigger=Pin.IRQ_RISING)
Timer().init(mode=Timer.ONE_SHOT, period=200, callback=switch_right) toggle_switch_left.irq(handler=switch_left_down, trigger=Pin.IRQ_FALLING)
toggle_switch_right.irq(handler=switch_right_debouncer, trigger=Pin.IRQ_RISING)
# Blinker Rechts
def switch_right_up(pin):
global blinker_rechts, blinker_links
# stop blinker links if running
if blinker_links == True:
blinker_links = False
# do nothing if blinker rechts is already running
if blinker_rechts == True:
return
blinker_rechts = True
# start blinker rechts
_thread.start_new_thread(blink_rechts, ())
def switch_right_down(pin):
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)
# Button Interrupt
def button_pressed(pin): def button_pressed(pin):
pass 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): def button_debouncer(pin):
Timer().init(mode=Timer.ONE_SHOT, period=200, callback=button_pressed) 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) button.irq(handler=button_debouncer, trigger=Pin.IRQ_RISING)
########################################################################### ###########################################################################
# MAIN LOOPS # MAIN LOOPS
def blinker_loop():
while True:
get_switch_left()
get_switch_right()
if status_rechts == 1:
blink_rechts()
if status_links == 1:
blink_links()
utime.sleep(0.1)
def animation_loop(): def animation_loop():
animation_NR = 0 global animation_NR, reset_animation
while True: while True:
reset_animation = False
if button.value() == 1:
print(animation_NR)
elif button.value() == 0:
if animation_NR < anzahl_animationen - 1:
animation_NR = animation_NR + 1
else:
animation_NR = 0
print(animation_NR)
utime.sleep(delay)
if animation_NR == 0: if animation_NR == 0:
licht_aus() licht_aus()
elif animation_NR == 1: elif animation_NR == 1:
@ -323,12 +362,6 @@ def animation_loop():
kit() kit()
def test_loop2():
while True:
utime.sleep(0.5)
print('thread2')
def test_loop(): def test_loop():
while True: while True:
@ -346,8 +379,8 @@ def test_loop():
# start blinker_loop on second core # start blinker_loop on second core
_thread.start_new_thread(test_loop2, ()) #_thread.start_new_thread(animation_loop, ())
#blinker_loop() #blinker_loop()
#animation_loop() animation_loop()
test_loop() #test_loop()