# Caroline Hanzlik
# Interactive Art Project: James Turrell Aten Reign Box

# For connecting Bread Board
#  - Potentiometer: A1
#  - Speaker: D3
#  - led strip 1: D2
#  - led strip 2: D4


# import libraries
import board, time, neopixel, digitalio, adafruit_vl53l1x
from rainbowio import colorwheel
from analogio import AnalogIn
from audiocore import WaveFile

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, 222, 30)
    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))
)

INDIGO = ((63, 0, 255))
VIOLET = ((127, 0, 255))

# setting up colors
colors = [RED, MAGENTA, ORANGE, AMBER, GOLD, YELLOW, GREEN, JADE, BLUE, INDIGO, VIOLET, PURPLE]

# setting up sound
try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        print("This board does not support AudioOut")

audio = AudioOut(board.D3)
path = "sounds/"

def play_sound(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            if distance_sensor.data_ready:
                proximity = distance_sensor.distance
                print(proximity)
                if proximity == None:
                    strip.brightness = 0.08
                    strip2.brightness = 0.08
                else:
                    strip.brightness = (151-round(proximity)) / 255
                    strip2.brightness = (151-round(proximity)) / 255
            color = round(potentiometer.value/(65520/255))
            strip.fill(colorwheel(color))
            strip2.fill(colorwheel(color))
            proximity = 0
            distance_sensor.clear_interrupt()
            time.sleep(0.1)

# setting up LED strip
strip = neopixel.NeoPixel(board.D2, 300, brightness = 0.5, auto_write = True)
strip2 = neopixel.NeoPixel(board.D4, 300, brightness = 0.5, auto_write = True)

# setting up tof distance sensor
i2c = board.I2C()
distance_sensor = adafruit_vl53l1x.VL53L1X(i2c)
distance_sensor.distance_mode = 1
distance_sensor.timing_budget = 100
distance_sensor.start_ranging()

# setting up potentiometer
potentiometer = AnalogIn(board.A1)

"""
Change sound!!!
"""
while True:
    play_sound("potter.wav")








