import hyperion, time, colorsys, random

# Get the parameters
sleepTime = float(hyperion.args.get('AsleepTime', 0.01))
color = hyperion.args.get('color', (255,0,0))
text = hyperion.args.get('text', "Hello World")

# Check parameters

# Initialize the led data
width = 150
height = 5
imageData = bytearray(height * width * (0,0,0))

fontarray=[]

fontarray += (0x00,0x00,0x00,0x00,0x00)	# space
fontarray += (0x08,0x08,0x08,0x00,0x08)	# !
fontarray += (0x00,0x00,0x00,0x00,0x00)	# "
fontarray += (0x00,0x00,0x00,0x00,0x00)	# #
fontarray += (0x00,0x00,0x00,0x00,0x00)	# $
fontarray += (0x00,0x00,0x00,0x00,0x00)	# %
fontarray += (0x00,0x00,0x00,0x00,0x00)	# &
fontarray += (0x00,0x00,0x00,0x00,0x00)	# '
fontarray += (0x00,0x00,0x00,0x00,0x00)	# (
fontarray += (0x00,0x00,0x00,0x00,0x00)	# )
fontarray += (0x00,0x00,0x00,0x00,0x00)	# *
fontarray += (0x00,0x00,0x00,0x00,0x00)	# +
fontarray += (0x00,0x00,0x00,0x00,0x00)	# ,
fontarray += (0x00,0x00,0x00,0x00,0x00)	# -
fontarray += (0x00,0x00,0x00,0x0c,0x0c)	# .
fontarray += (0x00,0x00,0x00,0x00,0x00)	# /

fontarray += (0x3E,0x26,0x2A,0x32,0x3E)	# 0
fontarray += (0x08,0x18,0x08,0x08,0x1C)
fontarray += (0x3C,0x02,0x1C,0x20,0x3E)
fontarray += (0x3E,0x02,0x1C,0x02,0x3E)
fontarray += (0x20,0x20,0x28,0x3E,0x08)
fontarray += (0x3E,0x20,0x3C,0x02,0x3C)
fontarray += (0x3E,0x20,0x3E,0x22,0x3E)
fontarray += (0x3E,0x02,0x04,0x08,0x08)
fontarray += (0x3E,0x22,0x3E,0x22,0x3E)
fontarray += (0x3E,0x22,0x3E,0x02,0x3E)	# 9
fontarray += (0x00,0x08,0x00,0x08,0x00)	# :
fontarray += (0x00,0x00,0x00,0x00,0x00)	# ;
fontarray += (0x00,0x00,0x00,0x00,0x00)	# <
fontarray += (0x00,0x00,0x00,0x00,0x00)	# =
fontarray += (0x00,0x00,0x00,0x00,0x00)	# >
fontarray += (0x00,0x00,0x00,0x00,0x00)	# ?

fontarray += (0x0E,0x15,0x17,0x10,0x3E)		# @
fontarray += (0x3E,0x22,0x22,0x3E,0x22)		# A
fontarray += (0x3E,0x22,0x3C,0x22,0x3E)
fontarray += (0x3E,0x20,0x20,0x20,0x3E)
fontarray += (0x3C,0x22,0x22,0x22,0x3C)
fontarray += (0x3E,0x20,0x3C,0x20,0x3E)
fontarray += (0x3E,0x20,0x38,0x20,0x20)
fontarray += (0x3E,0x20,0x26,0x22,0x3E)
fontarray += (0x22,0x22,0x3E,0x22,0x22)
fontarray += (0x3E,0x08,0x08,0x08,0x3E)
fontarray += (0x06,0x02,0x02,0x22,0x3E)
fontarray += (0x22,0x24,0x38,0x24,0x22)
fontarray += (0x20,0x20,0x20,0x20,0x3E)
fontarray += (0x22,0x36,0x2A,0x22,0x22)
fontarray += (0x22,0x32,0x2A,0x26,0x22)
fontarray += (0x1C,0x22,0x22,0x22,0x1C)
fontarray += (0x3C,0x22,0x3C,0x20,0x20)
fontarray += (0x3E,0x22,0x22,0x3E,0x08)
fontarray += (0x3C,0x22,0x3C,0x22,0x22)
fontarray += (0x3E,0x20,0x3E,0x02,0x3E)
fontarray += (0x3E,0x08,0x08,0x08,0x08)
fontarray += (0x22,0x22,0x22,0x22,0x3E)
fontarray += (0x22,0x22,0x14,0x14,0x08)
fontarray += (0x22,0x22,0x2A,0x2A,0x14)
fontarray += (0x22,0x14,0x08,0x14,0x22)
fontarray += (0x22,0x22,0x14,0x08,0x08)
fontarray += (0x3E,0x04,0x08,0x10,0x3E)	# Z

