mushroomlamp/testlight.py

95 lines
2.6 KiB
Python
Raw Normal View History

# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import datetime
import board
from rainbowio import colorwheel
import neopixel
import random
NUMPIXELS = 35 # Update this to match the number of LEDs.
SPEED = 0.80 # Increase to slow down the rainbow. Decrease to speed it up.
BRIGHTNESS = 0.99 # A number between 0.0 and 1.0, where 0.0 is off, and 1.0 is max.
PIN = board.D10 # This is the default pin on the 5x5 NeoPixel Grid BFF.
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
TOPLIGHTS = range(16,28)
MIDLIGHTS = range(0,16)
BOTLIGHTS = range(28,35)
ALLIGHTS = [TOPLIGHTS, MIDLIGHTS, BOTLIGHTS]
def rainbow_cycle(wait):
for color in range(255):
for pixel in range(len(pixels)): # pylint: disable=consider-using-enumerate
pixel_index = (pixel * 256 // len(pixels)) + color * 5
pixels[pixel] = colorwheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
def rainbow_cycle_set_old(pixel_set, index, color, loops):
# level = loops + (50 * index)
level = (index % loops) * color
for i in pixel_set:
pixels[i] = colorwheel(level & 255)
pixels.show()
def color_test(colors):
for index, light_set in enumerate(ALLIGHTS):
for pixel in light_set:
pixels[pixel] = colors[index]
pixels.show()
def turn_off():
print("Turning off all")
pixels.fill((0,0,0))
pixels.show()
def turn_on(event_obj):
try:
specific_time = datetime.datetime.now() + datetime.timedelta(hours=1.5)
color = random.randint(0, 255)
while True:
# rainbow_cycle(SPEED)
# for i in TOPLIGHTS:
# pixels[i] = (100,0,100)
# for i in MIDLIGHTS:
# pixels[i] = (255,0,0)
# for i in BOTLIGHTS:
# pixels[i] = (255,100,0)
# pixels.show()
for index, light_set in enumerate(ALLIGHTS):
for pixel in light_set:
pixel_index = (index * 256 // 16) + color
pixels[pixel] = colorwheel(int(pixel_index) & 255)
pixels.show()
# time.sleep(0.40)
time.sleep(0.20)
color += 1
if color > 256:
color = 1
if event_obj.is_set():
exit()
if datetime.datetime.now() > specific_time:
turn_off()
exit()
except KeyboardInterrupt:
turn_off()