code.py
 1  # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  # pylint: disable=wrong-import-position
 6  import time
 7  import gc
 8  import pulseio
 9  from busio import I2C
10  import board
11  gc.collect()
12  import adafruit_irremote
13  gc.collect()
14  from adafruit_motor import motor
15  gc.collect()
16  from adafruit_seesaw.seesaw import Seesaw
17  gc.collect()
18  from adafruit_seesaw.pwmout import PWMOut
19  gc.collect()
20  import neopixel
21  gc.collect()
22  import audioio
23  gc.collect()
24  import audiocore
25  gc.collect()
26  
27  
28  print("Blimp!")
29  
30  # Create Infrared reader
31  pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=100, idle_state=True)
32  decoder = adafruit_irremote.GenericDecode()
33  REMOTE_FORWARD = [255, 2, 175, 80]
34  REMOTE_BACKWARD = [255, 2, 239, 16]
35  REMOTE_PAUSE = [255, 2, 127, 128]
36  
37  # Create seesaw object
38  seesaw = Seesaw(I2C(board.SCL, board.SDA))
39  
40  # Create one motor on seesaw PWM pins 22 & 23
41  motor_a = motor.DCMotor(PWMOut(seesaw, 22), PWMOut(seesaw, 23))
42  motor_a.throttle = 0
43  
44  # Neopix!@
45  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
46  
47  # audio file
48  a = audioio.AudioOut(board.A0)
49  
50  def play_audio(wavfile):
51      f = open(wavfile, "rb")
52      wav = audiocore.WaveFile(f)
53      a.play(wav)
54      while a.playing:
55          pass
56      f.close()
57      gc.collect()
58  
59  play_audio("overview.wav")
60  t = time.monotonic()
61  
62  while True:
63      command = None   # assume no remote commands came in
64      if len(pulsein) > 25:  # check in any IR data came in
65          pulses = decoder.read_pulses(pulsein)
66      try:
67          code = decoder.decode_bits(pulses, debug=False)
68          if code in (REMOTE_FORWARD, REMOTE_BACKWARD, REMOTE_PAUSE):
69              # we only listen to a few different codes
70              command = code
71          else:
72              continue
73      # on any failure, lets just restart
74      # pylint: disable=bare-except
75      except:
76          continue
77  
78      if command:
79          if code == REMOTE_FORWARD:
80              play_audio("fan_forward.wav")
81              motor_a.throttle = 1  # full speed forward
82              pixels.fill((255,0,0))
83          elif code == REMOTE_BACKWARD:
84              play_audio("fan_backward.wav")
85              motor_a.throttle = -1  # full speed backward
86              pixels.fill((0,0,255))
87          elif code == REMOTE_PAUSE:
88              motor_a.throttle = 0  # stop motor
89              play_audio("fan_stopped.wav")
90              pixels.fill((0,0,0))
91          time.sleep(0.5)
92  
93      # play yayayay every 3 seconds
94      if (time.monotonic() - t > 3) and motor_a.throttle != 0:
95          t = time.monotonic()
96          play_audio("yayyayyay.wav")