#import modules
import RPi.GPIO as GPIO
from time import sleep
import os
import picamera
import pytumblr
from fractions import Fraction

#create variables to hold commands 
makeVid = "convert -delay 50 image*.jpg animation.gif"
upload1 = "mpg321 sounds/upload.mp3"
press1 = "mpg321 sounds/press.mp3"
start1 = "mpg321 sounds/start.mp3"

#create variables to hold pin numbers
greenLed = 26
blueLed = 17
redLed = 27
whiteLed=14
button = 18


# AuthenticateS via OAuth, copy from https://api.tumblr.com/console/calls/user/info
#client = pytumblr.TumblrRestClient(
 
#)



#set up pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(greenLed, GPIO.OUT)
GPIO.setup(blueLed, GPIO.OUT)
GPIO.setup(redLed, GPIO.OUT)
GPIO.setup(whiteLed, GPIO.OUT)
GPIO.setwarnings(False)

camera = picamera.PiCamera() #initiate picamera module and class
camera.resolution = (800, 480) #set resolution of picture here
camera.brightness = 60 #set brightness settings to help with dark photos
camera.annotate_foreground = picamera.Color(y=2, u=0, v=0) #set color of annotation 

try:
    GPIO.output(whiteLed, True)
    GPIO.output(redLed, False)
#read button 
    while True:
        input_state = GPIO.input(button)
        if input_state == True:
            print('Button Pressed')
            sleep(0.2)
	    os.system(start1)
	    GPIO.output(whiteLed, False)
            #if pressed blink blue LED at two speeds
            for i in range(3):
                GPIO.output(blueLed, True)
                sleep(1)
                GPIO.output(blueLed, False)
                sleep(1)
	    
            for i in range(3):
                GPIO.output(blueLed, True)
                sleep(.25)
                GPIO.output(blueLed, False)
                sleep(.25)

            #start camera preview                
            camera.start_preview()
		
            #display text over preview screen
            camera.annotate_text = 'Get Ready!'
	    sleep(2)
	    GPIO.output(greenLed, True)
            camera.annotate_text = '1'
	    os.system(press1)
	    GPIO.output(greenLed, False)
            #take 6 photos
            for i, filename in enumerate(camera.capture_continuous('image{counter:04d}.jpg')):
                sleep(1)
                if i == 1:
		    GPIO.output(greenLed, True)
                    os.system(press1)
                    camera.annotate_text = '2'
		    GPIO.output(greenLed, False)
                    
                elif i == 2:
		    GPIO.output(greenLed, True)
		    os.system(press1)
                    camera.annotate_text = '3'
		    GPIO.output(greenLed, False)
                    
                elif i == 3:
		    GPIO.output(greenLed, True)
                    os.system(press1)
                    camera.annotate_text = '4'
		    GPIO.output(greenLed, False)
                elif i == 4:
		    GPIO.output(greenLed, True)
                    os.system(press1)
                    camera.annotate_text = '5'
		    GPIO.output(greenLed, False)
		    GPIO.output(redLed, True)
                if i == 5:
                    break
            camera.stop_preview() #stop preview 
            os.system(makeVid) #send command to convert images to GIF
            print('uploading') #let us know photo is about to start uploading

            #upload photo to Tumblr
            client.create_photo(
			'snackattacker2014', 
			state="draft",
			data="animation.gif"
			)
	    print("uploaded") #let us know GIF has been uploaded
            #turn on uploaded LED and play samples
            #GPIO.output(redLed, True)
            os.system(upload1)
            GPIO.output(redLed, False)
	    GPIO.output(whiteLed, True)
 #hit Ctrl + C to stop program
except KeyboardInterrupt:
    print ('program stopped') 

finally:  
    GPIO.cleanup() #cleanup GPIO channels
    

