import board, time, neopixel, digitalio
from mfrc522 import MFRC522
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

# configure button
button = digitalio.DigitalInOut(board.GP15) # Wired to pin GP15
button.switch_to_input(pull=digitalio.Pull.UP)
counter = 0 # default state of button (used later)
fruit_order = ["apple", "pear", "blueberry", "strawberry", "orange"]

# configure RFID
sck = board.GP2
mosi = board.GP3
miso = board.GP4
cs = board.GP5
rst = board.GP6
rfid = MFRC522(sck, mosi, miso, cs, rst)
APPLE_UID = "510E0F0252" # change this based on your UID
PEAR_UID = "4176D649A8" # change this based on your UID
BLUEBERRY_UID = "51538A028A" # change this based on your UID
STRAWBERRY_UID = "5156800285" # change this based on your UID
ORANGE_UID = "31519A02F8" # change this based on your UID

# configure LED strand and colors, keeping in mind that my neopixel is GRB and not RGB
pixels = neopixel.NeoPixel(board.GP0, 15, brightness=0.5, auto_write=True)
APPLE_LED = (0,255,0)
PEAR_LED = (255,0,0)
BLUEBERRY_LED = (0,0,255)
STRAWBERRY_LED = (40,255,15)
ORANGE_LED = (40,255,0)
BLACK = (0,0,0)
pixels.fill(BLACK) # pixels all start off
delay = 1 # duration lights stay on (seconds)

# configure audio
audio = AudioOut(board.GP16)
path = "Fruit/"
def play_sound(filename): # play_sound function for each fruit
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            pass

# configure lights for each fruit scanned
def apple():
    pixels[0] = APPLE_LED
    play_sound("Apple.wav")
    time.sleep(delay)
    pixels.fill(BLACK)
def pear():
    pixels[1:3] = PEAR_LED * 2
    play_sound("Pears.wav")
    time.sleep(delay)
    pixels.fill(BLACK)
def blueberry():
    pixels[3:6] = BLUEBERRY_LED * 3
    play_sound("Blueberries.wav")
    time.sleep(delay)
    pixels.fill(BLACK)
def strawberry():
    pixels[6:10] = STRAWBERRY_LED * 4
    play_sound("Strawberries.wav")
    time.sleep(delay)
    pixels.fill(BLACK)
def orange():
    pixels[10:] = ORANGE_LED * 5
    play_sound("Oranges.wav")
    time.sleep(delay)
    pixels.fill(BLACK)

def light_up(fruit): # function that lights up the neopixels based on button push
    if fruit == "apple":
        apple()
    if fruit == "pear":
        pear()
    if fruit == "blueberry":
        blueberry()
    if fruit == "strawberry":
        strawberry()
    if fruit == "orange":
        orange()

print("Ready to scan!")

while True:
    (status, tag_type) = rfid.request(rfid.REQALL)
    if status == rfid.OK:
        (status, raw_uid) = rfid.anticoll()
        if status == rfid.OK:
            rfid_data = "".join("{:02X}".format(x) for x in raw_uid)
            if rfid_data == APPLE_UID:
                apple()
            elif rfid_data == PEAR_UID:
                pear()
            elif rfid_data == BLUEBERRY_UID:
                blueberry()
            elif rfid_data == STRAWBERRY_UID:
                strawberry()
            elif rfid_data == ORANGE_UID:
                orange()
    if button.value == False:
        print("button pressed!")
        counter += 1
        if counter > len(fruit_order):
            pixels.fill(BLACK)
            counter = 0
        elif counter <= len(fruit_order):
            fruit = fruit_order[counter - 1]
            light_up(fruit)