def plot_string(x, y, str, fg, bg, xscale):
	xplot = x
	for c in range(0,len(str)):
		plot_char(xplot, y, str[c], fg, bg, xscale)
		xplot = xplot + 7*xscale


def plot_char(x, plotY, char, fg, bg, xscale):
	offset = ascii_to_offset(char)

	for y in range (0, 5):
		row = fontarray[offset+y]
		plotX = x
		for bit in range (6,-1,-1):
			if ((1<<bit) & row):
				for i in range (0,xscale):
					setPixel(plotX + i, plotY + y, fg)
			else:
				for i in range (0,xscale):
					setPixel(plotX + i, plotY + y, bg)
			plotX = plotX + xscale

def ascii_to_offset(char):
	chr = ord(char)
# symbnols, numbers, capital letters
	if (chr>=0x20) and (chr<0x5b):
		return 5*(chr-0x20);
# lower case letters
	elif (chr>=0x61) and (chr<=0x7a):
		return 5*(chr-0x40);
	else:
		return 0;	# which is space = blank

def setPixel(x,y,rgb):
	global imageData, width, height
	if (x<width) and (y<height) and (x>=0) and (y>=0):
		offset = y*width*3 + x*3
		if offset+2 < len(imageData):
			imageData[offset]   = rgb[0]
			imageData[offset+1] = rgb[1]
			imageData[offset+2] = rgb[2]

def randomColor():
	color = colorsys.hsv_to_rgb(random.random() ,1, 1)
	color = ( int(color[0]*255), int(color[1]*255), int(color[2]*255) )
	return color;


while not hyperion.abort():
	linecount = 2
	Yspacing = 8
	for y in range(Yspacing, -Yspacing*linecount, -1):
		imageData = bytearray(height * width * (0,0,0))
		plot_string(0, y,          " Merry ", (128,0,0), (0,0,0), 3)
		plot_string(0, y+Yspacing, " XMAS! ", (128,0,0), (0,0,0), 3)
		hyperion.setImage(width, height, imageData)
		if y%Yspacing == 0:
			time.sleep(1.0)
		else:
			time.sleep(0.1)

	for i in range (0,5):
		imageData = bytearray(height * width * (0,0,0))
		plot_string(0, 0, time.strftime(" %H:%M:%S"), (0,128,128), (0,0,0), 2)
		hyperion.setImage(width, height, imageData)
		time.sleep(1.0)

while not hyperion.abort():
	for x in range(-196,150):
		imageData = bytearray(height * width * (0,0,0))
		plot_string(x, 1, "Merry XMAS !!!", (255,0,0), (0,0,0), 2)
		hyperion.setImage(width, height, imageData)
		time.sleep(0.1)

	for x in range(150,-197,-1):
		imageData = bytearray(height * width * (0,0,0))
		plot_string(x, 0, "Merry XMAS !!!", (0,255,0), (0,0,0), 2)
		hyperion.setImage(width, height, imageData)
		time.sleep(0.1)

