diff --git a/gametest.py b/gametest.py index bc07c47..82af61c 100644 --- a/gametest.py +++ b/gametest.py @@ -4,11 +4,17 @@ import pygame, signal, sys, time, os, RPi.GPIO as GPIO from pygame.locals import * -#GPIO +#GPIO for Buttons startPin = 32 errorPin = 31 shutdownPin = 3 +#GPIO for LED +pin_red = 22 +pin_green = 18 +pin_blue = 16 + +#initialise gpio GPIO.setmode(GPIO.BOARD) GPIO.setup(startPin, GPIO.IN) GPIO.setup(errorPin, GPIO.IN) @@ -19,6 +25,7 @@ timePerError = 5 # every time the wire is touched, some time is added as penalty screenSizeX = 1920 screenSizeY = 1080 +#initialise the game pygame.init() #colors @@ -34,12 +41,15 @@ font1 = pygame.font.Font('freesansbold.ttf', 90) font2 = pygame.font.Font('freesansbold.ttf', 65) font3 = pygame.font.Font('freesansbold.ttf', 45) -# preset highscores +#predefinition of led variables, for later global use +red = "" +green = "" +blue = "" +# preset highscores hs1_name = "Fritz" hs2_name = "Bernd" hs3_name = "Max" - hs1_time = 100 hs2_time = 200 hs3_time = 300 @@ -49,9 +59,94 @@ MAX_NAME_LENGTH = 10 def signal_handler(sig, frame): + # signal handler, later called to interpret + # ctrl+c as "quit" rather than "abort" print('Programm exiting') exit_application() +def led_init(): + # initialises the LED pins + # mind that the pins for the LEDs are pullup by default, + # which is on purpose, since this causes the LED strip to + # be turned off when the program isnt running or their state + # is undefined + + # set the led pins to be outputs + # careful: these pins dont drive the + # led strip themselves, but rather 3 + # n-channel mosfets, type IRLZ34N + global red,green,blue + GPIO.setup(pin_red, GPIO.OUT) + GPIO.setup(pin_green, GPIO.OUT) + GPIO.setup(pin_blue, GPIO.OUT) + + # set the pwm frequency to 100 + # if you experience strobing, try changing + # the number + red = GPIO.PWM(pin_red, 100) + blue = GPIO.PWM(pin_blue, 100) + green = GPIO.PWM(pin_green, 100) + + # start pwm generation with a duty cycle of 0 + green.start(0) + blue.start(0) + red.start(0) + +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 green + factor = 100/255 + 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 blue + factor = 100/255 + 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 red + factor = 100/255 + red.ChangeDutyCycle(amount*factor) + +def change_led_colour(red_amount,green_amount,blue_amount): + # sets an RGB value for the led strip. + global red,green,blue + 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() + return false + red.ChangeDutyCycle(red_amount) + blue.ChangeDutyCycle(blue_amount) + green.ChangeDutyCycle(green_amount) + +def change_led_speed(colour, speed): + #changes the speed of the LED pwm + global red,green,blue + + if colour == "red": + red.ChangeDutyCycle(speed) + elif colour == "green": + green.ChangeDutyCycle(speed) + elif colour == "blue": + blue.ChangeDutyCycle(speed) + else: + return false + +def led_alert(): + # stops the running program for 10 seconds + # causes the red LED to flash + # use as error message + 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) + def toggle_fullscreen(): screen = pygame.display.get_surface() tmp = screen.convert() @@ -186,17 +281,21 @@ def checkHighscores(time): #signal handing for controlled exit via ctrl+c signal.signal(signal.SIGINT, signal_handler) -pygame.mouse.set_visible(False) # hide mouse +#hide mouse +pygame.mouse.set_visible(False) screen = pygame.display.set_mode((screenSizeX, screenSizeY), pygame.FULLSCREEN) #pygame.display.set_caption('Heisser Draht') # not required for fullscreen application #screen = toggle_fullscreen() +#initialise led strip +led_init() while True: # main loop #global label storage print("New game starting") + change_led_colour(10,100,10) time_surface = 0 errors_surface = 0 time_rectangle = 0 @@ -242,8 +341,12 @@ while True: # main loop start_time = int(time.time()) + change_led_colour(10,10,100) + + while True: # game running print("Game running normally") + handle_events() if GPIO.input(errorPin) == True: @@ -276,6 +379,8 @@ while True: # main loop #if GPIO.input(startPin) == True and justStarted == False: if GPIO.input(startPin) != True: + change_led_colour(100,10,10) + print("Game ended because start pin wasnt true") checkHighscores(int_time)