
import datetime
import time,datetime
import numpy as np
import random;

from neopixel import *


# LED strip configuration:
LED_COUNT      = 500      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
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 = 64   # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)


# Define functions which animate LEDs in various ways.

def wheel(pos):
	"""Generate rainbow colors across 0-255 positions."""
	if pos < 85:
		return Color(pos * 3, 255 - pos * 3, 0)
	elif pos < 170:
		pos -= 85
		return Color(255 - pos * 3, 0, pos * 3)
	else:
		pos -= 170
		return Color(0, pos * 3, 255 - pos * 3)

def rainbow(strip, wait_ms=3, iterations=1):
	"""Draw rainbow that fades across all pixels at once."""
	for j in range(256*iterations):
		for i in range(strip.numPixels()):
			strip.setPixelColor(i, wheel((i+j) & 255))
		strip.show()
		time.sleep(wait_ms/1000.0)

def rainbowCycle(strip, wait_ms=3, iterations=5):
	"""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(((i * 256 / strip.numPixels()) + j) & 255))
		strip.show()
		time.sleep(wait_ms/1000.0)

def cyclecolumn (strip,colour):
	"""light each column to bottom"""
	for c in range(0,9):
		for r in range (49,0,-1):
			strip.setPixelColor(mymatrix[r,c],colour )
			strip.show()
			time.sleep(0.01)
	for c in range(0,9):
		for r in range (0,49):
			strip.setPixelColor(mymatrix[r,c],Color(0,0,0))
			strip.show()
			time.sleep(0.01)
	#all off now

def sparkly (strip,colour,cycles,dly):
	#flicker lights on a green tree
	def showframe(bmatrix):
		for ctree in range (0,LED_COUNT-1):
		   strip.setPixelColor(ctree,bmatrix[ctree])
		strip.show()
		
	def fillframe (bmatrix):
		for ctree in range (0,LED_COUNT-1):
			bmatrix[ctree]=colour
			
	#frame of colour+random stars
	#create blank frames of all colour
	fillframe(blankframe)
	fillframe(bm1) #clear old colour and replace with new
	fillframe(bm2)
	#make 50 random bright and less bright stars, show them with the blankframe
	for ctr in range (0,50):
		ranplace=random.randrange(0,LED_COUNT-1)
		bm1[ranplace]=Color(127,127,127)
		bm2[ranplace]=Color(255,255,255)
	#bm1 and 2 have random stars, bm2 is brightest
	for lps in range (cycles):
		showframe(blankframe)
		time.sleep(0.02) 
		showframe(bm1) 
		time.sleep(0.02)
		showframe(bm2)
		time.sleep(0.02)
		

 
# Main program logic follows:
if __name__ == '__main__':
	# Create NeoPixel object with appropriate configuration.
	strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
	# Intialize the library (must be called once before other functions).
	strip.begin()
	mymatrix = np.arange(500).reshape(50,10)
	bm1=np.arange(500)
	bm2=np.arange(500)
	blankframe=np.arange(500)
	mymatrix=np.array([       
			[49,50,149,150,249,250,349,350,449,450],
			[48,51,148,151,248,251,348,351,448,451],
			[47,52,147,152,247,252,347,352,447,452],
			[46,53,146,153,246,253,346,353,446,453],
			[45,54,145,154,245,254,345,354,445,454],
			[44,55,144,155,244,255,344,355,444,455],
			[43,56,143,156,243,256,343,356,443,456],
			[42,57,142,157,242,257,342,357,442,457],
			[41,58,141,158,241,258,341,358,441,458],
			[40,59,140,159,240,259,340,359,440,459],
			[39,60,139,160,239,260,339,360,439,460],
			[38,61,138,161,238,261,338,361,438,461],
			[37,62,137,162,237,262,337,362,437,462],
			[36,63,136,163,236,263,336,363,436,463],
			[35,64,135,164,235,264,335,364,435,464],
			[34,65,134,165,234,265,334,365,434,465],
			[33,66,133,166,233,266,333,366,433,466],
			[32,67,132,167,232,267,332,367,432,467],
			[31,68,131,168,231,268,331,368,431,468],
			[30,69,130,169,230,269,330,369,430,469],
			[29,70,129,170,229,270,329,370,429,470],
			[28,71,128,171,228,271,328,371,428,471],
			[27,72,127,172,227,272,327,372,427,472],
			[26,73,126,173,226,273,326,373,426,473],
			[25,74,125,174,225,274,325,374,425,474],
			[24,75,124,175,224,275,324,375,424,475],
			[23,76,123,176,223,276,323,376,423,476],
			[22,77,122,177,222,277,322,377,422,477],
			[21,78,121,176,221,278,321,378,421,478],
			[20,79,120,179,220,279,320,379,420,479],
			[19,80,119,180,219,280,319,380,419,480],
			[18,81,118,181,218,281,318,381,418,481],
			[17,82,117,182,217,282,317,382,417,482],
			[16,83,116,183,216,283,316,383,416,483],
			[15,84,115,184,215,284,315,384,415,484],
			[14,85,114,185,214,285,314,385,414,485],
			[13,86,113,186,213,286,313,386,413,486],
			[12,87,112,187,212,287,312,387,412,487],
			[11,88,111,188,211,288,311,388,411,488],
			[10,89,110,189,210,289,310,389,410,489],
			[ 9,90,109,190,209,290,309,390,409,490],
			[ 8,91,108,191,208,291,308,391,408,491],
			[ 7,92,107,192,207,292,307,392,407,492],
			[ 6,93,106,193,206,293,306,393,406,493],
			[ 5,94,105,194,205,294,305,394,405,494],
			[ 4,95,104,195,204,295,304,395,404,495],
			[ 3,96,103,196,203,296,303,396,403,496],
			[ 2,97,102,197,202,297,302,397,402,497],
			[ 1,98,102,198,201,298,301,398,401,498],
			[ 0,99,100,199,200,299,300,399,400,499]])
		  
	#mymatrix.dtype=np.int16
	print 'Press Ctrl-C to quit.'
	while True: 
		# Rainbow animations
		currtime=datetime.datetime.now()
		#Leds are GRB in the colour registers
		print currtime.hour
		if((currtime.hour>=16) and (currtime.hour<23)):
				cyclecolumn(strip, Color(127,0,0))
				sparkly(strip,Color(127,0,0),100,1000)
				cyclecolumn(strip,Color(0,127,0))
				sparkly(strip,Color(0,127,0),100,1000)
				cyclecolumn(strip,Color(0,0,127))
				sparkly(strip,Color(0,0,127),100,1000)
				cyclecolumn(strip,Color(127,127,127))
				rainbowCycle(strip)
		else:
			for ctr in range(LED_COUNT-1):
				strip.setPixelColor(ctr,Color (0,0,0) )
			strip.show()
