{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove LED Bar Example\n",
    "\n",
    "This example shows how to use the [Grove LED Bar](http://www.seeedstudio.com/depot/Grove-LED-Bar-v20-p-2474.html) on the Pynq-Z1 board.  The LED bar has 10 LEDs: 8 green LEDs, 1 orange LED, and 1 red LED. The brightness for each LED can be set independantly.\n",
    "\n",
    "For this notebook, a PYNQ Arduino shield is also required.  The LED bar is attached to the G4 connection on the shield. The grove LED bar also works with PMODA and PMODB on the Pynq-Z1 board. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Make sure the base overlay is loaded\n",
    "from pynq import Overlay\n",
    "Overlay(\"base.bit\").download()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Instantiate and reset LED Bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.iop import Grove_LEDbar\n",
    "from pynq.iop import ARDUINO\n",
    "from pynq.iop import ARDUINO_GROVE_G4\n",
    "\n",
    "# Instantiate Grove LED Bar on Arduino shield G4\n",
    "ledbar = Grove_LEDbar(ARDUINO,ARDUINO_GROVE_G4)\n",
    "ledbar.reset()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Turn individual LEDs on or off\n",
    "\n",
    "Write a 10-bit binary pattern, with each bit representing the corresponding LED. 1 = on, 0 = off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "\n",
    "# Light up different bars in a loop\n",
    "for i in range(2):\n",
    "    ledbar.write_binary(0b1010100000)\n",
    "    sleep(0.5)\n",
    "    ledbar.write_binary(0b0000100100)\n",
    "    sleep(0.5)\n",
    "    ledbar.write_binary(0b1010101110)\n",
    "    sleep(0.5)\n",
    "    ledbar.write_binary(0b1111111110)\n",
    "    sleep(0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Set LEDs individually with different brightness levels\n",
    "\n",
    "The brightness of each LED can be set individually by writing a list of 10x 8-bit values to the LED bar. 0 is off, 0xff is full brightness. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Brightness 0-255\n",
    "HIGH = 0xFF\n",
    "MED  = 0xAA\n",
    "LOW  = 0x01\n",
    "OFF  = 0X00\n",
    "\n",
    "brightness = [OFF, OFF, OFF, LOW, LOW, MED, MED, HIGH, HIGH, HIGH]\n",
    "\n",
    "ledbar.write_brightness(0b1111111111,brightness)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.  Set the \"level\" or the number of LEDs which are set \n",
    "\n",
    "A number or level of LEDs can be turned on, started from either end of the LED bar. For example, this feature could be used to indicate the level of something being measured.\n",
    "\n",
    "write_level(level, bright_level, green_to_red)\n",
    "\n",
    "* level is the number of LEDs that are on.\n",
    "* bright_level [0-10] is the level of brightness\n",
    "* green_to_red = 1 means the LEDs start being lit from the \"green\" end of the LED bar\n",
    "* green_to_red = 0 means the LEDs start being lit from the \"red\" end of the LED bar.\n",
    "\n",
    "For example, ledbar.write_level(5,4,1) will light 5 LEDs, to brightness 4 (out of 10) and will start from the Green LED (the LED furthest away from Grove connector on the LED bar module.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range (1,11):\n",
    "    ledbar.write_level(i,3,0)\n",
    "    sleep(0.3)\n",
    "for i in range (1,10):\n",
    "    ledbar.write_level(i,3,1)\n",
    "    sleep(0.3)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. Controlling the LED Bar from the board buttons\n",
    "\n",
    "This cell demonstrates controlling the \"level\" of the LEDs from onboard buttons. \n",
    "\n",
    "* Button 0 to increase level\n",
    "* Button 1 to decrease level\n",
    "* Button 3 to exit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.board import Button\n",
    "\n",
    "btns = [Button(index) for index in range(4)] \n",
    "i = 1\n",
    "ledbar.reset()\n",
    "\n",
    "done = False\n",
    "while not done:\n",
    "    if (btns[0].read()==1):\n",
    "        sleep(0.2)\n",
    "        ledbar.write_level(i,2,1)\n",
    "        i = min(i+1,9)\n",
    "    elif (btns[1].read()==1):\n",
    "        sleep(0.2)\n",
    "        i = max(i-1,0)\n",
    "        ledbar.write_level(i,2,1)\n",
    "    elif (btns[3].read()==1):\n",
    "        ledbar.reset()\n",
    "        done = True"
   ]
  }
 ],
 "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
}
