# Make save arts

import board
import neopixel
import time
import digitalio
import touchio
import pwmio
from adafruit_motor import servo

#import colors
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
)
INDIGO=(63,0,255)
VIOLET=(127, 0, 255)
BLACK=(0,0,0)
MAROON=(128,0,0)
colors=[RED,ORANGE,VIOLET,GREEN,YELLOW,MAGENTA,MAROON,AQUA,PINK, INDIGO]

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


#setup lightstrip
strip_pin= board.A1
strip_num_of_lights=30
strip=neopixel.NeoPixel(strip_pin,strip_num_of_lights, brightness = 0.9, auto_write=True)


#setup buttons
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)


#setup speaker
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
#configure
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
audio = AudioOut(board.SPEAKER)
#path where sound file can be found
path="drumSounds/"
#list of sounds

#create a PWMOut object
pwm= pwmio.PWMOut(board.A2, frequency=50)
#create a servo object
servo_1 = servo.Servo(pwm, max_pulse=2320)
#brown wire=GND, orange =Vout, yellow= signal(A2)


#set up touchpads
pad=[board.A3, board.A4, board.A5,board.A6,board.TX]

touchpad=[]

for i in range(len(pad)):
    touchpad.append(touchio.TouchIn(pad[i]))

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

code=[2,0,0,3]
listcode=[]
SaveOn=False
trys=0
while True:
    #turn save on and off
    if button_A.value:
        SaveOn=True
    if button_B.value and button_A.value:
        SaveOn=False

    #setup save
    if SaveOn==True:
        touched=False
        for i in range(len(touchpad)):
            if touchpad[i].value:
                listcode.append(i)
                pixels.fill(WHITE)
                strip.fill(WHITE)
                playfile("beep_.wav")
                touched=True
                print(listcode)
                time.sleep(0.2)
            if button_B.value:
                if listcode==code:
                    print("open")
                    playfile("ding.wav")
                    time.sleep(0.3)
                    trys=0
                    servo_1.angle= 85
                    pixels.fill(GREEN)
                    for i in range(10):
                        playfile("countdown.wav")
                        pixels.fill(colors[i])
                        strip.fill(colors[i])
                        time.sleep(1)
                        pixels.fill(BLACK)
                        strip.fill(BLACK)
                    servo_1.angle= 0
                    time.sleep(1.0)
                    listcode.clear()
                else:
                    print("wrong code")
                    listcode.clear()
                    pixels.fill(RED)
                    strip.fill((0,255,0))
                    trys=trys+1
                    time.sleep(0.3)
                    print("trys= ",trys)
                    playfile("error.wav")
                    if trys==3:
                        print("save locked")
                        trys=0
                        while button_A.value ==False and button_B.value == False:
                            pixels.fill(RED)
                            strip.fill((0,255,0))
                            time.sleep(1)
                            pixels.fill(BLACK)
                            strip.fill(BLACK)
                            SaveOn=False
                            playfile("alarm_1.wav")
                        pixels.fill(BLUE)
                        strip.fill((255,0,0))
                        time.sleep(1)
                        pixels.fill(BLACK)
                        strip.fill(BLACK)
            if touched == False:
                pixels.fill(BLACK)
                strip.fill(BLACK)

