From 65fa8a4e046d610d836984de451fb1551103cd97 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 21 Dec 2024 09:40:49 -0700 Subject: [PATCH] Adding test files for proof of concept light changing, movement detection and threading --- .gitignore | 3 ++ testlight.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ testmotion.py | 39 +++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 testlight.py create mode 100644 testmotion.py diff --git a/.gitignore b/.gitignore index ceaea36..f958d50 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,6 @@ dist .yarn/install-state.gz .pnp.* +# Python junk +.venv +__* diff --git a/testlight.py b/testlight.py new file mode 100644 index 0000000..2b6a04a --- /dev/null +++ b/testlight.py @@ -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() diff --git a/testmotion.py b/testmotion.py new file mode 100644 index 0000000..b2e7269 --- /dev/null +++ b/testmotion.py @@ -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) \ No newline at end of file