# Jack Yen
# IS Art Project

# Import Necessary Libraries (as well as others)
import board
import time
import analogio
import digitalio
import pwmio
import neopixel
from adafruit_motor import servo
from analogio import AnalogIn
import touchio
import adafruit_lis3dh
import busio
import random
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.color import (
    AMBER, #(255, 100, 0)
    AQUA, # (50, 255, 255)
    BLACK, #OFF (0, 0, 0)
    BLUE, # (0, 0, 255)
    CYAN, # (0, 255, 255)
    GOLD, # (255, 215, 0)
    GREEN, # (0, 255, 0)
    JADE, # (0, 255, 40)
    MAGENTA, #(255, 0, 20)
    OLD_LACE, # (253, 245, 230)
    ORANGE, # (255, 40, 0)
    PINK, # (242, 90, 255)
    PURPLE, # (180, 0, 255)
    RED, # (255, 0, 0)
    TEAL, # (0, 255, 120)
    WHITE, # (255, 255, 255)
    YELLOW, # (255, 150, 0)
    RAINBOW # a list of colors to cycle through
    # RAINBOW is RED, ORANGE, YELLOW, GREEN, BLUE, and PURPLE ((255, 0, 0), (255, 40, 0), (255, 150, 0), (0, 255, 0), (0, 0, 255), (180, 0, 255))
)



# Set up pixel board and light strip
pixels_pin = board.NEOPIXEL
pixels_num_of_lights = 10
pixels = neopixel.NeoPixel(pixels_pin, pixels_num_of_lights, brightness=0.5, auto_write=True)

strip_pin = board.A1
strip_num_of_lights = 30
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights, brightness=0.5, auto_write=True)

# Set up button A and B
button_A = digitalio.DigitalInOut(board.BUTTON_A)
button_A.switch_to_input(pull = digitalio.Pull.DOWN)
button_B = digitalio.DigitalInOut(board.BUTTON_B)
button_B.switch_to_input(pull = digitalio.Pull.DOWN)


# Set up potentiometer
potentiometer = AnalogIn(board.A3)


# Create function for playing voice clip
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
audio = AudioOut(board.SPEAKER)
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

while True:
    pixels.fill(AMBER)
    if button_A.value:
        initial = True
        play_sound("wand.wav")
        strip.brightness = 0
        strip.fill(AMBER)
        for i in range(100):
            strip.brightness = i/100
            time.sleep(0.01)
        strip.fill(AMBER)


    if button_B.value:
        for i in range(30):
            strip[i] = AMBER
            time.sleep(0.03)
        for i in range(29, -1, -1):
            strip[i] = BLUE
            time.sleep(0.03)
        print(potentiometer.value)
        if potentiometer.value >= 49140:
            play_sound("Brad-Pitt.wav")
        elif potentiometer.value >= 32760:
            play_sound("you-look-awesome.wav")
        elif potentiometer.value >= 16380:
            play_sound("looking-good.wav")
        elif potentiometer.value<16380:
            print("No praise needed!")
        else:
            pass
        for x in range(30):
            strip[i] = AMBER
            time.sleep(0.03)
        time.sleep(0.5)









