{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Uk\u00e1zka pou\u017eit\u00ed n\u00e1stroje IPython na manipulaci se senzorov\u00fdmi daty\n",
      "=======\n",
      "\n",
      "P\u0159\u00edklad vyu\u017e\u00edv\u00e1 modulovou stavebnici MLAB a jej\u00ed knihovnu https://github.com/MLAB-project/MLAB-I2c-modules \n",
      "\n",
      "Zprovozn\u011bn\u00ed demo k\u00f3du\n",
      "---------------------\n",
      "\n",
      "Nejd\u0159\u00edve zjist\u00edme zda m\u00e1me p\u0159\u00edstup pro z\u00e1pis a \u010dten\u00ed do syst\u00e9mov\u00e9ho za\u0159\u00edzen\u00ed. A jak\u00e9 \u010d\u00edslo m\u00e1 I\u00b2C sb\u011brnice na  kterou m\u00e1me p\u0159ipojen\u00e1 \u010didla. \n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!i2cdetect -l"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "i2c-0\ti2c       \tintel drm CRTDDC_A              \tI2C adapter\r\n",
        "i2c-1\ti2c       \tintel drm LVDSBLC_B             \tI2C adapter\r\n",
        "i2c-2\ti2c       \tintel drm LVDSDDC_C             \tI2C adapter\r\n",
        "i2c-3\ti2c       \tintel drm HDMIB                 \tI2C adapter\r\n",
        "i2c-4\ti2c       \tDPDDC-B                         \tI2C adapter\r\n",
        "i2c-5\ti2c       \ti2c-tiny-usb at bus 002 device 002\tI2C adapter\r\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Proto\u017ee pro p\u0159ipojen\u00ed \u010didel k po\u010d\u00edta\u010di pou\u017e\u00edv\u00e1me adapt\u00e9r i2c-tiny-usb. Vid\u00edme, \u017ee sb\u011brnice m\u00e1 aktu\u00e1ln\u011b ozna\u010den\u00ed nap\u0159\u00edklad i2c-8. \n",
      "\n",
      "V p\u0159\u00edpad\u011b, \u017ee v\u00fd\u0161e uveden\u00fd p\u0159\u00edklad vr\u00e1t\u00ed chybu, nebo pojmenov\u00e1n\u00ed \"unknown\" tak nem\u00e1me p\u0159\u00edstup k syst\u00e9mov\u00fdm rozhran\u00edm. Ten z\u00edsk\u00e1me vytvo\u0159en\u00edm souboru s n\u00e1sleduj\u00edc\u00edm obsahem ve slo\u017ece:  /etc/udev/rules.d/i2c-devices.rules"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "KERNEL==\"i2c-[0-9]*\", GROUP=\"i2c\""
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Toto ozna\u010den\u00ed budeme je\u0161t\u011b d\u00e1le pot\u0159ebovat, proto si jej ulo\u017e\u00edme da prom\u011bnn\u00e9. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "port = 5"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Budeme pokra\u010dovat na\u010dten\u00edm pot\u0159ebn\u00fdch modul\u016f pro zach\u00e1zen\u00ed s I\u00b2C sn\u00edma\u010di."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import time\n",
      "import datetime\n",
      "import sys\n",
      "\n",
      "from pymlab import config\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Nyn\u00ed si nadefinujeme strukturu p\u0159ipojen\u00ed jednotliv\u00fdch \u010didel na I\u00b2C sb\u011brnici."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cfg = config.Config(\n",
      "    port = port,\n",
      "    bus = [\n",
      "        {\n",
      "            \"type\": \"i2chub\",\n",
      "            \"address\": 0x72,\n",
      "            \n",
      "            \"children\": [\n",
      "                {\"name\": \"mag\", \"type\": \"mag01\" , \"channel\": 6, },   \n",
      "            ],\n",
      "        },\n",
      "    ],\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Tuto strukturu inicializujeme, aby jsme dos\u00e1hli definovan\u00e9 konfigurace \u010didel."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cfg.initialize()\n",
      "mag_sensor = cfg.get_device(\"mag\")\n",
      "time.sleep(0.5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Nyn\u00ed u\u017e m\u016f\u017eeme p\u0159\u00edmo komunikovat se za\u0159\u00edzen\u00edm pojmenovan\u00fdm jako gauge."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "MEASUREMENTS = 100\n",
      "x = np.zeros(MEASUREMENTS)\n",
      "y = np.zeros(MEASUREMENTS)\n",
      "z = np.zeros(MEASUREMENTS)\n",
      "\n",
      "\n",
      "for n in range(MEASUREMENTS):\n",
      "    mag_sensor.route()  #V p\u0159\u00edpad\u011b v\u00edce \u010didel je pot\u0159eba ke ka\u017ed\u00e9mu p\u0159ed jeho pou\u017eit\u00edm nechat vyroutovat cesutu na sb\u011brnici.\n",
      "    (x[n], y[n], z[n]) = mag_sensor.axes()\n",
      "    print(n, x[n], y[n], z[n])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.savez(\"data_ground\", t, p)\n",
      "#np.savez(\"data_top\", t, p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "amin(p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "amax(p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "std(p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "plt.plot(p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "plt.plot(t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    }
   ],
   "metadata": {}
  }
 ]
}