from gpiozero import LED
import time
#test ws281x ring
from rpi_ws281x import PixelStrip,Color

import sys
inputString = sys.argv[1]

LED_COUNT = 40
LED_PIN = 12
LED_FREQ_HZ = 800000
LED_DMA = 10
LED_BRIGHTNESS = 255
LED_INVERT = False
LED_CHANNEL = 0
#Python dictionary containing characters and the matching morse code
converted = {"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--..","0":"-----","1":".----","2":"..---","3":"...--","4":"....-","5":".....","6":"-....","7":"--...","8":"---..","9":"----.",".":".-.-.-",",":"--..--",":":"---...","?":"..--..","'":".----.","-":"-....-","/":"-..-.","(":"-.--.-",")":"-.--.-","\"":".-..-.","@":".--.-.","=":"-...-","[":"-.--.-","]":"-.--.-","$":"...-..-","+":".-.-.",";":"-.-.-.","_":"..--.-","!":"---."}

#led = LED(26)                            #Assign pin #26 to a variable
speedPercent = 100                       #Change to proportionately alter speed; 100 = Normal Speed; 50 = Half Speed
longTime = 30.0 / speedPercent           #Duration of the dash
shortTime = 10.0 / speedPercent          #Duration of the period
intraTime = 10.0 / speedPercent          #Duration of space between flash on same character
spaceTime = 70.0 / speedPercent          #Duration between words
betweenCharTime = 30.0 / speedPercent    #Duration between characters
#inputString = ""

def ledOn(strip):
	for i in range(strip.numPixels()):
		strip.setPixelColor(i,Color(255,255,255))
	strip.show()

def ledOff(strip):
	for i in range(strip.numPixels()):
		strip.setPixelColor(i,Color(0,0,0))
	strip.show()

#Flash LED for the dash
def longFlash(strip):
	ledOn(strip)
	time.sleep(longTime)
	ledOff(strip)

#Flash LED for the period
def shortFlash(strip):
	ledOn(strip)
	time.sleep(shortTime)
	ledOff(strip)

#Sleep for time of a space
def space():
	time.sleep(spaceTime)
#Ask for input
#returns the input
def getInput():
	return raw_input("Please enter text or type \"quit\" to exit: ")

#Make input lowercase and assign it to a variable
#inputString = "HELLO WORLD" #getInput().lower()

strip = PixelStrip(LED_COUNT,LED_PIN,LED_FREQ_HZ,LED_DMA,LED_INVERT,LED_BRIGHTNESS,LED_CHANNEL)
strip.begin()
ledOff(strip)
#While the input isn't "quit"
print inputString
while (inputString != "quit"):
	#Go through each character of the input
	for c in inputString:
		#If it is a space
		if c == " ":
			space()
		#If it is in the dictionary
		elif c in converted:
			#convert character to the morse code
			morseconverted = converted[c]
			#goes through each character in the morse code
			for symbol in morseconverted:
				#If the symbol is a dash
				if symbol == "-":
					longFlash(strip)
					time.sleep(intraTime)
				#If the symbol is a period
				elif symbol == ".":
					shortFlash(strip)
					time.sleep(intraTime)
				#If the symbol is somehow not a dash or period
				else:
					print "Not a '-' or '.'"
			time.sleep(betweenCharTime)
		#If the character is not supported in the dictionary
		else:
			print "'" + c + "'" + " is not a supported character"
	#Ask for input again
	#inputString = getInput().lower()
	exit()
