def button_wait(b, o, press, push_time, timeout_time): def push(): o.off() time.sleep(MINIMUM_BUTTON_PRESS_PERIOD) o.on() kwargs = {} if timeout_time: kwargs['timeout'] = timeout_time kwargs['press'] = press o.on() time.sleep(MINIMUM_BUTTON_PRESS_PERIOD) # clear queue if isinstance(b, list): for button in b: button.presses() else: b.presses() try: t = Timer(push_time, push) t.start() if isinstance(b, list): wait_ret = Button.wait_many(b, **kwargs) else: wait_ret = b.wait(**kwargs) finally: t.join() print("button wait returned: ", wait_ret) return wait_ret
play_order = [] failed = False while not failed: play_order += [randrange(4)] # Play sequence for i in play_order: lights[i].on() notes[i].play(0.4).wait() lights[i].off() time.sleep(0.2) # Wait for user to repeat for i in play_order: button_pressed = Button.wait_many(buttons, timeout=3) if button_pressed != i: failed = True break # Light and play while button is pressed. lights[button_pressed].on() notes[button_pressed].play(duration=None) buttons[button_pressed].wait(press=False) time.sleep(0.2) lights[button_pressed].off() notes[button_pressed].stop() if not failed: time.sleep(1.0)