No Description

isd1820.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/python
  2. # Author: Zion Orent <zorent@ics.com>
  3. # Copyright (c) 2015 Intel Corporation.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. import time, sys, atexit
  24. import pyupm_isd1820 as upmIsd1820
  25. # Instantiate a ISD1820 on digital pins 2 (play) and 3 (record)
  26. # This example was tested on the Grove Recorder.
  27. myRecorder = upmIsd1820.ISD1820(2, 3)
  28. doRecord = False
  29. if len(sys.argv) > 1:
  30. doRecord = True
  31. # This lets you run code on exit,
  32. # including functions from myRecorder
  33. def exitHandler():
  34. # turn off whatever we were doing.
  35. if (doRecord):
  36. myRecorder.record(False)
  37. else:
  38. myRecorder.play(False)
  39. print "Exiting"
  40. sys.exit(0)
  41. # Register exit handlers
  42. atexit.register(exitHandler)
  43. # if an argument was specified (any argument), go into record mode,
  44. # else playback a previously recorded sample
  45. print "Supply any argument to the command line to record."
  46. print "Running this example without arguments will play back any "
  47. print "previously recorded sound."
  48. print "There is approximately 10 seconds of recording time.\n"
  49. # depending on what was selected, do it, and sleep for 15 seconds
  50. if (doRecord):
  51. myRecorder.record(True)
  52. else:
  53. myRecorder.play(True)
  54. # There are about 10 seconds of recording/playback time, so we will
  55. # sleep for a little extra time.
  56. print "Sleeping for 15 seconds..."
  57. time.sleep(15)
  58. # exitHandler runs automatically