mushroomlamp/testmotion.py

39 lines
972 B
Python

# 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)