|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "85294ea7", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### Button Tests\n", |
| 9 | + "\n", |
| 10 | + "Here is some code to test and play with the button on top of your Voice Kit.\n", |
| 11 | + "\n", |
| 12 | + "##### Lights" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "id": "6e2301d6", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "from aiy.leds import Leds, Color\n", |
| 23 | + "import time" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "id": "88487129", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "First we turn the button red, wait for one second and then turn it off again." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "id": "494c7bee", |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "with Leds() as buttonLight:\n", |
| 42 | + " buttonLight.update(buttonLight.rgb_on(Color.RED))\n", |
| 43 | + " time.sleep(1.0)\n", |
| 44 | + " buttonLight.update(buttonLight.rgb_off())" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "id": "e66dbf08", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "This time we make a list of colors and then crcle through it lighting up the button in all specified colors." |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "id": "74f24845", |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "colors = [Color.RED, Color.GREEN, Color.BLUE, Color.WHITE]\n", |
| 63 | + "\n", |
| 64 | + "with Leds() as buttonLight:\n", |
| 65 | + " print('Enjoy the light show')\n", |
| 66 | + " \n", |
| 67 | + " for c in colors:\n", |
| 68 | + " buttonLight.update(buttonLight.rgb_on(c))\n", |
| 69 | + " time.sleep(0.5)\n", |
| 70 | + "\n", |
| 71 | + " buttonLight.update(buttonLight.rgb_off())" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "id": "1bf6cc7f", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "##### Pressing the button\n", |
| 80 | + "\n", |
| 81 | + "We can detect of the button is pressed or released and then execute a callback function. Here we define two of those callback functions `lightOn()` and `lightOff()`. Then we enter an infinite loop and if the button is pressed or released we run the callback function. Run the next two cells and try." |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "id": "425474a8", |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "from aiy.pins import BUTTON_GPIO_PIN\n", |
| 92 | + "from gpiozero import Button" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "id": "11153ec6", |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "def lightOn():\n", |
| 103 | + " buttonLight.update(buttonLight.rgb_on(Color.RED))\n", |
| 104 | + " \n", |
| 105 | + "def lightOff():\n", |
| 106 | + " buttonLight.update(buttonLight.rgb_off())\n", |
| 107 | + " \n", |
| 108 | + "with Leds() as buttonLight, Button(BUTTON_GPIO_PIN) as button:\n", |
| 109 | + " try:\n", |
| 110 | + " print('Press the button. I know you are dying to :)')\n", |
| 111 | + " while True:\n", |
| 112 | + " button.when_pressed=lightOn\n", |
| 113 | + " button.when_released=lightOff\n", |
| 114 | + " except KeyboardInterrupt:\n", |
| 115 | + " print('Bye')" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "id": "e646d1a9", |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [] |
| 125 | + } |
| 126 | + ], |
| 127 | + "metadata": { |
| 128 | + "kernelspec": { |
| 129 | + "display_name": "Python 3 (ipykernel)", |
| 130 | + "language": "python", |
| 131 | + "name": "python3" |
| 132 | + }, |
| 133 | + "language_info": { |
| 134 | + "codemirror_mode": { |
| 135 | + "name": "ipython", |
| 136 | + "version": 3 |
| 137 | + }, |
| 138 | + "file_extension": ".py", |
| 139 | + "mimetype": "text/x-python", |
| 140 | + "name": "python", |
| 141 | + "nbconvert_exporter": "python", |
| 142 | + "pygments_lexer": "ipython3", |
| 143 | + "version": "3.7.3" |
| 144 | + } |
| 145 | + }, |
| 146 | + "nbformat": 4, |
| 147 | + "nbformat_minor": 5 |
| 148 | +} |
0 commit comments