Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pulseio
- import board
- import adafruit_irremote
- from adafruit_circuitplayground.express import cpx
- pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
- decoder = adafruit_irremote.GenericDecode()
- pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
- pulseout = pulseio.PulseOut(pwm)
- encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550], zero=[550, 1700], trail=0)
- rock = [255, 0, 0, 255]
- paper = [0, 255, 0, 255]
- scissors = [0, 0 , 255, 255]
- white = (0, 0, 0)
- red = (255, 0, 0)
- green = (0, 255, 0)
- our_team = rock
- def convert(thing):
- if thing == rock:
- return "r"
- if thing == paper:
- return "g"
- if thing == scissors:
- return "b"
- def simulate(code):
- result = {
- "rr": ("draw", white),
- "rg": ("lose", red),
- "rb": ("win", green),
- "gr": ("win", green),
- "gg": ("draw", white),
- "gb": ("lose", red),
- "br": ("lose", red),
- "bg": ("win", green),
- "bb": ("draw", white)
- }
- k = "{}{}".format(convert(our_team), convert(code))
- try:
- return result[k]
- except KeyError:
- print("getting here")
- return None
- while True:
- print("Starting...")
- pulses = decoder.read_pulses(pulsein)
- try:
- received_code = decoder.decode_bits(pulses, debug=False)
- except adafruit_irremote.IRNECRepeatException:
- continue
- except adafruit_irremote.IRDecodeException as e:
- continue
- print("Recieved: ", received_code)
- result = simulate(received_code)
- if result:
- if result[0] == "draw":
- print("Draw")
- cpx.pixels.fill(result[1])
- if result[0] == "win":
- print("Win")
- cpx.pixels.fill(result[1])
- if result[0] == "lose":
- print("Lose")
- cpx.pixels.fill(result[1])
- if cpx.button_a:
- print("Button A pressed! \n")
- cpx.red_led = True
- encoder.transmit(pulseout, our_team)
- cpx.red_led = False
- time.sleep(0.2)
Add Comment
Please, Sign In to add comment