Adding test files for proof of concept light changing, movement detection and threading
This commit is contained in:
parent
879a07e86e
commit
65fa8a4e04
3
.gitignore
vendored
3
.gitignore
vendored
@ -130,3 +130,6 @@ dist
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
# Python junk
|
||||
.venv
|
||||
__*
|
||||
|
94
testlight.py
Normal file
94
testlight.py
Normal file
@ -0,0 +1,94 @@
|
||||
# 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()
|
39
testmotion.py
Normal file
39
testmotion.py
Normal file
@ -0,0 +1,39 @@
|
||||
# ADXL345 Python library for Raspberry Pi
|
||||
|
||||
import time
|
||||
import adafruit_adxl34x
|
||||
import board
|
||||
import testlight
|
||||
import threading
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
i2c = board.I2C() # uses board.SCL and board.SDA
|
||||
accelerometer = adafruit_adxl34x.ADXL345(i2c)
|
||||
accelerometer.enable_tap_detection()
|
||||
|
||||
testlight.turn_off()
|
||||
powered_on = False
|
||||
|
||||
|
||||
|
||||
|
||||
while True:
|
||||
if accelerometer.events["tap"]:
|
||||
powered_on = not powered_on
|
||||
print('Turn on Light!' if powered_on else 'Power Off')
|
||||
|
||||
# Turn on light
|
||||
if powered_on:
|
||||
background_task = threading.Event()
|
||||
background_thread = threading.Thread(target=testlight.turn_on, daemon=True, args=(background_task,))
|
||||
background_thread.start()
|
||||
|
||||
# Power off light
|
||||
if not powered_on:
|
||||
background_task.set()
|
||||
testlight.turn_off()
|
||||
|
||||
|
||||
|
||||
time.sleep(0.25)
|
Loading…
Reference in New Issue
Block a user