code.py
1 # SPDX-FileCopyrightText: 2021 Erin St Blaine for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Crystal Chandelier with Circuit Playground BlueFruit 7 Adafruit invests time and resources providing this open source code. 8 Please support Adafruit and open source hardware by purchasing 9 products from Adafruit! 10 Written by Erin St Blaine & Limor Fried for Adafruit Industries 11 Copyright (c) 2020-2021 Adafruit Industries 12 Licensed under the MIT license. 13 All text above must be included in any redistribution. 14 """ 15 16 # pylint: disable=import-error 17 # pylint: disable=no-member 18 import board 19 import digitalio 20 import rotaryio 21 import neopixel 22 from adafruit_circuitplayground import cp 23 24 from adafruit_led_animation.group import AnimationGroup 25 from adafruit_led_animation.animation.comet import Comet 26 from adafruit_led_animation.animation.solid import Solid 27 from adafruit_led_animation.color import ( 28 BLACK, 29 PURPLE, 30 ) 31 32 NEOPIXEL_PIN = board.A1 # NeoPixels connected here (crystals + rings) 33 NUM_PIXELS = 92 # Number of pixels in the crystals and rings 34 35 MIN_BRIGHTNESS = 1 # Minimum LED brightness as a percentage (0 to 100) 36 MAX_BRIGHTNESS = 25 # Maximum LED brighrness as a percentage (0 to 100) 37 MIN_FPS = 2 # Minimum animation speed in frames per second (>0) 38 MAX_FPS = 8 # Maximum animation speed in frames per second (>0) 39 40 # Set initial brightness and speed to center values 41 BRIGHTNESS = (MIN_BRIGHTNESS + MAX_BRIGHTNESS) // 2 42 FPS = (MIN_FPS + MAX_FPS) // 2 43 SPEED = 1 / FPS # Integer frames-per-second to seconds interval 44 LEVEL = BRIGHTNESS * 0.01 # Integer brightness percentage to 0.0-1.0 coeff. 45 46 button = digitalio.DigitalInOut(board.A5) 47 button.direction = digitalio.Direction.INPUT 48 button.pull = digitalio.Pull.UP 49 BUTTON_STATE = None 50 BUTTON_VALUE = None 51 52 PIXELS = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=LEVEL, 53 auto_write=False) 54 55 # NeoPixels off ASAP on startup 56 cp.pixels.fill(0) # Onboard pixels 57 cp.pixels.show() 58 PIXELS.fill(0) # NeoPixel strip 59 PIXELS.show() 60 61 ENCODER = rotaryio.IncrementalEncoder(board.A2, board.A3) 62 63 # LED ANIMATIONS ----------------------------------------------------------- 64 65 COMET = Comet(PIXELS, speed=SPEED, tail_length=8, 66 color=PURPLE, bounce=True) 67 CP_COMET = Comet(cp.pixels, speed=SPEED, tail_length=8, 68 color=PURPLE, bounce=True) 69 DARK_RINGS = Solid(PIXELS, color=BLACK) 70 DARK_CPB = Solid(cp.pixels, color=BLACK) 71 DARK = AnimationGroup( 72 DARK_RINGS, 73 DARK_CPB, 74 ) 75 76 # Animations Playlist, reorder as desired. AnimationGroups play at same time 77 ANIMATIONS = AnimationGroup( 78 COMET, 79 CP_COMET, 80 ) 81 82 83 # MAIN LOOP ---------------------------------------------------------------- 84 85 LAST_POSITION = ENCODER.position 86 MODE = 1 87 while True: 88 89 POSITION = ENCODER.position 90 if POSITION != LAST_POSITION: 91 MOVE = POSITION - LAST_POSITION 92 if cp.switch: 93 94 FPS = max(MIN_FPS, min(FPS + MOVE, MAX_FPS)) 95 SPEED = 1.0 / FPS 96 COMET.speed = SPEED 97 print("comet speed = ", SPEED) 98 else: 99 BRIGHTNESS = max(MIN_BRIGHTNESS, min(BRIGHTNESS + MOVE, 100 MAX_BRIGHTNESS)) 101 LEVEL = BRIGHTNESS * 0.1 102 if LEVEL > 1: 103 LEVEL = 1 104 cp.pixels.brightness = LEVEL 105 PIXELS.brightness = LEVEL 106 print("brightness = ", LEVEL) 107 LAST_POSITION = POSITION 108 if not BUTTON_VALUE and BUTTON_STATE is None: 109 BUTTON_STATE = "pressed" 110 if BUTTON_VALUE and BUTTON_STATE == "pressed": 111 print("Button pressed.") 112 if MODE == 1: 113 MODE = 2 114 else: 115 MODE = 1 116 BUTTON_STATE = None 117 if MODE == 1: 118 ANIMATIONS.animate() 119 else: 120 DARK.animate()