{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Buttons and LEDs demonstration\n",
    "\n",
    "This demo shows how to use push-buttons (BTN0-3), LEDs (LD0-3), and RGB LEDs (LD4-5) on the PYNQ-Z1. \n",
    "\n",
    "You can do the following to control the LEDs or RGB LEDs:\n",
    "    \n",
    "    Button 0 pressed:        RGB LEDs change color.\n",
    "    Button 1 pressed:        LEDs shift from right to left (LD0 -> LD3).\n",
    "    Button 2 pressed:        LEDs shift from left to right (LD3 -> LD0).\n",
    "    Button 3 pressed:        Turns off all the LEDS and ends this demo.\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "End of this demo ...\n"
     ]
    }
   ],
   "source": [
    "from time import sleep\n",
    "from pynq.board import LED\n",
    "from pynq.board import RGBLED\n",
    "from pynq.board import Button\n",
    "\n",
    "Delay1 = 0.3\n",
    "Delay2 = 0.1\n",
    "color = 0\n",
    "leds = [LED(index) for index in range(4)]\n",
    "rgbleds = [RGBLED(index) for index in [4,5]] \n",
    "btns = [Button(index) for index in range(4)]\n",
    "        \n",
    "for led in leds:\n",
    "    led.on()    \n",
    "while (btns[3].read()==0):\n",
    "    if (btns[0].read()==1):\n",
    "        color = (color+1) % 8\n",
    "        for rgbled in rgbleds:\n",
    "            rgbled.write(color)\n",
    "        sleep(Delay1)\n",
    "        \n",
    "    elif (btns[1].read()==1):\n",
    "        for led in leds:\n",
    "            led.off()\n",
    "        sleep(Delay2)\n",
    "        for led in leds:\n",
    "            led.toggle()\n",
    "            sleep(Delay2)\n",
    "            \n",
    "    elif (btns[2].read()==1):\n",
    "        for led in leds[::-1]:\n",
    "            led.off()\n",
    "        sleep(Delay2)\n",
    "        for led in leds[::-1]:\n",
    "            led.toggle()\n",
    "            sleep(Delay2)                  \n",
    "    \n",
    "print('End of this demo ...')\n",
    "for led in leds:\n",
    "    led.off()\n",
    "for rgbled in rgbleds:\n",
    "    rgbled.off()\n",
    "    \n",
    "del leds,btns"
   ]
  }
 ],
 "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
}
