{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Buzzer v1.2\n",
    "\n",
    "This example shows how to use the [Grove Buzzer v1.2](http://www.seeedstudio.com/wiki/Grove_-_Buzzer).\n",
    "\n",
    "A Grover Buzzer, and PYNQ Grove Adapter are required. \n",
    "\n",
    "To set up the Pynq-Z1 for this notebook, the PYNQ Grove Adapter is connected to PMODB and the Grove Buzzer is connected to G1 on the PYNQ Grove Adapter. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq import Overlay\n",
    "Overlay(\"base.bit\").download()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Illustrate playing a pre-defined melody"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.iop import Grove_Buzzer\n",
    "from pynq.iop import PMODB\n",
    "from pynq.iop import PMOD_GROVE_G1 \n",
    "\n",
    "grove_buzzer = Grove_Buzzer(PMODB, PMOD_GROVE_G1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Play a piece of music"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "grove_buzzer.play_melody()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Generate a tone of desired period and for a desired number of times\n",
    "The tone_period is in microseconds and the 50% duty cycle will be generated for the given tone_period "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Play a tone\n",
    "tone_period = 1200\n",
    "num_cycles = 500\n",
    "grove_buzzer.play_tone(tone_period,num_cycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Controlling the tone\n",
    "This example will use a grove potentiometer to control the tone of the sound. Plug the potentiometer into A1 group on the shield."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.iop import ARDUINO\n",
    "from pynq.iop import Arduino_Analog\n",
    "from pynq.iop import ARDUINO_GROVE_A1\n",
    "\n",
    "analog1 = Arduino_Analog(ARDUINO, ARDUINO_GROVE_A1)\n",
    "\n",
    "rounds = 200\n",
    "\n",
    "for i in range(rounds):\n",
    "    tone_period = int(analog1.read_raw()[0]/5)\n",
    "    num_cycles = 500\n",
    "    grove_buzzer.play_tone(tone_period,50)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
