#!/usr/bin/python
import RPi.GPIO as GPIO
import os
from omxplayer.player import OMXPlayer
from time import sleep

GPIO.setmode(GPIO.BCM)
PIR_PIN = 18            # Set PIR Pin
PI_RX = 20              # Set Pi Link Pin
PI_TX = 21              # Set Pi Link Pin

GPIO.setup(PIR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)      # Set PIR Pin as Input with pull-up resistor
GPIO.setup(PI_RX, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)      # Set Pi RX Link Pin as Input with pull-down resistor
GPIO.setup(PI_TX, GPIO.OUT)                                 # Set Pi TX Link Pin as Output
GPIO.output(PI_TX, 0)

Intro1 = '/home/pi/Videos/WitchWickedBrew_Intro.mp4'    # Define Video Files and Paths
Intro2 = '/home/pi/Videos/WitchBrew_Intro.mp4'
Intro3 = '/home/pi/Videos/WitchHour_Intro.mp4'
Intro4 = '/home/pi/Videos/WitchHour_Intro.mp4'
Intro5 = '/home/pi/Videos/WitchSeance_Intro.mp4'
Witch1 = '/home/pi/Videos/WitchWickedBrew_Spell.mp4'    # Runtime = 138
Witch2 = '/home/pi/Videos/WitchBrew_Song.mp4'           # Runtime = 114
Witch3 = '/home/pi/Videos/WitchHour_Song.mp4'           # Runtime = 133
Witch4 = '/home/pi/Videos/WitchSpellbound_Spell.mp4'    # Runtime = 123
Witch5 = '/home/pi/Videos/WitchSeance_Spell.mp4'        # Runtime = 153

