import time
import random
import RPi.GPIO as GPIO
from neopixel import *

# LED strip configuration:
LED_COUNT      = 12      # Number of LED pixels.
LED_PIN        = 19      # GPIO pin connected to the pixels (must supBlackout neopixels!).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 1       # 0 for pins 18 and 12, and 1 for pins 19 and 13
LED_STRIP      = ws.SK6812_STRIP_RGBW	
#LED_STRIP      = ws.SK6812W_STRIP

rain = 5
cloud = 6

GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.OUT, initial=0)
GPIO.setup(6, GPIO.OUT, initial=0)


# Define functions which animate LEDs in various ways.
def colorWipe(strip, color, wait_ms=80):
    	"""Wipe color across display a pixel at a time."""
	for i in range(strip.numPixels()):
		strip.setPixelColor(i, color)
		strip.show()
		time.sleep(wait_ms/1000.0)


def sunshine(strip, wait_ms=20, iterations=1):
	"""Draw rainbow that uniformly distributes itself across all pixels."""
	for j in range(256*iterations):
		for i in range(strip.numPixels()):
			strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
		strip.show()
		time.sleep(wait_ms/1000.0)


def lightningStrobe(strip, color):
    	"""Wipe white across display a pixel at a time very quickly."""
	for i in range(strip.numPixels()):
		strip.setPixelColor(i, color)
		strip.show()
		time.sleep(5/1000.0)
	for i in range(strip.numPixels()):
		strip.setPixelColor(i,0)
		strip.show()
		time.sleep(20/1000.0)
	for i in range(strip.numPixels()):
	        strip.setPixelColor(i, color)
		strip.show()
		time.sleep(3/1000.0)
	for i in range(strip.numPixels()):
		strip.setPixelColor(i,0)
		strip.show()
		time.sleep(8/1000.0)		
	for i in range(strip.numPixels()):
		strip.setPixelColor(i, color)
		strip.show()
		time.sleep(5/1000.0)
	for i in range(strip.numPixels()):
		strip.setPixelColor(i,0)
		strip.show()
		time.sleep(10/1000.0)

def snowSparkle(strip, color, wait_ms=100, iterations=20):
    	"""Movie theater light style chaser animation."""
	for j in range(iterations):
		for q in range(3):
			for i in range(0, strip.numPixels(), 3):
				strip.setPixelColor(i+q, color)
			strip.show()
			time.sleep(wait_ms/1000.0)
			for i in range(0, strip.numPixels(), 3):
				strip.setPixelColor(i+q, 0)

# Define functions that emulate weather conditions

def rain():
        GPIO.output(5, 1)
        colorWipe(strip, Color(94, 181, 172))  # Purpleish color wipe  
	time.sleep(5000)
        colorWipe(strip, Color(103, 101, 133))  # Purpleish color wipe 
        time.sleep(5000)
        colorWipe(strip, Color(0,0,0), 1) # Blackout neopixels
        GPIO.cleanup() # cleanup all GPIO

def cloud():
        GPIO.output(6, 1)
        colorWipe(strip, Color(255, 185, 10))  # white theater chase
        time.sleep(random.randint(1,30))
        colorWipe(strip, Color(0,0,0), 1) # Blackout neopixels
        GPIO.cleanup() # cleanup all GPIO

def sun():
        colorWipe(strip, Color(44,255,255))  # Yello color wipe
        time.sleep(random.randint(1,30))
        colorWipe(strip, Color(0,0,0), 1) # Blackout neopixels
        GPIO.cleanup() # cleanup all GPIO

def thunder():
        GPIO.output(6, 1)
        time.sleep(random.randint(1, 5))        
        lightningStrobe(strip, Color(127, 127, 127, 127))  # White lightningStrobe
        time.sleep(random.randint(1, 5))
        lightningStrobe(strip, Color(255, 255, 255, 255))  # White lightningStrobe
        time.sleep(random.randint(1, 5))
        lightningStrobe(strip, Color(127, 127, 127, 127))  # White lightningStrobe
        time.sleep(random.randint(1, 5))
        colorWipe(strip, Color(0,0,0), 1) # Blackout neopixels
        GPIO.cleanup() # cleanup all GPIO

def snow():
        GPIO.output(6, 1)
        time.sleep(random.randint(1, 5))
        snowSparkle(strip, Color(255, 255, 255))  # white theater chase
        time.sleep(random.randint(1,5))
        colorWipe(strip, Color(0,0,0), 1) # Blackout neopixels
        GPIO.cleanup() # cleanup all GPIO


# Main program logic follows:
try:
        # Create NeoPixel object with appropriate configuration.
	strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
	# Intialize the library (must be called once before other functions).
	strip.begin()


	print ('Press Ctrl-C to quit.')
        weather = input("Enter the weather. It can be rainy, cloudy, sunny, thundery, snowy: ")
        print ('Now activating', weather)
        
        if weather == "rainy":
            rain()
        elif weather == "cloudy": 
            cloud()
        elif weather == "sunny":
            sun()
        elif weather == "thundery":
            thunder()
        elif weather == "snowy":
            snow()
        else:
            print "You typed it in wrong try again."
            weather = input("Enter the weather. It can be rainy, cloudy, sunny, thundery, snowy: ")



except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
	colorWipe(strip, Color(0,0,0), 1) # Blackout neopixels
        GPIO.cleanup() # cleanup all GPIO
