changed led handler and blinking mechanics
This commit is contained in:
parent
d64b0858fc
commit
dc1bc6fe7b
124
gametest_rev1.py
124
gametest_rev1.py
@ -141,6 +141,9 @@ led_red_brightness = 0
|
|||||||
led_green_brightness = 0
|
led_green_brightness = 0
|
||||||
led_blue_brightness = 0
|
led_blue_brightness = 0
|
||||||
|
|
||||||
|
# leds are turned on and can be switched off by default
|
||||||
|
leds_off_toggle = False
|
||||||
|
|
||||||
############################################
|
############################################
|
||||||
######## END OF PRE-INITIALISATION #########
|
######## END OF PRE-INITIALISATION #########
|
||||||
############################################
|
############################################
|
||||||
@ -191,32 +194,45 @@ def led_init():
|
|||||||
|
|
||||||
def change_red(amount):
|
def change_red(amount):
|
||||||
# takes an input from 0 to 255 and converts it to Duty Cycle
|
# takes an input from 0 to 255 and converts it to Duty Cycle
|
||||||
|
# valid duty cycle values are between 0 and 100
|
||||||
|
# stores the new value in led_brightness_COLOUR
|
||||||
|
# this value is later read by the led_handler to set the
|
||||||
|
# corresponding duty cycles to allow fast interactive updating
|
||||||
|
#
|
||||||
# to be used by change_led_colour
|
# to be used by change_led_colour
|
||||||
global led_red, led_red_brightness
|
global led_red_brightness
|
||||||
led_red_brightness = amount
|
|
||||||
factor = 100/255
|
factor = 100/255
|
||||||
led_red.ChangeDutyCycle(led_red_brightness*factor)
|
led_red_brightness = amount*factor
|
||||||
|
|
||||||
def change_green(amount):
|
def change_green(amount):
|
||||||
# takes an input from 0 to 255 and converts it to Duty Cycle
|
# takes an input from 0 to 255 and converts it to Duty Cycle
|
||||||
|
# valid duty cycle values are between 0 and 100
|
||||||
|
# stores the new value in led_brightness_COLOUR
|
||||||
|
# this value is later read by the led_handler to set the
|
||||||
|
# corresponding duty cycles to allow fast interactive updating
|
||||||
|
#
|
||||||
# to be used by change_led_colour
|
# to be used by change_led_colour
|
||||||
global led_green, led_green_brightness
|
global led_green_brightness
|
||||||
led_green_brightness = amount
|
|
||||||
factor = 100/255
|
factor = 100/255
|
||||||
led_green.ChangeDutyCycle(led_green_brightness*factor)
|
led_green_brightness = amount*factor
|
||||||
|
|
||||||
def change_blue(amount):
|
def change_blue(amount):
|
||||||
# takes an input from 0 to 255 and converts it to Duty Cycle
|
# takes an input from 0 to 255 and converts it to Duty Cycle
|
||||||
|
# valid duty cycle values are between 0 and 100
|
||||||
|
# stores the new value in led_brightness_COLOUR
|
||||||
|
# this value is later read by the led_handler to set the
|
||||||
|
# corresponding duty cycles to allow fast interactive updating
|
||||||
|
#
|
||||||
# to be used by change_led_colour
|
# to be used by change_led_colour
|
||||||
global led_blue, led_blue_brightness
|
global led_blue_brightness
|
||||||
led_blue_brightness = amount
|
|
||||||
factor = 100/255
|
factor = 100/255
|
||||||
led_blue.ChangeDutyCycle(led_blue_brightness_temp*factor)
|
led_blue_brightness = amount*factor
|
||||||
|
|
||||||
def change_led_colour(red_amount,green_amount,blue_amount):
|
def change_led_colour(red_amount,green_amount,blue_amount):
|
||||||
# sets an RGB value for the led strip.
|
# sets an RGB value for the led strip.
|
||||||
if red_amount < 0 or red_amount > 255 or green_amount < 0 or green_amount > 255 or blue_amount < 0 or blue_amount > 255:
|
if red_amount < 0 or red_amount > 255 or green_amount < 0 or green_amount > 255 or blue_amount < 0 or blue_amount > 255:
|
||||||
led_alert()
|
# valid rgb values should be between 0 and 255
|
||||||
|
print('invalid rgb value received: ' + str(red_amount) + ' ,' + str(green_amount) + ' ,' + str(blue_amount))
|
||||||
return false
|
return false
|
||||||
change_red(red_amount)
|
change_red(red_amount)
|
||||||
change_blue(blue_amount)
|
change_blue(blue_amount)
|
||||||
@ -224,6 +240,9 @@ def change_led_colour(red_amount,green_amount,blue_amount):
|
|||||||
|
|
||||||
def change_led_speed(colour, speed):
|
def change_led_speed(colour, speed):
|
||||||
# changes the speed of the LED pwm
|
# changes the speed of the LED pwm
|
||||||
|
#
|
||||||
|
# !!!!OBSOLETE!!!!
|
||||||
|
#
|
||||||
global led_red,led_green,led_blue
|
global led_red,led_green,led_blue
|
||||||
|
|
||||||
if colour == "red":
|
if colour == "red":
|
||||||
@ -236,40 +255,57 @@ def change_led_speed(colour, speed):
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
def led_handler():
|
def led_handler():
|
||||||
# global timer_led_blink_toggle, timer_led_one_shot
|
global timer_led_blink_toggle, timer_led_switched
|
||||||
# if led_blink == True:
|
|
||||||
# if int((pygame.time.get_ticks()/1000))%2 == 0:
|
|
||||||
# if timer_led_one_shot == 0:
|
|
||||||
# timer_led_one_shot = 1
|
|
||||||
# if timer_led_blink_toggle == 1:
|
|
||||||
# print('BLINK: switching leds off')
|
|
||||||
# print('red br.:' + str(led_red_brightness))
|
|
||||||
|
|
||||||
# change_led_colour(0,0,0)
|
# led blinking effect. can be toggled on or off by setting led_blink to true or false.
|
||||||
# timer_led_blink_toggle = 0
|
# blinks every second-
|
||||||
# else:
|
|
||||||
# print('BLINK: reverting colours')
|
|
||||||
# change_led_colour(led_red_brightness,led_green_brightness,led_blue_brightness)
|
|
||||||
# timer_led_blink_toggle = 1
|
|
||||||
# else:
|
|
||||||
# timer_led_one_shot = 0
|
|
||||||
pass
|
|
||||||
|
|
||||||
def led_alert():
|
if led_blink == True:
|
||||||
# stops the running program for 10 seconds
|
# this equals to 0 every tick for one second, and 1 every tick for another second
|
||||||
# causes the red LED to flash
|
# it requires being handled in a one-shot situation since this is called
|
||||||
# use as error message
|
# multiple times per second, and will equal to 0 or 1 multiple times per second
|
||||||
#
|
# aswell. the real value of get_ticks()/1000 cannot be predetermined.
|
||||||
# !!!!untested!!!!
|
if int((pygame.time.get_ticks()/1000))%2 == 0:
|
||||||
#
|
if timer_led_switched == 0:
|
||||||
# OBSOLETE!!!!
|
timer_led_switched = 1
|
||||||
#
|
if timer_led_blink_toggle == 1:
|
||||||
change_led_colour(100, 0, 0)
|
print('BLINK: switching leds off')
|
||||||
change_led_speed(red, 1)
|
leds_toggle()
|
||||||
for i in range(1,10):
|
timer_led_blink_toggle = 0
|
||||||
time.sleep(1)
|
else:
|
||||||
change_led_colour(100, 100, 100)
|
print('BLINK: reverting colours')
|
||||||
change_led_speed(red, 100)
|
leds_toggle()
|
||||||
|
timer_led_blink_toggle = 1
|
||||||
|
else:
|
||||||
|
timer_led_switched = 0
|
||||||
|
|
||||||
|
|
||||||
|
# update the led duty cycle
|
||||||
|
# may cause flickering, not sure :)
|
||||||
|
# TODO: test this
|
||||||
|
|
||||||
|
global led_red,led_green,led_blue
|
||||||
|
|
||||||
|
# dont update the pwm for the LEDs when they're supposed to be off
|
||||||
|
# instead, just copy the needed values to the 3 brightness variables!
|
||||||
|
# the values will be correctly put in once the toggle is back
|
||||||
|
|
||||||
|
if leds_off_toggle == False:
|
||||||
|
led_red.ChangeDutyCycle(led_red_brightness)
|
||||||
|
led_green.ChangeDutyCycle(led_green_brightness)
|
||||||
|
led_blue.ChangeDutyCycle(led_blue_brightness)
|
||||||
|
|
||||||
|
def leds_toggle():
|
||||||
|
|
||||||
|
global led_red,led_green,led_blue,leds_off_toggle
|
||||||
|
|
||||||
|
if leds_off_toggle == False:
|
||||||
|
led_red.ChangeDutyCycle(0)
|
||||||
|
led_green.ChangeDutyCycle(0)
|
||||||
|
led_blue.ChangeDutyCycle(0)
|
||||||
|
leds_off_toggle = True
|
||||||
|
else:
|
||||||
|
leds_off_toggle = False
|
||||||
|
|
||||||
def toggle_fullscreen():
|
def toggle_fullscreen():
|
||||||
screen = pygame.display.get_surface()
|
screen = pygame.display.get_surface()
|
||||||
@ -481,7 +517,7 @@ timer_led_blink_toggle = 1
|
|||||||
led_red_brightness_temp = 0
|
led_red_brightness_temp = 0
|
||||||
led_green_brightness_temp = 0
|
led_green_brightness_temp = 0
|
||||||
led_blue_brightness_temp = 0
|
led_blue_brightness_temp = 0
|
||||||
timer_led_one_shot = 0
|
timer_led_switched = 0
|
||||||
|
|
||||||
############################################
|
############################################
|
||||||
########## END OF INITIALISATION ###########
|
########## END OF INITIALISATION ###########
|
||||||
@ -496,7 +532,7 @@ while True:
|
|||||||
clear_screen()
|
clear_screen()
|
||||||
handle_events()
|
handle_events()
|
||||||
show_debug()
|
show_debug()
|
||||||
#led_handler()
|
led_handler()
|
||||||
|
|
||||||
print(int((pygame.time.get_ticks()/1000))%2)
|
print(int((pygame.time.get_ticks()/1000))%2)
|
||||||
if game_running == False and game_ending == False:
|
if game_running == False and game_ending == False:
|
||||||
|
Loading…
Reference in New Issue
Block a user