{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Light Sensor 1.1\n",
    "\n",
    "This example shows how to use the [Grove Light Sensor v1.1](http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor). You will also see how to plot a graph using matplotlib.\n",
    "\n",
    "The Grove Light Sensor produces an analog signal which requires an ADC.\n",
    "\n",
    "The Grove Light Sensor, PYNQ Grove Adapter, and Grove I2C ADC are used for this example. \n",
    "\n",
    "When the ambient light intensity increases, the resistance of the LDR or Photoresistor will decrease. This means that the output signal from this module will be HIGH in bright light, and LOW in the dark. Values for the sensor ranges from ~5.0 (bright) to >35.0 (dark)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Load overlay\n",
    "Download base overlay."
   ]
  },
  {
   "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": [
    "### 2. Read single luminance value\n",
    "Now read from the Grove Light sensor which is connected to the ADC. In this example, the PYNQ Grove Adapter is connected to PMODA interface on the board.\n",
    "\n",
    "The Grove I2C ADC is used as a bridge between G4 on the Grove Adapter and the Grove Light Sensor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.71\n"
     ]
    }
   ],
   "source": [
    "from pynq.iop import Grove_Light\n",
    "from pynq.iop import PMODA\n",
    "from pynq.iop import PMOD_GROVE_G4\n",
    "\n",
    "lgt = Grove_Light(PMODA, PMOD_GROVE_G4)\n",
    "\n",
    "sensor_val = lgt.read()\n",
    "print(sensor_val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Plot the light intensity over time\n",
    "This Python code will do multiple light measurements over a 10 second period. \n",
    "\n",
    "To change the light intensity, cover and uncover the light sensor. In typical ambient light, there is no need to provide an external light source, as the sensor is already reading at full scale. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEKCAYAAADpfBXhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGIlJREFUeJzt3X9w5Hd93/HnWzWkWIcPg8fnSDa6YzMJOCYYpklMyA9d\n8QGJO7ZnOqHYOkA0Q5pMOZgDgg1B1akKmQIXKDGhLcH4/DtNSFMb0hbO46iJCRpCsDE2xgFZJxup\n1sXgcHeCSUj07h+70q10+rHSan/ou8/HjOZW3/3ufj/7udVrP/v5fL6fb2QmkqRi6Wp1ASRJW89w\nl6QCMtwlqYAMd0kqIMNdkgrIcJekAjLc1ZEi4tqI+D817vvGiPiLBpZlPiJe0KjnV2cy3FW3iHhd\nRIxHxKmIeDIivhARv97qcsHqwZmZd2TmazbwVKueEBIRfxYR/3aN+/sq5ThR+XksIq6r5bmXPU9D\nP2RULIa76hIR7wA+DLwf2JWZFwC/BvxMRDxjlcc0833XLmfpJbAzM88BrgX+Q0S8qnJf1PgcQfu8\nHrU5w12bFhHnACPAr2fmn2TmHEBmfiUzX5+ZP6jsd1NEfCwi/jQiTgL9EXFORNwSEccjYjIifrOy\n7zMj4umIuLjqOOdFxPci4rzK7/8qIu6v7HdfRLx4rWKuUvYlreCIeFVEfL3ynL8XEWPLWuMRER+M\niO9ExEREvLqy8beAnwM+WmmV/+56ZcnMceBh4JKV6nSVenkh8F+Al0fEyYj4zhrHkQx31eXlwDOB\nu2vY9xpgNDOfDXwe+CjwbGA30A+8ISLelJn/APxxZf8FrwXGMvOpiHgpcCPwZuC5wH8D7l7tW8I6\nEsofHsAfAdcBzwMerby2aj8NPFK5/4PAJwEy873AXwBvycxzMvOtaxwvKsd7BXAx8OUV9lmtXr5O\n+RvRFzLz2Zn53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u8GXleZTbQH+BHgi+s+ez1rF6z1A7yG8n/gN4Dr\nG3Wcdvyh3Fr9J+AB4H7gy5X6eC5wT6VePgc8p9VlbXK9/AJwd+V2R9YF5Rkyf1V5b/wPYGcH18Vv\nUP5we5DyAOIzOqUugDuAGeDvKY87vAk4d7XXTnnmzDcpT854VS3HcPkBSSogB1QlqYAMd0kqIMNd\nkgrIcJekAjLcJamADHdJKiDDXZIKyHCXpAL6/93ir8gtwXg6AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x315ad610>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import time\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "lgt.set_log_interval_ms(100)\n",
    "lgt.start_log()\n",
    "# Change input during this time\n",
    "time.sleep(10)\n",
    "r_log = lgt.get_log()\n",
    "\n",
    "plt.plot(range(len(r_log)), r_log, 'ro')\n",
    "plt.title('Grove Light Plot')\n",
    "min_r_log = min(r_log)\n",
    "max_r_log = max(r_log)\n",
    "plt.axis([0, len(r_log), min_r_log, max_r_log])\n",
    "plt.show()"
   ]
  }
 ],
 "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
}
