trying a new way of controlling the LED strips
This commit is contained in:
		| @@ -135,6 +135,15 @@ led_red = 0 | |||||||
| led_green = 0 | led_green = 0 | ||||||
| led_blue = 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 ######### | ######## END OF PRE-INITIALISATION ######### | ||||||
| ############################################ | ############################################ | ||||||
| @@ -183,26 +192,41 @@ def led_init(): | |||||||
|         led_blue.start(0) |         led_blue.start(0) | ||||||
|         led_red.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): | 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 | ||||||
|         # to be used by change_led_colour |         # to be used by change_led_colour | ||||||
|         global led_green |         global led_green, led_green_brightness | ||||||
|  |         led_green_brightness = amount | ||||||
|         factor = 100/255 |         factor = 100/255 | ||||||
|         led_green.ChangeDutyCycle(amount*factor) |         led_green.ChangeDutyCycle(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 | ||||||
|         # to be used by change_led_colour |         # to be used by change_led_colour | ||||||
|         global led_blue |         global led_blue, led_blue_brightness | ||||||
|  |         led_blue_brightness = amount | ||||||
|         factor = 100/255 |         factor = 100/255 | ||||||
|         led_blue.ChangeDutyCycle(amount*factor) |         led_blue.ChangeDutyCycle(amount*factor) | ||||||
|      |      | ||||||
| def change_red(amount): | def get_red(): | ||||||
|         # takes an input from 0 to 255 and converts it to Duty Cycle |     global led_red_brightness | ||||||
|         # to be used by change_led_colour |     return led_red_brightness | ||||||
|         global led_red |  | ||||||
|         factor = 100/255 | def get_green(): | ||||||
|         led_red.ChangeDutyCycle(amount*factor) |     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): | 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. | ||||||
| @@ -226,6 +250,23 @@ def change_led_speed(colour, speed): | |||||||
|         else: |         else: | ||||||
|             return false |             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(): | def led_alert(): | ||||||
|         # stops the running program for 10 seconds |         # stops the running program for 10 seconds | ||||||
|         # causes the red LED to flash |         # causes the red LED to flash | ||||||
| @@ -233,6 +274,8 @@ def led_alert(): | |||||||
|         # |         # | ||||||
|         # !!!!untested!!!! |         # !!!!untested!!!! | ||||||
|         # |         # | ||||||
|  |         # OBSOLETE!!!! | ||||||
|  |         # | ||||||
|         change_led_colour(100, 0, 0) |         change_led_colour(100, 0, 0) | ||||||
|         change_led_speed(red, 1) |         change_led_speed(red, 1) | ||||||
|         for i in range(1,10): |         for i in range(1,10): | ||||||
| @@ -306,7 +349,7 @@ def enter_name(): | |||||||
|                 screen.blit(highscore_surface, highscore_rectangle) |                 screen.blit(highscore_surface, highscore_rectangle) | ||||||
|  |  | ||||||
|                 textbox_text_surface = pygame_font_2.render('Enter Name:',True, pygame_font_main_color) |                 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_text_rectangle.topleft = (700, 400) | ||||||
|  |  | ||||||
|                 textbox_surface = pygame_font_2.render(str(name), True, pygame_font_main_color) |                 textbox_surface = pygame_font_2.render(str(name), True, pygame_font_main_color) | ||||||
| @@ -444,6 +487,8 @@ highscore_checked = False | |||||||
| pin_start_inhibit = False | pin_start_inhibit = False | ||||||
| timer_game_ending_started = False | timer_game_ending_started = False | ||||||
|  |  | ||||||
|  | led_blink = True | ||||||
|  |  | ||||||
| ############################################ | ############################################ | ||||||
| ########## END OF INITIALISATION ########### | ########## END OF INITIALISATION ########### | ||||||
| ############################################ | ############################################ | ||||||
| @@ -457,6 +502,7 @@ while True: | |||||||
|         clear_screen() |         clear_screen() | ||||||
|         handle_events() |         handle_events() | ||||||
|         show_debug() |         show_debug() | ||||||
|  |         led_handler() | ||||||
|  |  | ||||||
|         if game_running == False and game_ending == False:  |         if game_running == False and game_ending == False:  | ||||||
|             # one shot for changing the led colour |             # one shot for changing the led colour | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user