import board, neopixel, time, math, digitalio, touchio
from adafruit_led_animation.sequence import AnimationSequence


from adafruit_led_animation.animation.SparklePulse import SparklePulse
from adafruit_led_animation.animation.rainbowchase import RainbowChase


from adafruit_led_animation.color import RED, YELLOW, ORANGE, GREEN, TEAL, \
    CYAN, BLUE, PURPLE, MAGENTA, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE, WHITE, BLACK

# only JADE is used from this list; feel free to test out other colors listed!

colors = [RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, PURPLE,
          MAGENTA, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE, WHITE, BLACK]

# to use
from audiopwmio import PWMAudioOut as AudioOut
from audiomp3 import MP3Decoder


# set up the 2 16x16 LED matrices
TOTAL_WIDTH = 32
TOTAL_HEIGHT = 16
NUM_PIXELS = TOTAL_WIDTH * TOTAL_HEIGHT

pixels = neopixel.NeoPixel(board.A1, NUM_PIXELS, auto_write=False, brightness=0.2)


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

# set path to the folder where the sound files can be found on the device, example below is for a folder named alarm
path = "bubbles/"

# set up the mp3 decoder
filename = "bubbles.mp3" # change to valid file in your path
mp3_file = open(path + filename, "rb")
decoder = MP3Decoder(mp3_file)

sparkle_pulse = SparklePulse(pixels, speed=0.05, period=3, color=JADE)
rainbow_chase = RainbowChase(pixels, speed=0.01, size=2, spacing=0, step=8)


# call this function passing in a string for the full filename with extension.
def play_mp3(filename):
    decoder.file = open(path + filename, "rb")
    audio.play(decoder)
    while audio.playing:
        rainbow_chase.animate()
        sparkle_pulse.animate()

touchpad_A2 = touchio.TouchIn(board.A2)
touchpad_A3 = touchio.TouchIn(board.A3)

pixels.fill(BLACK)
pixels.show()

while True:
    if touchpad_A2.value:
        print('you touched a2')
        while touchpad_A2.value == True:
            play_mp3(filename)
            time.sleep(10)
            pixels.fill(BLACK)
            pixels.show()
    if touchpad_A3.value:
        print('you touched a3')
        while touchpad_A3.value == True:
            sparkle_pulse.animate()








