# Color Communication Boxes
import board, time, neopixel, digitalio, busio
from adafruit_debouncer import Debouncer
from rainbowio import colorwheel
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

button_input = digitalio.DigitalInOut(board.A3)
button_input.switch_to_input(pull=digitalio.Pull.UP)
button = Debouncer(button_input)

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

wheel_running = False
wheel_value = 0
strip.fill((0, 0, 0))

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

path = "school_sounds/"

#configure the speaker
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
audio = AudioOut(board.SPEAKER)

while True:
    if wheel_running:
        strip.fill(colorwheel(wheel_value))
        time.sleep(0.05)
        wheel_value += 1
        if wheel_value > 255:
            wheel_value = 0
    button.update()
    if button.fell:
        print(wheel_value)
        if wheel_running:
            if wheel_value > 0 and wheel_value < 8:
                play_sound("Red.wav")
            if wheel_value > 8 and wheel_value < 25:
                play_sound("Orange.wav")
            if wheel_value > 25 and wheel_value < 55:
                play_sound("Yellow.wav")
            if wheel_value > 55 and wheel_value < 95:
                play_sound("Green.wav")
            if wheel_value > 95 and wheel_value < 185:
                play_sound("Blue.wav")
            if wheel_value > 185 and wheel_value < 210:
                play_sound("Purple.wav")
            if wheel_value > 210 and wheel_value < 250:
                play_sound("Pink.wav")
            if wheel_value > 250 and wheel_value < 255:
                play_sound("Red.wav")
            wheel_running = False
        else:
            wheel_running = True
    if button.rose:
        print("button released")

