|
| 1 | +# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +"""Interactive light show using built-in LED and capacitive touch""" |
| 6 | +import time |
| 7 | +from rainbowio import colorwheel |
| 8 | +import adafruit_dotstar |
| 9 | +import board |
| 10 | +import touchio |
| 11 | + |
| 12 | +led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1) |
| 13 | +touch_A0 = touchio.TouchIn(board.A0) |
| 14 | +touch_A1 = touchio.TouchIn(board.A1) |
| 15 | +touch_A2 = touchio.TouchIn(board.A2) |
| 16 | + |
| 17 | + |
| 18 | +def cycle_sequence(seq): |
| 19 | + """Allows other generators to iterate infinitely""" |
| 20 | + while True: |
| 21 | + for elem in seq: |
| 22 | + yield elem |
| 23 | + |
| 24 | + |
| 25 | +def rainbow_cycle(seq): |
| 26 | + """Rainbow cycle generator""" |
| 27 | + rainbow_sequence = cycle_sequence(seq) |
| 28 | + while True: |
| 29 | + # pylint: disable=stop-iteration-return |
| 30 | + led[0] = (colorwheel(next(rainbow_sequence))) |
| 31 | + yield |
| 32 | + |
| 33 | + |
| 34 | +def brightness_cycle(): |
| 35 | + """Allows cycling through brightness levels""" |
| 36 | + brightness_value = cycle_sequence([1, 0.8, 0.6, 0.4, 0.2]) |
| 37 | + while True: |
| 38 | + # pylint: disable=stop-iteration-return |
| 39 | + led.brightness = next(brightness_value) |
| 40 | + yield |
| 41 | + |
| 42 | + |
| 43 | +color_sequences = cycle_sequence( |
| 44 | + [ |
| 45 | + range(256), # rainbow_cycle |
| 46 | + [50, 160], # Python colors! |
| 47 | + [0], # red |
| 48 | + [85], # green |
| 49 | + [170], # blue |
| 50 | + ] |
| 51 | +) |
| 52 | + |
| 53 | +cycle_speeds = cycle_sequence([0.1, 0.3, 0.5]) |
| 54 | + |
| 55 | +brightness = brightness_cycle() |
| 56 | + |
| 57 | +CYCLE_SPEED_INITIAL = 0.3 |
| 58 | +cycle_speed_start = time.monotonic() |
| 59 | +cycle_speed = cycle_speed_start + CYCLE_SPEED_INITIAL |
| 60 | + |
| 61 | +rainbow = None |
| 62 | +touch_A0_state = None |
| 63 | +touch_A1_state = None |
| 64 | +touch_A2_state = None |
| 65 | + |
| 66 | +while True: |
| 67 | + now = time.monotonic() |
| 68 | + |
| 69 | + if not touch_A0.value and touch_A0_state is None: |
| 70 | + touch_A0_state = "ready" |
| 71 | + if touch_A0.value and touch_A0_state == "ready" or rainbow is None: |
| 72 | + rainbow = rainbow_cycle(next(color_sequences)) |
| 73 | + touch_A0_state = None |
| 74 | + |
| 75 | + if now >= cycle_speed: |
| 76 | + next(rainbow) |
| 77 | + cycle_speed_start = now |
| 78 | + cycle_speed = cycle_speed_start + CYCLE_SPEED_INITIAL |
| 79 | + |
| 80 | + if not touch_A1.value and touch_A1_state is None: |
| 81 | + touch_A1_state = "ready" |
| 82 | + if touch_A1.value and touch_A1_state == "ready": |
| 83 | + CYCLE_SPEED_INITIAL = next(cycle_speeds) |
| 84 | + cycle_speed_start = now |
| 85 | + cycle_speed = cycle_speed_start + CYCLE_SPEED_INITIAL |
| 86 | + touch_A1_state = None |
| 87 | + |
| 88 | + if not touch_A2.value and touch_A2_state is None: |
| 89 | + touch_A2_state = "ready" |
| 90 | + if touch_A2.value and touch_A2_state == "ready": |
| 91 | + next(brightness) |
| 92 | + touch_A2_state = None |
0 commit comments