trying a new way of controlling the LED strips

This commit is contained in:
Patrick Tschuchnig 2019-09-11 15:01:29 +02:00
parent 003ccce40f
commit dc9497ab6b

View File

@ -135,6 +135,15 @@ led_red = 0
led_green = 0
led_blue = 0
# RPi.GPIO does not provide getter methods for PWM, therefor
# defining my own here
led_red_brightness = 0
led_green_brightness = 0
led_blue_brightness = 0
# predefine led blink timer to 0
timer_led_blink = 0
############################################
######## END OF PRE-INITIALISATION #########
############################################
@ -183,26 +192,41 @@ def led_init():
led_blue.start(0)
led_red.start(0)
def change_red(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
# to be used by change_led_colour
global led_red, led_red_brightness
led_red_brightness = amount
factor = 100/255
led_red.ChangeDutyCycle(amount*factor)
def change_green(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
# to be used by change_led_colour
global led_green
global led_green, led_green_brightness
led_green_brightness = amount
factor = 100/255
led_green.ChangeDutyCycle(amount*factor)
def change_blue(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
# to be used by change_led_colour
global led_blue
global led_blue, led_blue_brightness
led_blue_brightness = amount
factor = 100/255
led_blue.ChangeDutyCycle(amount*factor)
def change_red(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
# to be used by change_led_colour
global led_red
factor = 100/255
led_red.ChangeDutyCycle(amount*factor)
def get_red():
global led_red_brightness
return led_red_brightness
def get_green():
global led_green_brightness
return led_green_brightness
def get_blue():
global led_blue_brightness
return led_blue_brightness
def change_led_colour(red_amount,green_amount,blue_amount):
# sets an RGB value for the led strip.
@ -226,6 +250,23 @@ def change_led_speed(colour, speed):
else:
return false
def led_handler():
if led_blink == True:
if timer_led_blink == 0:
timer_led_blink_starttime = pygame.time.get_ticks()
timer_led_blink = (timer_led_blink_starttime-pygame.time.get_ticks())
if (timer_led_blink/1000)%2 = 0:
if timer_led_blink_toggle == 1:
led_red_brightness_temp = get_red()
led_green_brightness_temp = get_green()
led_blue_brightness_temp = get_blue()
change_led_colour(0,0,0)
timer_led_blink_toggle = 0
else:
change_led_colour(led_red_brightness_temp,led_green_brightness_temp,led_blue_brightness_temp)
timer_led_blink_toggle = 1
def led_alert():
# stops the running program for 10 seconds
# causes the red LED to flash
@ -233,6 +274,8 @@ def led_alert():
#
# !!!!untested!!!!
#
# OBSOLETE!!!!
#
change_led_colour(100, 0, 0)
change_led_speed(red, 1)
for i in range(1,10):
@ -306,7 +349,7 @@ def enter_name():
screen.blit(highscore_surface, highscore_rectangle)
textbox_text_surface = pygame_font_2.render('Enter Name:',True, pygame_font_main_color)
textbox_text_rectangle = textbox_surface.get_rect()
textbox_text_rectangle = textbox_text_surface.get_rect()
textbox_text_rectangle.topleft = (700, 400)
textbox_surface = pygame_font_2.render(str(name), True, pygame_font_main_color)
@ -444,6 +487,8 @@ highscore_checked = False
pin_start_inhibit = False
timer_game_ending_started = False
led_blink = True
############################################
########## END OF INITIALISATION ###########
############################################
@ -457,6 +502,7 @@ while True:
clear_screen()
handle_events()
show_debug()
led_handler()
if game_running == False and game_ending == False:
# one shot for changing the led colour