{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Temperature Sensor 1.2\n",
    "\n",
    "This example shows how to use the [Grove Temperature Sensor v1.2](http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor_V1.2) on the Pynq-Z1 board. You will also see how to plot a graph using matplotlib. The Grove Temperature sensor produces an analog signal, and requires an ADC. \n",
    "\n",
    "A [Grove Temperature sensor](http://www.seeedstudio.com/depot/grove-led-bar-p-1178.html) and Pynq Grove Adapter, or Pynq Shield is required. The Grove Temperature Sensor, Pynq Grove Adapter, and Grove I2C ADC are used for this example. \n",
    "\n",
    "You can read a single value of temperature or read multiple values at regular intervals for a desired duration.\n",
    "\n",
    "At the end of this notebook, a Python only solution with single-sample read functionality is provided."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Load overlay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.pl import Overlay\n",
    "Overlay(\"base.bit\").download()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Read single temperature\n",
    "This example shows on how to get a single temperature sample from the Grove TMP sensor.\n",
    "\n",
    "The Grove ADC is assumed to be attached to the GR4 connector of the StickIt. The StickIt module is assumed to be plugged in the 1st PMOD labeled JB. The Grove TMP sensor is connected to the other connector of the Grove ADC.\n",
    "\n",
    "Grove ADC provides a raw sample which is converted into resistance first and then converted into temperature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "27.24 degree Celsius\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "from pynq.iop import Grove_TMP\n",
    "from pynq.iop import PMODB\n",
    "from pynq.iop import PMOD_GROVE_G4\n",
    "\n",
    "tmp = Grove_TMP(PMODB, PMOD_GROVE_G4)\n",
    "temperature = tmp.read()\n",
    "print(float(\"{0:.2f}\".format(temperature)),'degree Celsius')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Start logging once every 100ms for 10 seconds\n",
    "Executing the next cell will start logging the temperature sensor values every 100ms, and will run for 10s. You can try touch/hold the temperature sensor to vary the measured temperature.\n",
    "\n",
    "You can vary the logging interval and the duration by changing the values 100 and 10 in the cellbelow. The raw samples are stored in the internal memory, and converted into temperature values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYEAAAEKCAYAAAD0Luk/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X+cHHWd5/HXZxhgScbkWKORQAjDoBIISnh4nAsrThCV\n81C49Q5lJ55hT9h1XfSiuOHHzs6M2UVx4TQi3h7oEuWniotw6p6BZQdOdqOiQQlEDZOZCQYMRgOT\nIXuQMJ/9o6o7lU71TM90dXf9eD8fj35Md01X97e+1V2frvp8f5i7IyIixdTW6gKIiEjrKAiIiBSY\ngoCISIEpCIiIFJiCgIhIgSkIiIgUmIKAiGBmbzezza0uhzSfgoAAYGbvNbP1ZjZuZr8ys38xsw+2\nuEwLzWyXmY2FfyfC8pWWnd7K8tXDzA4Nt2dBE9/zj81sT1h3z5rZw2b29shTauo0ZGa3m9kVDSqm\nNJmCgGBmHwM+A1wNzHf3VwF/ApxmZgdXWafhnx13f9LdX+buc9z9ZQQHqZMiyx5qdBlmyswOmuop\n1HjQneQ9ZrIP/snd5wCHA3cAXzezWfWUQ7JNQaDgzGwOMAB80N3vcvfnAdz9J+7+PnffEz7vJjP7\ngpl928x2Ad1mNsfMvmJmz5jZsJldGT73EDPbaWYnRN5nnpntNrN54eNzzGxD+LzvmdlJtRQ3vEXL\n/ztm9lkz22pmT5nZ50qBq3SJw8yuNLNfm9mTZvYOMzvXzJ4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xPxfHAiuBRcACgjOCHgpY\nF5Ooa9tbHQS2AUdHHh8VLiuM8BT3TuBmd787XLzdzOaH/38V8EyrytdEpwPvMrMtwO3AmWZ2M/Cr\nAtbFL4En3f3h8PE3CIJCET8XbwAecvffuvtLwF3AaRSzLkqqbfs2YGHkeTUdT1sdBH4IHGdmi8zs\nEOC9wD0tLlOz/R3wuLuviSy7h2CWRID3A3dXrpQ37n6Fux/t7scSfA7ud/f3Af+H4tXFduBJM3tN\nuOgtwGMU8HMB/Bx4o5n9TpjkfAtBw4Ei1YWx/9lxtW2/B3hv2HqqEzgO+MGUL97qHsNmdjZBS4g2\n4Evu/qmWFqiJzOx04EHgUYJTOgeuINhxXyOI6qPA+e7+bKvK2Wxm9mbgY+7+LjP7XQpYF2b2eoIE\n+cHAFuBC4CCKWRcfJzjovQRsAD4AvIwC1IWZ3QZ0Ay8HtgN9wDeBrxOz7WZ2OfDfgT0El5fXTfke\nrQ4CIiLSOq2+HCQiIi2kICAiUmAKAiIiBaYgICJSYAoCIiIFpiAgIlJgCgIiIgWmICAiUmD/BrWu\nl8kOquTXAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x316f01f0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import time\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "tmp.set_log_interval_ms(100)\n",
    "tmp.start_log()\n",
    "# Change input during this time\n",
    "time.sleep(10)\n",
    "tmp_log = tmp.get_log()\n",
    "\n",
    "plt.plot(range(len(tmp_log)), tmp_log, 'ro')\n",
    "plt.title('Grove Temperature Plot')\n",
    "min_tmp_log = min(tmp_log)\n",
    "max_tmp_log = max(tmp_log)\n",
    "plt.axis([0, len(tmp_log), min_tmp_log, max_tmp_log])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 4. A Pure Python class to exercise the AXI IIC Controller inheriting from PMOD_IIC\n",
    "This class is ported from http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "from math import log\n",
    "from pynq.iop import PMOD_GROVE_G3\n",
    "from pynq.iop import PMOD_GROVE_G4\n",
    "from pynq.iop.pmod_iic import Pmod_IIC\n",
    "\n",
    "class Python_Grove_TMP(Pmod_IIC):\n",
    "    \"\"\"This class controls the grove temperature sensor.\n",
    "    \n",
    "    This class inherits from the PMODIIC class.\n",
    "    \n",
    "    Attributes\n",
    "    ----------\n",
    "    iop : _IOP\n",
    "        The _IOP object returned from the DevMode.\n",
    "    scl_pin : int\n",
    "        The SCL pin number.\n",
    "    sda_pin : int\n",
    "        The SDA pin number.\n",
    "    iic_addr : int\n",
    "        The IIC device address.\n",
    "    \n",
    "    \"\"\"\n",
    "    def __init__(self, pmod_id, gr_pins, model = 'v1.2'): \n",
    "        \"\"\"Return a new instance of a grove OLED object. \n",
    "        \n",
    "        Parameters\n",
    "        ----------\n",
    "        pmod_id : int\n",
    "            The PMOD ID (1, 2) corresponding to (PMODA, PMODB).\n",
    "        gr_pins: list\n",
    "            The group pins on Grove Adapter. G3 or G4 is valid.\n",
    "        model : string\n",
    "            Temperature sensor model (can be found on the device).\n",
    "            \n",
    "        \"\"\"\n",
    "        if gr_pins in [PMOD_GROVE_G3, PMOD_GROVE_G4]:\n",
    "            [scl_pin,sda_pin] = gr_pins\n",
    "        else:\n",
    "            raise ValueError(\"Valid group numbers are G3 and G4.\")\n",
    "            \n",
    "        # Each revision has its own B value\n",
    "        if model == 'v1.2':\n",
    "            # v1.2 uses thermistor NCP18WF104F03RC\n",
    "            self.bValue = 4250\n",
    "        elif model == 'v1.1':\n",
    "            # v1.1 uses thermistor NCP18WF104F03RC\n",
    "            self.bValue = 4250\n",
    "        else:\n",
    "            # v1.0 uses thermistor TTC3A103*39H\n",
    "            self.bValue = 3975\n",
    "        \n",
    "        super().__init__(pmod_id, scl_pin, sda_pin, 0x50)\n",
    "        \n",
    "        # Initialize the Grove ADC         \n",
    "        self.send([0x2,0x20]);   \n",
    "        \n",
    "    def read(self):\n",
    "        \"\"\"Read temperature in Celsius from grove temperature sensor.\n",
    "        \n",
    "        Parameters\n",
    "        ----------\n",
    "        None\n",
    "        \n",
    "        Returns\n",
    "        -------\n",
    "        float\n",
    "            Temperature reading in Celsius.\n",
    "        \n",
    "        \"\"\"\n",
    "        val = self._read_grove_adc()\n",
    "        R = 4095.0/val - 1.0\n",
    "        temp = 1.0/(log(R)/self.bValue + 1/298.15)-273.15\n",
    "        return temp\n",
    "        \n",
    "    def _read_grove_adc(self):       \n",
    "       self.send([0])\n",
    "       bytes = self.receive(2)\n",
    "       return 2*(((bytes[0] & 0x0f) << 8) | bytes[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "26.78 degree Celsius\n"
     ]
    }
   ],
   "source": [
    "from pynq import PL\n",
    "\n",
    "# Flush IOP state\n",
    "PL.reset_ip_dict()\n",
    "\n",
    "py_tmp = Python_Grove_TMP(PMODB, PMOD_GROVE_G4)\n",
    "temperature = py_tmp.read()\n",
    "print(float(\"{0:.2f}\".format(temperature)),'degree Celsius')"
   ]
  }
 ],
 "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
}
