code.py
  1  # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # CircuitPython 3.0 CRICKIT demo
  6  import time
  7  from adafruit_seesaw.seesaw import Seesaw
  8  from adafruit_seesaw.pwmout import PWMOut
  9  from adafruit_motor import servo
 10  from busio import I2C
 11  import audioio
 12  import audiocore
 13  import microcontroller
 14  import board
 15  
 16  i2c = I2C(board.SCL, board.SDA)
 17  ss = Seesaw(i2c)
 18  
 19  print("Yanny or Laurel data logging!")
 20  
 21  LOOKATPERSON = 90
 22  LOOKLEFT = 60
 23  LOOKRIGHT = 120
 24  
 25  #################### 1 Servo
 26  pwm = PWMOut(ss, 17)
 27  pwm.frequency = 50
 28  myservo = servo.Servo(pwm)
 29  myservo.angle = LOOKATPERSON # introduce yourself
 30  
 31  #################### 2 buttons w/2 LEDs
 32  BUTTON_1 = 2
 33  BUTTON_2 = 3
 34  LED_1    = 8
 35  LED_2    = 9
 36  
 37  # Two buttons are pullups, connect to ground to activate
 38  ss.pin_mode(BUTTON_1, ss.INPUT_PULLUP)
 39  ss.pin_mode(BUTTON_2, ss.INPUT_PULLUP)
 40  # Two LEDs are outputs, on by default
 41  ss.pin_mode(LED_1, ss.OUTPUT)
 42  ss.pin_mode(LED_2, ss.OUTPUT)
 43  ss.digital_write(LED_1, True)
 44  ss.digital_write(LED_2, True)
 45  
 46  #################### log files
 47  logfile = "/log.csv"
 48  # pylint: disable=pointless-statement
 49  # check that we could append if wanted to
 50  try:
 51      fp = open(logfile, "a")
 52      fp.close
 53  # pylint: disable=bare-except
 54  except:
 55      print("File system not writable, halting")
 56      while True:
 57          pass
 58  
 59  #################### Audio files
 60  wavfile = "yanny.wav"
 61  f = open(wavfile, "rb")
 62  wav = audiocore.WaveFile(f)
 63  a = audioio.AudioOut(board.A0)
 64  a.play(wav)
 65  t = time.monotonic()
 66  
 67  # Wait
 68  while time.monotonic() - t < 7.5:
 69      pass
 70  
 71  while time.monotonic() - t < 9.5:
 72      myservo.angle = LOOKLEFT
 73      ss.digital_write(LED_1, True)
 74      ss.digital_write(LED_2, False)
 75      time.sleep(0.5)
 76      myservo.angle = LOOKRIGHT
 77      ss.digital_write(LED_1, False)
 78      ss.digital_write(LED_2, True)
 79      time.sleep(0.5)
 80  
 81  myservo.angle = LOOKATPERSON
 82  
 83  # reset LEDs
 84  ss.digital_write(LED_1, False)
 85  ss.digital_write(LED_2, False)
 86  
 87  selection = None
 88  # wait until
 89  while not selection:
 90      if not ss.digital_read(BUTTON_1):
 91          selection = "Yanny"
 92          ss.digital_write(LED_1, True)
 93          myservo.angle = LOOKLEFT
 94          break
 95      if not ss.digital_read(BUTTON_2):
 96          selection = "Laurel"
 97          ss.digital_write(LED_2, True)
 98          myservo.angle = LOOKRIGHT
 99          break
100      # if we havent selected, wait until they do!
101      if a.playing and time.monotonic() - t > 15.5:
102          a.pause()
103  
104  # now we have a selection!
105  with open(logfile, "a") as fp:
106      print("Writing!"+selection+", 1\n")
107      fp.write(selection+", 1\n")
108      fp.flush()
109  print("Written")
110  
111  # OK play the rest of the music
112  a.resume()
113  while a.playing:
114      pass
115  
116  ss.digital_write(LED_1, False)
117  ss.digital_write(LED_2, False)
118  
119  microcontroller.reset()