# word-learning book companion -- baby lizard book

import board, time, neopixel, adafruit_mpr121, audiobusio
from audiopwmio import PWMAudioOut as AudioOut
from audiomp3 import MP3Decoder
from adafruit_debouncer import Button # debounced buttons

# set up light strip
strip = neopixel.NeoPixel(board.GP16, 30, brightness=0.5)

# set up 12-pad capacitive touch sensor
i2c = board.STEMMA_I2C()
touch_pad = adafruit_mpr121.MPR121(board.STEMMA_I2C())

# wait for the touch sensor to calibrate
print("calibrating...don't touch yet!")
time.sleep(2)

# to make the touchpads less sensitive to reduce false activations, adjust thresholds
# higher touch threshold = less sensitive = fewer false triggers
for i in range(7):
    touch_pad[i].threshold = 25  # default is 12, try 20-30
    touch_pad[i].release_threshold = 12  # should be ~half of touch threshold

# set up colors; this code only uses PINK and BLUE, but other colors are included below
RED = (255, 0, 0)
TEAL = (0, 255, 120)
YELLOW =  (255, 150, 0)
AQUA = (50, 255, 255)
BLUE =  (0, 0, 255)
CYAN = (0, 255, 255)
GREEN =  (0, 255, 0)
JADE =  (0, 255, 40)
MAGENTA =  (255, 0, 20)
ORANGE =  (255, 40, 0)
PINK = (242, 90, 255)
PURPLE = (180, 0, 255)
BLACK = (0, 0, 0)

# set up list of colors to be used
colors = [BLUE, PINK, BLUE, PINK, BLUE, PINK, BLUE]

# create a pads list and fill it with Buttons using the
# touch_pads as input for creating Button objects.
# Each Button is debounced.

# creating debounced buttons and pads list
pads = [] # list that will hold debounced pads
for t_pad in touch_pad:
    pads.append(Button(t_pad, value_when_pressed=True))

# create an I2S audio output w/our pins
audio = audiobusio.I2SOut(
   bit_clock=board.GP10,
   word_select=board.GP9,
   data=board.GP11
)

# set up the speaker
audio = AudioOut(board.GP14) # if tip of speaker plug is plugged into GP14

# set path to the folder where the sound files can be found on the device, example below is for a folder named animals
path = "animal_names/"
animals = ["lizard.mp3", "lion.mp3", "zebra.mp3", "cat.mp3", "snake.mp3", "alligator1.mp3", "fish.mp3"]

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

# call this function passing in a string for the full filename with the extension.
def play_mp3(filename):
    decoder.file = open(path + filename, "rb")
    audio.play(decoder)
    print("sound is playing")
    while audio.playing:
        pass
    audio.stop()
    time.sleep(0.01)

print("code is running")
strip.fill(BLACK)   # ensure that no lights are turned on at start of program

file_num = 0

# the sections variable won't be referenced in the code -- use this part to plan out what sections of the strip will be
# lit based on how your light stirp is positioned on the panel

sections = [
    (0, 4),    # Pad 0: pixels 0-3 (4 pixels)
    (4, 8),    # Pad 1: pixels 4-7 (4 pixels)
    (8, 11),   # Pad 2: pixels 8-10 (3 pixels)
    (11, 15),  # Pad 3: pixels 11-14 (4 pixels)
    (14, 18),  # Pad 4: pixels 14-17 (4 pixels)
    (18, 24),  # Pad 5: pixels 18-23 (6 pixels)
    (24, 30),  # Pad 6: pixels 24-29 (6 pixels)
]

while True:
    for i in range(7):
        if file_num <= 6:
            pads[i].update()  # gets current state of each debounced pad
            if pads[i].pressed:  # true first time a pad is pressed
                print(f"You touched pad # {i}!")
                if pads[0].pressed:
                    strip[0:4] = [BLUE] * 4
                    play_mp3(animals[0])    # lizard
                    time.sleep(.1)
                strip.fill(BLACK)
                if pads[1].pressed:
                    strip[4:8] = [PINK] * 4
                    play_mp3(animals[1])    # lion
                    time.sleep(.1)
                strip.fill(BLACK)
                if pads[2].pressed:
                    strip[8:11] = [BLUE] * 3
                    play_mp3(animals[2])    # zebra
                    time.sleep(.1)
                strip.fill(BLACK)
                if pads[3].pressed:
                    strip[11:15] = [PINK] * 4
                    play_mp3(animals[3])    # cat
                    time.sleep(.1)
                strip.fill(BLACK)
                if pads[4].pressed:
                    strip[14:18] = [BLUE] * 4
                    play_mp3(animals[4])    # snake
                    time.sleep(.1)
                strip.fill(BLACK)
                if pads[5].pressed:
                    strip[18:24] = [PINK] * 6
                    play_mp3(animals[5])    # alligator
                    time.sleep(.1)
                strip.fill(BLACK)
                if pads[6].pressed:
                    strip[24:30] = [PINK] * 6
                    play_mp3(animals[6])    # fish
                    time.sleep(.1)
                strip.fill(BLACK)
                file_num += 1
            else:
                file_num = 0
            if pads[i].released:
                print(f"You released pad # {i}!")
