if screen_output: print "\nYour turn!" for i in array: # for each colour in current sequence (check against inputted sequence) event = pfio.read_input() # read the button port state while event != 0: # wait till no buttons pressed event = pfio.read_input() # so a single button press is not read as 2 sleep(0.001) # delay while event == 0: # wait for any input event = pfio.read_input() sleep(0.001) # delay pin_number = pfio.get_pin_number(event) # calculate the input pin if screen_output: print colours[pin_number -1] # print the colour in sequence to the screen pfio.digital_write(pin_number+2,1) # light up the buttons pressed if event != pfio.get_pin_bit_mask(i): game = 0 # if any wrong buttons were pressed end the game break else: # otherwise the correct button was pressed previous = event event = pfio.read_input() while previous == event: # while the button is held down, wait
# # Python script to wait until certain button is pressed # returns button pressed and turns on a light to indicate it (for testing) # @Pierre # import piface.pfio as pfio pfio.init() button = pfio.read_input() while button == 0: button = pfio.read_input() pin_number = pfio.get_pin_number(button) pfio.digital_write(0,1) print pin_number
if screen_output: print "\nYour turn!" for i in array: # for each colour in current sequence (check against inputted sequence) event = pfio.read_input() # read the button port state while event != 0: # wait till no buttons pressed event = pfio.read_input( ) # so a single button press is not read as 2 sleep(0.001) # delay while event == 0: # wait for any input event = pfio.read_input() sleep(0.001) # delay pin_number = pfio.get_pin_number(event) # calculate the input pin if screen_output: print colours[pin_number - 1] # print the colour in sequence to the screen pfio.digital_write(pin_number + 2, 1) # light up the buttons pressed if event != pfio.get_pin_bit_mask(i): game = 0 # if any wrong buttons were pressed end the game break else: # otherwise the correct button was pressed previous = event event = pfio.read_input()
def ask_questions(self): for question in self.questions: # ask a question correct_answer_index = int(2 * random.random()) wrong_answer_index = correct_answer_index ^ 1 answers = ["", ""] answers[correct_answer_index] = question.correct_answer answers[wrong_answer_index] = question.wrong_answer values = [question.text] values.extend(answers) self.gui.update_question("%s\nA: %s\nB: %s" % tuple(values)) # wait for a button press pin_bit_pattern = pfio.read_input() while pin_bit_pattern == 0 and not self.stopped(): pin_bit_pattern = pfio.read_input() # since we can't have multi-leveled break statements... if self.stopped(): break # find out which button was pressed pin_number = pfio.get_pin_number(pin_bit_pattern) #print "pin number: %d" % pin_number #print self.player1.buttons[correct_answer_index].switch.pin_number if pin_number == self.player1.buttons[correct_answer_index].switch.pin_number: self.player1.buttons[correct_answer_index].light.turn_on() print "Player 1 got the correct answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question("%s\n\nThe correct answer was: %s\n\nPlayer 1 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player1.car.drive(3) self.player1.buttons[correct_answer_index].light.turn_off() elif pin_number == self.player1.buttons[wrong_answer_index].switch.pin_number: self.player1.buttons[wrong_answer_index].light.turn_on() print "Player 1 got the WRONG answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question("%s\n\nThe correct answer was: %s\n\nPlayer 2 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player2.car.drive(3) self.player1.buttons[wrong_answer_index].light.turn_off() elif pin_number == self.player2.buttons[correct_answer_index].switch.pin_number: self.player2.buttons[correct_answer_index].light.turn_on() print "Player 2 got the correct answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question("%s\n\nThe correct answer was: %s\n\nPlayer 2 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player2.car.drive(3) self.player2.buttons[correct_answer_index].light.turn_off() elif pin_number == self.player2.buttons[wrong_answer_index].switch.pin_number: self.player2.buttons[wrong_answer_index].light.turn_on() print "Player 2 got the WRONG answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question("%s\n\nThe correct answer was: %s\n\nPlayer 1 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player1.car.drive(3) self.player2.buttons[wrong_answer_index].light.turn_off() elif pin_number == self.buttons[4].switch.pin_number: self.buttons[4].light.turn_on() print "PASS" #print "The answer was: {}".format(question.correct_answer) time.sleep(1) self.buttons[4].light.turn_off() else: raise UnknownButtonError("detected change on pin: %d" % pin_number) # wait until nothing is pressed pin_bit_pattern = pfio.read_input() while pin_bit_pattern != 0: pin_bit_pattern = pfio.read_input() # should we keep playing? if self.stopped(): break
def ask_questions(self): for question in self.questions: # ask a question correct_answer_index = int(2 * random.random()) wrong_answer_index = correct_answer_index ^ 1 answers = ["", ""] answers[correct_answer_index] = question.correct_answer answers[wrong_answer_index] = question.wrong_answer values = [question.text] values.extend(answers) self.gui.update_question("%s\nA: %s\nB: %s" % tuple(values)) # wait for a button press pin_bit_pattern = pfio.read_input() while pin_bit_pattern == 0 and not self.stopped(): pin_bit_pattern = pfio.read_input() # since we can't have multi-leveled break statements... if self.stopped(): break # find out which button was pressed pin_number = pfio.get_pin_number(pin_bit_pattern) #print "pin number: %d" % pin_number #print self.player1.buttons[correct_answer_index].switch.pin_number if pin_number == self.player1.buttons[ correct_answer_index].switch.pin_number: self.player1.buttons[correct_answer_index].light.turn_on() print "Player 1 got the correct answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question( "%s\n\nThe correct answer was: %s\n\nPlayer 1 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player1.car.drive(3) self.player1.buttons[correct_answer_index].light.turn_off() elif pin_number == self.player1.buttons[ wrong_answer_index].switch.pin_number: self.player1.buttons[wrong_answer_index].light.turn_on() print "Player 1 got the WRONG answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question( "%s\n\nThe correct answer was: %s\n\nPlayer 2 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player2.car.drive(3) self.player1.buttons[wrong_answer_index].light.turn_off() elif pin_number == self.player2.buttons[ correct_answer_index].switch.pin_number: self.player2.buttons[correct_answer_index].light.turn_on() print "Player 2 got the correct answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question( "%s\n\nThe correct answer was: %s\n\nPlayer 2 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player2.car.drive(3) self.player2.buttons[correct_answer_index].light.turn_off() elif pin_number == self.player2.buttons[ wrong_answer_index].switch.pin_number: self.player2.buttons[wrong_answer_index].light.turn_on() print "Player 2 got the WRONG answer!" #print "The answer was: {}".format(question.correct_answer) self.gui.update_question( "%s\n\nThe correct answer was: %s\n\nPlayer 1 has 3 seconds to race!" % (question.text, question.correct_answer)) self.player1.car.drive(3) self.player2.buttons[wrong_answer_index].light.turn_off() elif pin_number == self.buttons[4].switch.pin_number: self.buttons[4].light.turn_on() print "PASS" #print "The answer was: {}".format(question.correct_answer) time.sleep(1) self.buttons[4].light.turn_off() else: raise UnknownButtonError("detected change on pin: %d" % pin_number) # wait until nothing is pressed pin_bit_pattern = pfio.read_input() while pin_bit_pattern != 0: pin_bit_pattern = pfio.read_input() # should we keep playing? if self.stopped(): break
def AnyButtonPressed(): button = pfio.read_input() while button == 0: button = pfio.read_input() pin_number = pfio.get_pin_number(button) return pin_number