import RPi.GPIO as GPIO
import time

DELAY = 0.0001
CLK = 27
DATA = 17

GPIO.setmode(GPIO.BCM)
GPIO.setup(DATA, GPIO.OUT)    
GPIO.setup(CLK, GPIO.OUT)

def sendData(data):
    if data == 1:
        GPIO.output(CLK, GPIO.LOW)
        GPIO.output(DATA, GPIO.HIGH)
        time.sleep(DELAY)
        GPIO.output(CLK, GPIO.HIGH)
        time.sleep(DELAY)
    elif data == 0:
        GPIO.output(CLK, GPIO.LOW)
        GPIO.output(DATA, GPIO.LOW)
        time.sleep(DELAY)
        GPIO.output(CLK, GPIO.HIGH)
        time.sleep(DELAY)
        
def sendByte(data):
        binary_str = format(data, '08b')
        bits = [int(bit) for bit in binary_str]
        for i, bit in enumerate(binary_str):
                sendData(bits[i])

def encode(c):
    table = {
        'START': 0xFF,
        'STOP': 0x00, '1': 0x81, '2': 0x82, '3': 0x83, '4': 0x84, '5': 0x85, '6': 0x86, '7': 0x87,
        '8': 0x88, '9': 0x89, '0': 0x8A, 'a': 0x8B, 'b': 0x8C, 'c': 0x8D, 'd': 0x8E, 'e': 0x8F,
        'f': 0x10, 'g': 0x11, 'h': 0x12, 'i': 0x13, 'j': 0x14, 'k': 0x15, 'l': 0x16, 'm': 0x17,
        'n': 0x18, 'o': 0x19, 'p': 0x1A, 'q': 0x1B, 'r': 0x1C, 's': 0x1D, 't': 0x1E, 'u': 0x1F,
        'v': 0x20, 'w': 0x21, 'x': 0x22, 'y': 0x23, 'z': 0x24, 'A': 0x25, 'B': 0x26, 'C': 0x27,
        'D': 0x28, 'E': 0x29, 'F': 0x2A, 'G': 0x2B, 'H': 0x2C, 'I': 0x2D, 'J': 0x2E, 'K': 0x2F,
        'L': 0x30, 'M': 0x31, 'N': 0x32, 'O': 0x33, 'P': 0x34, 'Q': 0x35, 'R': 0x36, 'S': 0x37,
        'T': 0x38, 'U': 0x39, 'V': 0x3A, 'W': 0x3B, 'X': 0x3C, 'Y': 0x3D, 'Z': 0x3E, ' ': 0x3F,
        '!': 0x40, '?': 0x41, '.': 0x42, '*': 0x43, '/': 0x44, '&': 0x45, ':': 0x46, '"': 0x47,
        '(': 0x48, ')': 0x49, '@': 0x4A, ',': 0x4B, '#': 0x4C, "'": 0x4D, '=': 0x4E, '+': 0x4F
    }
    
    return table.get(c, None)
    
def sendString(s):
        sendByte(encode('START'))
        for c in s:
                sendByte(encode(c))
        sendByte(encode('STOP'))

try:
    while True:
            inputStr = input("Enter message: ")
            sendString(inputStr)

except KeyboardInterrupt:
    print("Exiting...")
    GPIO.cleanup() 



