From dc1bc6fe7b9da1aba0513b0ab7e2fd74c59bc858 Mon Sep 17 00:00:00 2001 From: Patrick Tschuchnig Date: Thu, 12 Sep 2019 08:11:03 +0200 Subject: [PATCH] changed led handler and blinking mechanics --- gametest_rev1.py | 124 ++++++++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 44 deletions(-) diff --git a/gametest_rev1.py b/gametest_rev1.py index 93ad4c0..62cebc9 100644 --- a/gametest_rev1.py +++ b/gametest_rev1.py @@ -141,6 +141,9 @@ led_red_brightness = 0 led_green_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 ######### ############################################ @@ -191,32 +194,45 @@ def led_init(): def change_red(amount): # 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 - global led_red, led_red_brightness - led_red_brightness = amount + global led_red_brightness factor = 100/255 - led_red.ChangeDutyCycle(led_red_brightness*factor) + led_red_brightness = amount*factor def change_green(amount): # 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 - global led_green, led_green_brightness - led_green_brightness = amount + global led_green_brightness factor = 100/255 - led_green.ChangeDutyCycle(led_green_brightness*factor) + led_green_brightness = amount*factor def change_blue(amount): # 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 - global led_blue, led_blue_brightness - led_blue_brightness = amount + global led_blue_brightness 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): # 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: - 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 change_red(red_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): # changes the speed of the LED pwm + # + # !!!!OBSOLETE!!!! + # global led_red,led_green,led_blue if colour == "red": @@ -236,40 +255,57 @@ def change_led_speed(colour, speed): return false def led_handler(): - # global timer_led_blink_toggle, timer_led_one_shot - # 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)) + global timer_led_blink_toggle, timer_led_switched - # change_led_colour(0,0,0) - # timer_led_blink_toggle = 0 - # 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 + # led blinking effect. can be toggled on or off by setting led_blink to true or false. + # blinks every second- -def led_alert(): - # stops the running program for 10 seconds - # causes the red LED to flash - # use as error message - # - # !!!!untested!!!! - # - # OBSOLETE!!!! - # - change_led_colour(100, 0, 0) - change_led_speed(red, 1) - for i in range(1,10): - time.sleep(1) - change_led_colour(100, 100, 100) - change_led_speed(red, 100) + if led_blink == True: + # this equals to 0 every tick for one second, and 1 every tick for another second + # it requires being handled in a one-shot situation since this is called + # 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. + if int((pygame.time.get_ticks()/1000))%2 == 0: + if timer_led_switched == 0: + timer_led_switched = 1 + if timer_led_blink_toggle == 1: + print('BLINK: switching leds off') + leds_toggle() + timer_led_blink_toggle = 0 + else: + print('BLINK: reverting colours') + 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(): screen = pygame.display.get_surface() @@ -481,7 +517,7 @@ timer_led_blink_toggle = 1 led_red_brightness_temp = 0 led_green_brightness_temp = 0 led_blue_brightness_temp = 0 -timer_led_one_shot = 0 +timer_led_switched = 0 ############################################ ########## END OF INITIALISATION ########### @@ -496,7 +532,7 @@ while True: clear_screen() handle_events() show_debug() - #led_handler() + led_handler() print(int((pygame.time.get_ticks()/1000))%2) if game_running == False and game_ending == False: