# IMPORTS SETUP

# Standard imports
import board, neopixel, time, random, digitalio, adafruit_lis3dh, busio, pwmio
# For lights and colors
from rainbowio import colorwheel
# For playing sound
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
# For servos
from adafruit_motor import servo
# For buttons
from adafruit_debouncer import Debouncer

# LIGHTS SETUP

# Colors setup
from adafruit_led_animation.color import (
    AMBER,
    AQUA,
    BLACK,
    BLUE,
    CYAN,
    GOLD,
    GREEN,
    JADE,
    MAGENTA,
    OLD_LACE,
    ORANGE,
    PINK,
    PURPLE,
    RED,
    TEAL,
    WHITE,
    YELLOW,
    RAINBOW
)

# Pixels setup
pixels_pin = board.NEOPIXEL
lights_on_pixels = 10
pixels = neopixel.NeoPixel(pixels_pin, lights_on_pixels, brightness = 0.5, auto_write = True)

# Strip setup
strip_pin = board.A1
lights_on_strip = 30
strip = neopixel.NeoPixel(strip_pin, lights_on_strip, brightness=0.5, auto_write=True)

# ACCELEROMETER SETUP
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
accelerometer.range = adafruit_lis3dh.RANGE_8_G

# SERVO SETUP
pwm = pwmio.PWMOut(board.A2, frequency=50)
servo_1 = servo.Servo(pwm, max_pulse = 2500)

# BUTTON SETUP
button_speed_input = digitalio.DigitalInOut(board.A4)
button_speed_input.switch_to_input(pull = digitalio.Pull.UP)
button_speed = Debouncer(button_speed_input)

# SOUND SETUP
# Speaker setup
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
audio = AudioOut(board.SPEAKER)

# Playing sound
path = "sounds/"
def play_sound(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            pass

# Done with setup, here's the code:

pixels.fill(BLACK)
servo_1.angle = 180
servoangle = 180
servo_direction = -1
last_sound = 2

while True:
    x, y, z = accelerometer.acceleration
    # print((x, y, z))
    time.sleep(0.1)
    angle = (x + 9) * 1.67
    if angle < 0:
        angle = 0
    if angle > 30:
        angle = 30
    color = round(angle * (255/30))
    # print(color)
    strip.fill(colorwheel(color))
    pixels.fill(colorwheel(color))

    button_speed.update()
    if button_speed.fell:
        if servoangle < 0:
            servoangle = 0
        if servoangle > 180:
            servoangle = 180

        servoangle = servoangle + (servo_direction * 45)
        if servoangle == 0 or servoangle == 180:
            servo_direction = servo_direction * -1
            print("Reversing direction")
            if last_sound == 1:
                play_sound("2.wav")
                last_sound = 2
            else:
                play_sound("1.wav")
                last_sound = 1

        print("Moving from {} to {}".format(servo_1.angle, servoangle))
        servo_1.angle = servoangle