Created
October 22, 2015 21:03
-
-
Save ramalho/3e2397138d69c0d2c114 to your computer and use it in GitHub Desktop.
Genius clone on Raspberry Pi with Pingo.io, edited live on Midori
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import random | |
| import pingo | |
| import itertools | |
| rpi = pingo.detect.get_board() | |
| class Buzzer(object): | |
| def __init__(self): | |
| self.pin = rpi.gpio[27] | |
| self.pin.mode = pingo.PWM | |
| self.pin.value = 0 | |
| def on(self, frequency): | |
| self.pin.value = 50 | |
| self.pin.frequency = frequency | |
| def off(self): | |
| self.pin.value = 0 | |
| buzzer = Buzzer() | |
| class Led(object): | |
| def __init__(self, control_pin, button_pin, frequency): | |
| self.control_pin = rpi.gpio[control_pin] | |
| self.control_pin.mode = pingo.OUT | |
| self.button_pin = rpi.gpio[button_pin] | |
| self.button_pin.mode = pingo.IN | |
| self.frequency = frequency | |
| def on(self): | |
| self.control_pin.high() | |
| buzzer.on(self.frequency) | |
| def off(self): | |
| self.control_pin.low() | |
| buzzer.off() | |
| def is_on(self): | |
| return self.button_pin.state == 'HIGH' | |
| def buzz(self): | |
| buzzer.on(self.frequency) | |
| def play(self): | |
| self.on() | |
| time.sleep(0.5) | |
| self.off() | |
| red = Led(22, 26, 500) | |
| green = Led(5, 7, 5500) | |
| yellow = Led(8, 19, 10000) | |
| leds = [red, green, yellow] | |
| # import ipdb; ipdb.set_trace() | |
| def game_on(leds): | |
| length = 1 | |
| win_length = 10 | |
| sleep = .3 | |
| pattern = [] | |
| try: | |
| while True: | |
| append_pattern(leds, pattern) | |
| print('play pattern', pattern) | |
| play(pattern, sleep) | |
| won = read_pattern(leds, pattern) | |
| if won: | |
| length += 1 | |
| else: | |
| lost() | |
| pattern = [] | |
| length = 1 | |
| if length > win_length: | |
| win() | |
| pattern = [] | |
| length = 1 | |
| if length > 3: | |
| sleep = .15 | |
| time.sleep(1) | |
| finally: | |
| buzzer.off() | |
| def append_pattern(leds, pattern): | |
| pattern.append(random.choice(leds)) | |
| def play(pattern, sleep): | |
| for led in pattern: | |
| led.play() | |
| time.sleep(sleep) | |
| def lost(): | |
| print('Lame...') | |
| red.play() | |
| time.sleep(0.3) | |
| red.play() | |
| time.sleep(0.3) | |
| red.play() | |
| time.sleep(0.3) | |
| def win(): | |
| print('YEY! \o/') | |
| green.play() | |
| time.sleep(0.2) | |
| green.play() | |
| time.sleep(0.2) | |
| green.play() | |
| time.sleep(0.2) | |
| def read_pattern(leds, pattern): | |
| inputs = [] | |
| print('listening...') | |
| while True: | |
| for led in leds: | |
| if led.is_on(): | |
| print(led) | |
| right_led = pattern[len(inputs)] | |
| if led != right_led: | |
| print('na...') | |
| return False | |
| print('go on..') | |
| led.buzz() | |
| time.sleep(.6) | |
| inputs.append(led) | |
| buzzer.off() | |
| if(len(inputs) == len(pattern)): | |
| return True | |
| game_on(leds) | |
| def button_test(): | |
| try: | |
| while True: | |
| is_a_led_on = False | |
| for led in leds: | |
| if led.is_on(): | |
| led.buzz() | |
| is_a_led_on = True | |
| if not is_a_led_on: | |
| buzzer.off() | |
| finally: | |
| buzzer.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment