|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# Copyright (c) 2015 Eric Pascual |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | +# ----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +""" This demo illustrates how to use the two red-green LEDs of the EV3 brick. |
| 27 | +""" |
| 28 | + |
| 29 | +import time |
| 30 | +import math |
| 31 | + |
| 32 | +from ev3dev.ev3 import Leds |
| 33 | + |
| 34 | +print(__doc__.lstrip()) |
| 35 | + |
| 36 | +print('saving current LEDs state') |
| 37 | + |
| 38 | +# save current state |
| 39 | +saved_state = [led.brightness_pct for led in Leds.LEFT + Leds.RIGHT] |
| 40 | + |
| 41 | +Leds.all_off() |
| 42 | +time.sleep(1) |
| 43 | + |
| 44 | +# cycle LEDs like a traffic light |
| 45 | +print('traffic light') |
| 46 | +for _ in range(3): |
| 47 | + for color in (Leds.GREEN, Leds.YELLOW, Leds.RED): |
| 48 | + for group in (Leds.LEFT, Leds.RIGHT): |
| 49 | + Leds.set_color(group, color) |
| 50 | + time.sleep(0.5) |
| 51 | + |
| 52 | +Leds.all_off() |
| 53 | +time.sleep(0.5) |
| 54 | + |
| 55 | +# blink LEDs from side to side now |
| 56 | +print('side to side') |
| 57 | +for _ in range(3): |
| 58 | + for led in (Leds.red_left, Leds.red_right, Leds.green_left, Leds.green_right): |
| 59 | + led.brightness_pct = 100 |
| 60 | + time.sleep(0.5) |
| 61 | + led.brightness_pct = 0 |
| 62 | + |
| 63 | +Leds.all_off() |
| 64 | +time.sleep(0.5) |
| 65 | + |
| 66 | +# continuous mix of colors |
| 67 | +print('colors fade') |
| 68 | +for i in range(360): |
| 69 | + rd = math.radians(10 * i) |
| 70 | + Leds.red_left.brightness_pct = .5 * (1 + math.cos(rd)) |
| 71 | + Leds.green_left.brightness_pct = .5 * (1 + math.sin(rd)) |
| 72 | + Leds.red_right.brightness_pct = .5 * (1 + math.sin(rd)) |
| 73 | + Leds.green_right.brightness_pct = .5 * (1 + math.cos(rd)) |
| 74 | + time.sleep(0.05) |
| 75 | + |
| 76 | +Leds.all_off() |
| 77 | +time.sleep(0.5) |
| 78 | + |
| 79 | +print('restoring initial LEDs state') |
| 80 | +for led, level in zip(Leds.RIGHT + Leds.LEFT, saved_state) : |
| 81 | + led.brightness_pct = level |
| 82 | + |
0 commit comments