import urllib
import simplejson
import time
import RPi.GPIO as GPIO

GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

ASCII_DIC = {'a': '01100001',
             'b': '01100010',
             'c': '01100011',
             'd': '01100100',
             'e': '01100101',
             'f': '01100110',
             'g': '01100111',
             'h': '01101000',
             'i': '01101001',
             'j': '01101010',
             'k': '01101011',
             'l': '01101100',
             'm': '01101101',
             'n': '01101110',
             'o': '01101111',
             'p': '01110000',
             'q': '01110001',
             'r': '01110010',
             's': '01110011',
             't': '01110100',
             'u': '01110101',
             'v': '01110110',
             'w': '01110111',
             'x': '01111000',
             'y': '01111001',
             'z': '01111010',
             ' ': '00100000',
             '@': '11001100',
             '_': '11111111',
             'D': '11111111',
             'A': '11111111'}

def latest_tweet(twitter_handle):
    twitter_results = urllib.urlopen("http://search.twitter.com/search.json?q="+twitter_handle)
    results_list = simplejson.loads(twitter_results.read())
    return results_list["results"][0]["text"]
    
def text_to_Binary(x):
    """looks up the Binary values in Dictionary"""
    GPIO.output(7,GPIO.LOW)
    for i in x:
       return ASCII_DIC [i]
       #print ASCII_DIC [i]

def bin_Word(n):
    binaryA = []
    for i in n:
        if i == '1':
            binaryA.append("1")
        elif i == '0':
            binaryA.append("0")
    return binaryA
    #print binaryA

def TextToBinScreen(s):
    binT = []
    count=0
    for c in s:
        binW=bin_Word(text_to_Binary(c))
        binT.append(binW)

    for w in binT:
        #GPIO.output(7,GPIO.LOW)
        count += 1
        for l in w:
            print l
            if l == "1":
                GPIO.output(7,GPIO.HIGH)
                print ("gpio 7 High")
                time.sleep(0.1)
            elif l == "0":
                GPIO.output(7,GPIO.LOW)
                time.sleep(0.1)
                print ("gpio 7 LOW")    


while(True):
    Tweet=latest_tweet()# enter you twitter handle
    print Tweet
    text_to_Binary(Tweet)
    TextToBinScreen(Tweet)






            
            
            
    #print "Count: ",count

    #print "Count: ",count

text_to_Binary(Tweet)
TextToBinScreen(Tweet)

#def TakeInputAndReturn():
    #GPIO.output(7,GPIO.LOW)
    #text_to_Binary(Tweet)
    #TextToBinScreen(Tweet)
    #if raw_input("Run again? Y/N ") == "Y":
        #TakeInputAndReturn()

#TakeInputAndReturn()

    

