amp_skin.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. The AMP audio skin
  2. ==================
  3. Soldering and using the AMP audio skin.
  4. .. image:: img/skin_amp_1.jpg
  5. :alt: AMP skin
  6. :width: 250px
  7. .. image:: img/skin_amp_2.jpg
  8. :alt: AMP skin
  9. :width: 250px
  10. The following video shows how to solder the headers, microphone and speaker onto the AMP skin.
  11. .. raw:: html
  12. <iframe style="margin-left:3em;" width="560" height="315" src="http://www.youtube.com/embed/fjB1DuZRveo?rel=0" frameborder="0" allowfullscreen></iframe>
  13. For circuit schematics and datasheets for the components on the skin see :ref:`hardware_index`.
  14. Example code
  15. ------------
  16. The AMP skin has a speaker which is connected to ``DAC(1)`` via a small
  17. power amplifier. The volume of the amplifier is controlled by a digital
  18. potentiometer, which is an I2C device with address 46 on the ``IC2(1)`` bus.
  19. To set the volume, define the following function::
  20. import pyb
  21. def volume(val):
  22. pyb.I2C(1, pyb.I2C.MASTER).mem_write(val, 46, 0)
  23. Then you can do::
  24. >>> volume(0) # minimum volume
  25. >>> volume(127) # maximum volume
  26. To play a sound, use the ``write_timed`` method of the ``DAC`` object.
  27. For example::
  28. import math
  29. from pyb import DAC
  30. # create a buffer containing a sine-wave
  31. buf = bytearray(100)
  32. for i in range(len(buf)):
  33. buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
  34. # output the sine-wave at 400Hz
  35. dac = DAC(1)
  36. dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
  37. You can also play WAV files using the Python ``wave`` module. You can get
  38. the wave module `here <http://micropython.org/resources/examples/wave.py>`__ and you will also need
  39. the chunk module available `here <http://micropython.org/resources/examples/chunk.py>`__. Put these
  40. on your pyboard (either on the flash or the SD card in the top-level directory). You will need an
  41. 8-bit WAV file to play, such as `this one <http://micropython.org/resources/examples/test.wav>`_,
  42. or to convert any file you have with the command::
  43. avconv -i original.wav -ar 22050 -codec pcm_u8 test.wav
  44. Then you can do::
  45. >>> import wave
  46. >>> from pyb import DAC
  47. >>> dac = DAC(1)
  48. >>> f = wave.open('test.wav')
  49. >>> dac.write_timed(f.readframes(f.getnframes()), f.getframerate())
  50. This should play the WAV file. Note that this will read the whole file into RAM
  51. so it has to be small enough to fit in it.
  52. To play larger wave files you will have to use the micro-SD card to store it.
  53. Also the file must be read and sent to the DAC in small chunks that will fit
  54. the RAM limit of the microcontroller. Here is an example function that can
  55. play 8-bit wave files with up to 16kHz sampling::
  56. import wave
  57. from pyb import DAC
  58. from pyb import delay
  59. dac = DAC(1)
  60. def play(filename):
  61. f = wave.open(filename, 'r')
  62. total_frames = f.getnframes()
  63. framerate = f.getframerate()
  64. for position in range(0, total_frames, framerate):
  65. f.setpos(position)
  66. dac.write_timed(f.readframes(framerate), framerate)
  67. delay(1000)
  68. This function reads one second worth of data and sends it to DAC. It then waits
  69. one second and moves the file cursor to the new position to read the next second
  70. of data in the next iteration of the for-loop. It plays one second of audio at
  71. a time every one second.