# Light FX Using (Software) PWM
# Door Bottom
GPIO.setup(23,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
bottom_red = GPIO.PWM(23,100)
bottom_green = GPIO.PWM(22,100)
bottom_blue = GPIO.PWM(27,100)

# Door Around
GPIO.setup(17,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)
GPIO.setup(6,GPIO.OUT)
around_red = GPIO.PWM(17,100)
around_green = GPIO.PWM(5,100)
around_blue = GPIO.PWM(6,100)

# Chimney
GPIO.setup(13,GPIO.OUT)
GPIO.setup(19,GPIO.OUT)
GPIO.setup(26,GPIO.OUT)
chimney_red = GPIO.PWM(13,100)
chimney_green = GPIO.PWM(19,100)
chimney_blue = GPIO.PWM(26,100)

# Lightning
GPIO.setup(12,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
lightning_red = GPIO.PWM(12,100)
lightning_green = GPIO.PWM(25,100)
lightning_blue = GPIO.PWM(24,100)

os.system('clear')                                      # Clear the console
print("Halloween Media Player v1.0 by Third Earth Design(c)")
sleep(2)
print("Initialising...")
sleep(5)
print("Master Module Started...")
sleep(5)                                                # Delay Allows External Components to Initialize
while GPIO.input(PI_RX) == 0:                           # Waiting for Slave to Indicate Ready
    sleep(1)
    print("Waiting for Slave")
    sleep(1)
    print("Waiting for Slave...")
print("Slave Detected!")
sleep(1)
GPIO.output(PI_TX, 1)
sleep(6)
GPIO.output(PI_TX, 0)
sleep(1)
player = OMXPlayer(Intro1, args=['--no-osd', '--loop', '--blank'])  # Define Main OMXPlayer-Wrapper
player.pause()
sleep(1)

try:
    while True:                                             # Main Program - Loop Infinitely
        #Play the FIRST sequence of Witch videos
        print("Playing Witch Wicked Brew (Intro)")
        sleep(1)
        player.load(Intro1)
        sleep(5)
        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:                     # Read PIR Input Pin and loop until grounded
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        GPIO.output(PI_TX, 1)
        sleep(5)
        GPIO.output(PI_TX, 0)
        player.stop()
        sleep(2)
        print("Playing Witch Wicked Brew (Spell)")
        player.load(Witch1)
        bottom_green.start(5)
        sleep(5)
        around_green.start(5)
        sleep(132)      # Runtime of video -1sec to allow stop command to execute before video ends
        bottom_green.stop()
        around_green.stop()
        player.stop()
        print("Playback ending...")
        sleep(5)

        #Play the FIRST sequence of Pupmkin videos
        print("Playing Witch Brew (Intro)")
        player.load(Intro2)
        sleep(10)
        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        GPIO.output(PI_TX, 1)
        sleep(5)
        GPIO.output(PI_TX, 0)

        while GPIO.input(PI_RX) == 0:
            sleep(1)
            print("Waiting for Slave Playback to Finish")
            sleep(1)
            print("Waiting for Slave Playback to Finish...")

        print('Slave Playback Finished')
        sleep(10)
        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        player.stop()
        sleep(2)
        print("Playing Witch Brew (Song)")
        player.load(Witch2)
        sleep(3)
        bottom_green.start(5)
        sleep(2)
        around_green.start(5)
        sleep(97)
        chimney_green.start(100)
        bottom_green.ChangeDutyCycle(100)
        around_green.ChangeDutyCycle(100)
        sleep(0.5)
        chimney_green.stop()
        bottom_green.ChangeDutyCycle(5)
        around_green.ChangeDutyCycle(5)
        sleep(10.5)
        bottom_green.stop()
        around_green.stop()
        player.stop()
        print("Playback ending...")
        sleep(5)

        #Play the THIRD sequence of Witch videos
        print("Playing Witch Hour (Intro)")
        player.load(Intro3)
        sleep(5)

        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        GPIO.output(PI_TX, 1)
        sleep(5)
        GPIO.output(PI_TX, 0)
        player.stop()
        sleep(2)
        print("Playing Witch Hour (Song)")
        player.load(Witch3)
        sleep(4)
        bottom_red.start(3.3)
        bottom_green.start(8.9)
        bottom_blue.start(12.8)
        sleep(5)
        around_red.start(3.3)
        around_green.start(8.9)
        around_blue.start(12.8)
        sleep(2)
        bottom_red.ChangeDutyCycle(13.3)
        bottom_green.ChangeDutyCycle(35.5)
        bottom_blue.ChangeDutyCycle(51.2)
        around_red.ChangeDutyCycle(13.3)
        around_green.ChangeDutyCycle(35.5)
        around_blue.ChangeDutyCycle(51.2)
        sleep(0.5)
        bottom_red.ChangeDutyCycle(3.3)
        bottom_green.ChangeDutyCycle(8.9)
        bottom_blue.ChangeDutyCycle(12.8)
        around_red.ChangeDutyCycle(3.3)
        around_green.ChangeDutyCycle(8.9)
        around_blue.ChangeDutyCycle(12.8)
        sleep(114)
        bottom_red.ChangeDutyCycle(13.3)
        bottom_green.ChangeDutyCycle(35.5)
        bottom_blue.ChangeDutyCycle(51.2)
        around_red.ChangeDutyCycle(13.3)
        around_green.ChangeDutyCycle(35.5)
        around_blue.ChangeDutyCycle(51.2)
        chimney_red.start(13.3)
        chimney_green.start(35.5)
        chimney_blue.start(51.2)
        sleep(0.5)
        bottom_red.ChangeDutyCycle(3.3)
        bottom_green.ChangeDutyCycle(8.9)
        bottom_blue.ChangeDutyCycle(12.8)
        around_red.ChangeDutyCycle(3.3)
        around_green.ChangeDutyCycle(8.9)
        around_blue.ChangeDutyCycle(12.8)
        chimney_red.stop()
        chimney_green.stop()
        chimney_blue.stop()
        sleep(4)
        bottom_red.stop()
        bottom_green.stop()
        bottom_blue.stop()
        around_red.stop()
        around_green.stop()
        around_blue.stop()
        sleep(1)
        player.stop()
        print("Playback ending...")
        sleep(5)

        #Play the FOURTH sequence of Witch videos
        print("Playing Witch Spellbound (Intro)")
        player.load(Intro4)
        sleep(5)
        bottom_red.start(1.7)
        bottom_green.start(4.5)
        bottom_blue.start(6.4)
        sleep(10)

        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        sleep(10)
        player.stop()
        sleep(2)
        print("Playing Witch Spellbound (Spell)")
        player.load(Witch4)
        sleep(5)
        bottom_red.ChangeDutyCycle(3.3)
        bottom_green.ChangeDutyCycle(8.9)
        bottom_blue.ChangeDutyCycle(12.8)
        around_red.start(3.3)
        around_green.start(8.9)
        around_blue.start(12.8)
        sleep(35)
        chimney_red.start(5)
        chimney_green.start(5)
        chimney_blue.start(5)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(10)
        chimney_green.ChangeDutyCycle(10)
        chimney_blue.ChangeDutyCycle(10)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(15)
        chimney_green.ChangeDutyCycle(15)
        chimney_blue.ChangeDutyCycle(15)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(20)
        chimney_green.ChangeDutyCycle(20)
        chimney_blue.ChangeDutyCycle(20)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(25)
        chimney_green.ChangeDutyCycle(25)
        chimney_blue.ChangeDutyCycle(25)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(30)
        chimney_green.ChangeDutyCycle(30)
        chimney_blue.ChangeDutyCycle(30)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(35)
        chimney_green.ChangeDutyCycle(35)
        chimney_blue.ChangeDutyCycle(35)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(40)
        chimney_green.ChangeDutyCycle(40)
        chimney_blue.ChangeDutyCycle(40)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(45)
        chimney_green.ChangeDutyCycle(45)
        chimney_blue.ChangeDutyCycle(45)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(50)
        chimney_green.ChangeDutyCycle(50)
        chimney_blue.ChangeDutyCycle(50)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(55)
        chimney_green.ChangeDutyCycle(55)
        chimney_blue.ChangeDutyCycle(55)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(60)
        chimney_green.ChangeDutyCycle(60)
        chimney_blue.ChangeDutyCycle(60)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(65)
        chimney_green.ChangeDutyCycle(65)
        chimney_blue.ChangeDutyCycle(65)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(70)
        chimney_green.ChangeDutyCycle(70)
        chimney_blue.ChangeDutyCycle(70)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(75)
        chimney_green.ChangeDutyCycle(75)
        chimney_blue.ChangeDutyCycle(75)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(80)
        chimney_green.ChangeDutyCycle(80)
        chimney_blue.ChangeDutyCycle(80)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(85)
        chimney_green.ChangeDutyCycle(85)
        chimney_blue.ChangeDutyCycle(85)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(90)
        chimney_green.ChangeDutyCycle(90)
        chimney_blue.ChangeDutyCycle(90)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(95)
        chimney_green.ChangeDutyCycle(95)
        chimney_blue.ChangeDutyCycle(95)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(100)
        chimney_green.ChangeDutyCycle(100)
        chimney_blue.ChangeDutyCycle(100)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(95)
        chimney_green.ChangeDutyCycle(95)
        chimney_blue.ChangeDutyCycle(95)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(90)
        chimney_green.ChangeDutyCycle(90)
        chimney_blue.ChangeDutyCycle(90)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(85)
        chimney_green.ChangeDutyCycle(85)
        chimney_blue.ChangeDutyCycle(85)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(80)
        chimney_green.ChangeDutyCycle(80)
        chimney_blue.ChangeDutyCycle(80)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(75)
        chimney_green.ChangeDutyCycle(75)
        chimney_blue.ChangeDutyCycle(75)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(70)
        chimney_green.ChangeDutyCycle(70)
        chimney_blue.ChangeDutyCycle(70)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(65)
        chimney_green.ChangeDutyCycle(65)
        chimney_blue.ChangeDutyCycle(65)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(60)
        chimney_green.ChangeDutyCycle(60)
        chimney_blue.ChangeDutyCycle(60)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(55)
        chimney_green.ChangeDutyCycle(55)
        chimney_blue.ChangeDutyCycle(55)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(50)
        chimney_green.ChangeDutyCycle(50)
        chimney_blue.ChangeDutyCycle(50)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(45)
        chimney_green.ChangeDutyCycle(45)
        chimney_blue.ChangeDutyCycle(45)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(40)
        chimney_green.ChangeDutyCycle(40)
        chimney_blue.ChangeDutyCycle(40)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(35)
        chimney_green.ChangeDutyCycle(35)
        chimney_blue.ChangeDutyCycle(35)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(30)
        chimney_green.ChangeDutyCycle(30)
        chimney_blue.ChangeDutyCycle(30)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(25)
        chimney_green.ChangeDutyCycle(25)
        chimney_blue.ChangeDutyCycle(25)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(20)
        chimney_green.ChangeDutyCycle(20)
        chimney_blue.ChangeDutyCycle(20)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(15)
        chimney_green.ChangeDutyCycle(15)
        chimney_blue.ChangeDutyCycle(15)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(10)
        chimney_green.ChangeDutyCycle(10)
        chimney_blue.ChangeDutyCycle(10)
        sleep(0.25)
        chimney_red.ChangeDutyCycle(5)
        chimney_green.ChangeDutyCycle(5)
        chimney_blue.ChangeDutyCycle(5)
        sleep(0.25)
        chimney_red.stop()
        chimney_green.stop()
        chimney_blue.stop()
        sleep(0.25)
        sleep(24)
        around_red.ChangeDutyCycle(100)
        around_green.ChangeDutyCycle(100)
        around_blue.ChangeDutyCycle(100)
        sleep(0.5)
        around_red.ChangeDutyCycle(3.3)
        around_green.ChangeDutyCycle(8.9)
        around_blue.ChangeDutyCycle(12.8)
        sleep(19)
        around_red.ChangeDutyCycle(100)
        around_green.ChangeDutyCycle(100)
        around_blue.ChangeDutyCycle(100)
        sleep(0.5)
        around_red.ChangeDutyCycle(3.3)
        around_green.ChangeDutyCycle(8.9)
        around_blue.ChangeDutyCycle(12.8)
        sleep(20)
        around_red.ChangeDutyCycle(6.7)
        around_green.ChangeDutyCycle(17.8)
        around_blue.ChangeDutyCycle(25.6)
        bottom_red.ChangeDutyCycle(6.7)
        bottom_green.ChangeDutyCycle(17.8)
        bottom_blue.ChangeDutyCycle(25.6)
        chimney_red.start(6.7)
        chimney_green.start(17.8)
        chimney_blue.start(25.6)
        sleep(1)
        around_red.ChangeDutyCycle(3.3)
        around_green.ChangeDutyCycle(8.9)
        around_blue.ChangeDutyCycle(12.8)
        bottom_red.ChangeDutyCycle(3.3)
        bottom_green.ChangeDutyCycle(8.9)
        bottom_blue.ChangeDutyCycle(12.8)
        chimney_red.stop()
        chimney_green.stop()
        chimney_blue.stop()
        sleep(7)
        around_red.stop()
        around_green.stop()
        around_blue.stop()
        bottom_red.stop()
        bottom_green.stop()
        bottom_blue.stop()
        player.stop()
        print("Playback ending...")
        sleep(5)

        #Play the FIFTH sequence of Witch videos
        print("Playing Witch Seance (Intro)")
        player.load(Intro5)
        sleep(5)

        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        GPIO.output(PI_TX, 1)
        sleep(5)
        GPIO.output(PI_TX, 0)

        while GPIO.input(PI_RX) == 0:
            sleep(1)
            print("Waiting for Slave Playback to Finish")
            sleep(1)
            print("Waiting for Slave Playback to Finish...")

        print("Slave Playback Finished")
        sleep(5)

        print("Waiting for PIR Detection...")
        while GPIO.input(PIR_PIN) == 1:
            sleep(1)
            print("Waiting for PIR Detection")
            sleep(1)
            print("Waiting for PIR Detection...")

        print("Motion Detected")
        player.stop()
        sleep(2)
        print("Playing Witch Seance (Spell)")
        player.load(Witch5)
        sleep(32)
        lightning_red.start(100)
        lightning_green.start(100)
        lightning_blue.start(100)
        sleep(0.5)
        lightning_red.stop()
        lightning_green.stop()
        lightning_blue.stop()
        sleep(12.5)
        around_red.start(5.3)
        around_green.start(9.9)
        around_blue.start(9.9)
        bottom_red.start(5.3)
        bottom_green.start(9.9)
        bottom_blue.start(9.9)
        sleep(1.5)
        around_red.ChangeDutyCycle(10.7)
        around_green.ChangeDutyCycle(19.8)
        around_blue.ChangeDutyCycle(19.6)
        sleep(1.5)
        around_red.ChangeDutyCycle(21.3)
        around_green.ChangeDutyCycle(39.5)
        around_blue.ChangeDutyCycle(39.2)
        sleep(19)
        around_red.ChangeDutyCycle(10.7)
        around_green.ChangeDutyCycle(19.8)
        around_blue.ChangeDutyCycle(19.6)
        sleep(1.5)
        around_red.ChangeDutyCycle(5.3)
        around_green.ChangeDutyCycle(9.9)
        around_blue.ChangeDutyCycle(9.9)
        sleep(1.5)
        around_red.stop()
        around_green.stop()
        around_blue.stop()
        bottom_red.stop()
        bottom_green.stop()
        bottom_blue.stop()
        sleep(60.5)
        bottom_blue.start(100)
        around_red.start(100)
        chimney_green.start(100)
        sleep(0.25)
        bottom_blue.stop()
        around_red.stop()
        chimney_green.stop()
        sleep(0.75)
        bottom_green.start(100)
        around_blue.start(100)
        chimney_red.start(100)
        sleep(0.25)
        bottom_green.stop()
        around_blue.stop()
        chimney_red.stop()
        sleep(0.75)
        sleep(17)
        around_red.start(50)
        around_green.start(50)
        around_blue.start(50)
        bottom_red.start(50)
        bottom_green.start(50)
        bottom_blue.start(50)
        sleep(0.5)
        around_red.stop()
        around_green.stop()
        around_blue.stop()
        bottom_red.stop()
        bottom_green.stop()
        bottom_blue.stop()
        sleep(2)
        player.stop()
        print("Playback ending...")
        sleep(2)
        print("Restarting Video Sequence...")
        sleep(5)

except KeyboardInterrupt:
    print('Halloween Media Player Interrupted, QUIT')
    sleep(0.2)
    player.quit()   # Kill the `omxplayer` process gracefully.
    sleep(0.2)
    GPIO.cleanup()  # Exit GPIO cleanly