|
| 1 | +# SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# NeoPixel earrings example. Makes a nice blinky display with just a |
| 6 | +# few LEDs on at any time...uses MUCH less juice than rainbow display! |
| 7 | + |
| 8 | +import time |
| 9 | +from rainbowio import colorwheel |
| 10 | +import board |
| 11 | +import neopixel |
| 12 | +import adafruit_dotstar |
| 13 | + |
| 14 | +try: |
| 15 | + import urandom as random # for v1.0 API support |
| 16 | +except ImportError: |
| 17 | + import random |
| 18 | + |
| 19 | +dot = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 20 | +dot[0] = (0, 0, 0) |
| 21 | + |
| 22 | +numpix = 16 # Number of NeoPixels (e.g. 16-pixel ring) |
| 23 | +pixpin = board.D0 # Pin where NeoPixels are connected |
| 24 | +strip = neopixel.NeoPixel(pixpin, numpix, brightness=.3, auto_write=False) |
| 25 | + |
| 26 | +mode = 0 # Current animation effect |
| 27 | +offset = 0 # Position of spinner animation |
| 28 | +hue = 0 # Starting hue |
| 29 | +color = colorwheel(hue & 255) # hue -> RGB color |
| 30 | +prevtime = time.monotonic() # Time of last animation mode switch |
| 31 | + |
| 32 | +while True: # Loop forever... |
| 33 | + if mode == 0: # Random sparkles - lights just one LED at a time |
| 34 | + i = random.randint(0, numpix - 1) # Choose random pixel |
| 35 | + strip[i] = color # Set it to current color |
| 36 | + strip.show() # Refresh LED states |
| 37 | + # Set same pixel to "off" color now but DON'T refresh... |
| 38 | + # it stays on for now...bot this and the next random |
| 39 | + # pixel will be refreshed on the next pass. |
| 40 | + strip[i] = [0, 0, 0] |
| 41 | + time.sleep(0.008) # 8 millisecond delay |
| 42 | + elif mode == 1: # Spinny colorwheel (4 LEDs on at a time) |
| 43 | + for i in range(numpix): # For each LED... |
| 44 | + if ((offset + i) & 7) < 2: # 2 pixels out of 8... |
| 45 | + strip[i] = color # are set to current color |
| 46 | + else: |
| 47 | + strip[i] = [0, 0, 0] # other pixels are off |
| 48 | + strip.show() # Refresh LED states |
| 49 | + time.sleep(0.04) # 40 millisecond delay |
| 50 | + offset += 1 # Shift animation by 1 pixel on next frame |
| 51 | + if offset >= 8: |
| 52 | + offset = 0 |
| 53 | + # Additional animation modes could be added here! |
| 54 | + |
| 55 | + t = time.monotonic() # Current time in seconds |
| 56 | + if (t - prevtime) >= 8: # Every 8 seconds... |
| 57 | + mode += 1 # Advance to next mode |
| 58 | + if mode > 1: # End of modes? |
| 59 | + mode = 0 # Start over from beginning |
| 60 | + hue += 80 # And change color |
| 61 | + color = colorwheel(hue & 255) |
| 62 | + strip.fill([0, 0, 0]) # Turn off all pixels |
| 63 | + prevtime = t # Record time of last mode change |
0 commit comments