Guest User

Untitled

a guest
Sep 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import pulseio
  2. import board
  3. import adafruit_irremote
  4. from adafruit_circuitplayground.express import cpx
  5.  
  6. pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
  7. decoder = adafruit_irremote.GenericDecode()
  8.  
  9. pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
  10. pulseout = pulseio.PulseOut(pwm)
  11. encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550], zero=[550, 1700], trail=0)
  12.  
  13. rock = [255, 0, 0, 255]
  14. paper = [0, 255, 0, 255]
  15. scissors = [0, 0 , 255, 255]
  16.  
  17. white = (0, 0, 0)
  18. red = (255, 0, 0)
  19. green = (0, 255, 0)
  20.  
  21. our_team = rock
  22.  
  23. def convert(thing):
  24. if thing == rock:
  25. return "r"
  26. if thing == paper:
  27. return "g"
  28. if thing == scissors:
  29. return "b"
  30.  
  31. def simulate(code):
  32. result = {
  33. "rr": ("draw", white),
  34. "rg": ("lose", red),
  35. "rb": ("win", green),
  36. "gr": ("win", green),
  37. "gg": ("draw", white),
  38. "gb": ("lose", red),
  39. "br": ("lose", red),
  40. "bg": ("win", green),
  41. "bb": ("draw", white)
  42. }
  43. k = "{}{}".format(convert(our_team), convert(code))
  44. try:
  45. return result[k]
  46. except KeyError:
  47. print("getting here")
  48. return None
  49.  
  50.  
  51. while True:
  52. print("Starting...")
  53. pulses = decoder.read_pulses(pulsein)
  54. try:
  55. received_code = decoder.decode_bits(pulses, debug=False)
  56. except adafruit_irremote.IRNECRepeatException:
  57. continue
  58. except adafruit_irremote.IRDecodeException as e:
  59. continue
  60.  
  61. print("Recieved: ", received_code)
  62. result = simulate(received_code)
  63. if result:
  64. if result[0] == "draw":
  65. print("Draw")
  66. cpx.pixels.fill(result[1])
  67. if result[0] == "win":
  68. print("Win")
  69. cpx.pixels.fill(result[1])
  70. if result[0] == "lose":
  71. print("Lose")
  72. cpx.pixels.fill(result[1])
  73.  
  74. if cpx.button_a:
  75. print("Button A pressed! \n")
  76. cpx.red_led = True
  77. encoder.transmit(pulseout, our_team)
  78. cpx.red_led = False
  79. time.sleep(0.2)
Add Comment
Please, Sign In to add comment