import time
import digitalio
import busio
import board
import neopixel
import adafruit_lis3dh
try:
from audiocore import WaveFile
except ImportError:
from audioio import WaveFile
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
pass
from adafruit_led_animation.helper import PixelSubset
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.group import AnimationGroup
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import (
RED,
ORANGE,
YELLOW,
)
i2c = busio.I2C(board.SCL, board.SDA)
int1 = digitalio.DigitalInOut(board.D6)
accel = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
accel.range = adafruit_lis3dh.RANGE_4_G
accel.set_tap(1, 100)
THRESHOLD = 200
NUM_PIXELS = 75
NEOPIXEL_PIN = board.D5
POWER_PIN = board.D10
ONSWITCH_PIN = board.A1
strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=False)
strip.fill(0)
strip.show()
wave_file = open("sounds/" "sound.wav", 'rb')
wave = WaveFile(wave_file)
audio = AudioOut(board.A0)
enable = digitalio.DigitalInOut(POWER_PIN)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True
group1 = PixelSubset(strip, 0, 39)
group2 = PixelSubset(strip, 39, 74)
chase = Chase(group1, speed=0.1, color=(255, 0, 0), size=10, spacing=5)
sparkle = Sparkle(group2, speed=0.05, color=(0, 255, 0), num_sparkles=10)
solidOrange = Solid(group1, color=ORANGE)
solidRed = Solid(strip, color=RED)
solidClear = Solid(strip, color=(0, 0, 0))
comet_Yellow = Comet(group2, speed=0.05, color=(255, 255, 0), tail_length=5, ring=True, bounce=False,)
pulse = Pulse(group2, speed=0.1, color=YELLOW, period=3)
animations = AnimationSequence(
AnimationGroup(
solidOrange,
pulse,
sync=True,
),
AnimationGroup(
solidOrange,
pulse,
sync=True,
),
)
while True:
animations.animate()
x, y, z = accel.acceleration
accel_total = x * x + y * y # x=tilt, y=rotate
if accel_total > THRESHOLD:
audio.play(wave)
solidRed.animate()
time.sleep(1.5)
solidClear.animate()
animations.next()