code.py
  1  # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  import gc
  7  from digitalio import DigitalInOut, Direction, Pull
  8  from busio import I2C
  9  from adafruit_seesaw.seesaw import Seesaw
 10  from adafruit_seesaw.pwmout import PWMOut
 11  import touchio
 12  import audioio
 13  import audiocore
 14  import neopixel
 15  import board
 16  
 17  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
 18  pixels.fill((0,0,0))
 19  
 20  # Create seesaw object
 21  i2c = I2C(board.SCL, board.SDA)
 22  seesaw = Seesaw(i2c)
 23  
 24  # switch
 25  switch = DigitalInOut(board.SLIDE_SWITCH)
 26  switch.direction = Direction.INPUT
 27  switch.pull = Pull.UP
 28  
 29  # We need some extra captouches
 30  touch2 = touchio.TouchIn(board.A2)
 31  touch3 = touchio.TouchIn(board.A3)
 32  
 33  # LED for debugging
 34  led = DigitalInOut(board.D13)
 35  led.direction = Direction.OUTPUT
 36  
 37  # Create drive (PWM) object
 38  INFRARED_LED_SS = 13
 39  my_drive = PWMOut(seesaw, INFRARED_LED_SS)    # Drive 1 is on s.s. pin 13
 40  my_drive.frequency = 1000        # Our default frequency is 1KHz
 41  
 42  CAPTOUCH_THRESH = 850
 43  
 44  # Commands, each 8 bit command is preceded by the 5 bit Init sequence
 45  Init = [0, 0, 0, 1, 0]            # This must precede any command
 46  Calibrate = [1, 0, 1, 0, 1, 0, 1, 1]  # the initial calibration
 47  Up = [1, 0, 1, 1, 1, 0, 1, 1]     # Move arms/body down
 48  Down = [1, 1, 1, 1, 1, 0, 1, 1]   # Move arms/body up
 49  Left = [1, 0, 1, 1, 1, 0, 1, 0]   # Twist body left
 50  Right = [1, 1, 1, 0, 1, 0, 1, 0]  # Twist body right
 51  Close = [1, 0, 1, 1, 1, 1, 1, 0]  # Close arms
 52  Open = [1, 1, 1, 0, 1, 1, 1, 0]   # Open arms
 53  Test = [1, 1, 1, 0, 1, 0, 1, 1]   # Turns R.O.B. head LED on
 54  
 55  print("R.O.B. Start")
 56  
 57  
 58  def IR_Command(cmd):
 59      print("Sending ", cmd)
 60      gc.collect()                     # collect memory now, timing specific!
 61      # Output initialization and then command cmd
 62      for val in Init+cmd:             # For each value in initial+command
 63          if val:                      # if it's a one, flash the IR LED
 64              seesaw.analog_write(INFRARED_LED_SS, 65535)  # on
 65              seesaw.analog_write(INFRARED_LED_SS, 0)      # off 2ms later
 66          time.sleep(0.013)       # 17 ms total
 67      # pylint: disable=useless-else-on-loop
 68      else:
 69          time.sleep(0.015)       # 17 ms total
 70  
 71  a = audioio.AudioOut(board.A0)
 72  startfile = "startup.wav"
 73  loopfile = "loop.wav"
 74  with open(startfile, "rb") as f:
 75      wav = audiocore.WaveFile(f)
 76      a.play(wav)
 77      for _ in range(3):
 78          IR_Command(Calibrate)
 79          time.sleep(0.5)
 80      while a.playing:
 81          IR_Command(Open)
 82          time.sleep(1)
 83          IR_Command(Close)
 84          time.sleep(1)
 85  f = open(loopfile, "rb")
 86  wav = audiocore.WaveFile(f)
 87  a.play(wav, loop=True)
 88  
 89  while True:                          # Main Loop poll switches, do commands
 90      led.value = switch.value         # easily tell if we're running
 91      if not switch.value:
 92          continue
 93  
 94      #touch_vals = (touch2.raw_value, touch3.raw_value, seesaw.touch_read(0), seesaw.touch_read(1),
 95      #              seesaw.touch_read(2), seesaw.touch_read(3))
 96      #print(touch_vals)
 97  
 98      if touch2.raw_value > 3000:
 99          print("Open jaws")
100          pixels.fill((50,50,0))
101          IR_Command(Open)             # Button A opens arms
102  
103      elif touch3.raw_value > 3000:
104          print("Close jaws")
105          pixels.fill((0,50,0))
106          IR_Command(Close)            # Button B closes arms
107  
108      elif seesaw.touch_read(0) > CAPTOUCH_THRESH:
109          print("Up")
110          pixels.fill((50,0,50))
111          IR_Command(Up)
112  
113      elif seesaw.touch_read(1) > CAPTOUCH_THRESH:
114          print("Down")
115          pixels.fill((50,50,50))
116          IR_Command(Down)
117  
118      elif seesaw.touch_read(2) > CAPTOUCH_THRESH:
119          print("Left")
120          pixels.fill((50,0,0))
121          IR_Command(Left)
122  
123      elif seesaw.touch_read(3) > CAPTOUCH_THRESH:
124          print("Right")
125          pixels.fill((0,0,50))
126          IR_Command(Right)
127  
128      time.sleep(0.1)
129      pixels.fill((0,0,0))