# "Mensch ärgere Dich nicht!" Code

# all import statements
import board, time
import neopixel
import digitalio
from adafruit_apds9960.apds9960 import APDS9960
import pwmio
from adafruit_motor import servo
from adafruit_debouncer import Button
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 ((255, 0, 0), (255, 40, 0), (255, 150, 0), (0, 255, 0), (0, 0, 255), (180, 0, 255))
)


# Setting up all parts
# NEOPIXEL
strip = neopixel.NeoPixel(board.GP15, 120, brightness=1, auto_write=True) # Wired to GP15

# BUTTON
button_input= digitalio.DigitalInOut(board.GP6) # Wired to GP9
button_input.switch_to_input(pull=digitalio.Pull.UP)
button = Button(button_input)

# GESTURE SENSOR
# Wired: Blue -> GP4, Yellow -> GP5, Red -> 3.3V, Black -> Ground
i2c = board.STEMMA_I2C()
multi_sensor = APDS9960(i2c)
multi_sensor.enable_proximity = True
multi_sensor.enable_gesture = True

# CONTINUOUS ROTATION SERVO
pwm = pwmio.PWMOut(board.GP16, frequency=50) # Wired to GP16
servo = servo.ContinuousServo(pwm)

pressed = 0
pixels = [7, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    36, 37, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
    66, 67, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
    96, 97, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]


while True:
    button.update()
    strip.fill((0, 0, 0)) # LEDs are turned off at the beginning
    gesture = multi_sensor.gesture()
    if button.pressed: # Button is pressed to start the game
        pressed = 1
    if pressed == 1:
        strip.fill((127,255,0)) # LEDs turn green
        # Open the wooden drawer by pulling on the knob
        if gesture == 4: # Gesture Sensor detects left to right swipe
            servo.throttle = -0.1 # Wooden Drawer closes (feel free to adjust the speed)
            time.sleep(3.5)
            servo.throttle = 0 # Servo stops
            time.sleep(3)
            servo.throttle = 0.1 # Servo unwinds the thread
            time.sleep(3.7)
            servo.throttle = 0
            # LEDs turn yellow, red, green and blue
            for i in range(0, 7):
                strip[i] = BLUE
            for i in range(38, 44):
                strip[i] = BLUE
            for i in range(8, 14):
                strip[i] = RED
            for i in range(30, 36):
                strip[i] = RED
            for i in range(60, 66):
                strip[i] = YELLOW
            for i in range(98, 104):
                strip[i] = YELLOW
            for i in range(68, 74):
                strip[i] = GREEN
            for i in range(90, 96):
                strip[i] = GREEN
            # Rest of the pixels turn off
            for i in pixels:
                strip[i] = BLACK
            time.sleep(15) # Time to place game pieces on the game board (home base)
            # Game ends
    # Pull on the knob to open the drawer and place game pieces inside
    if gesture == 3: # Gesture sensor detects right to left swipe
        servo.throttle = -0.1 # Wooden drawer closes
        time.sleep(4)
        servo.throttle = 0
        time.sleep(3)
        servo.throttle = 0.1 # Thread unwinds 
        time.sleep(3)
        servo.throttle = 0 # Servo stops
        break
strip.fill((0, 0, 0)) # LEDs turn off 
# Game is over!


