#!/usr/bin/env python # # term.py # # term.py serial_port port_speed # # Neil Gershenfeld # CBA MIT 7/27/07 # Additions by Lindy Liggett # 27Nov2012 # # (c) Massachusetts Institute of Technology 2007 # Permission granted for experimental and personal use; # license for commercial sale available from MIT. # import sys,time,serial from Tkinter import * import tkMessageBox from select import * NROWS = 5 NCOLS = 80 def key(event): # # key press event handles # key = event.char #print 'send',ord(key) if (ord(key) == 13): key = chr(10) ser.write(key) #I think getting rid of this doesn't send the LETTER to the circuit def quit(): # # clean up and quit # sys.exit() def sendToLED(message): #define this!! print("sending to LED") for letter in message: ser.write(letter) #send the letter print(letter) #so it sends... gets stuck? byte = ser.read(1) while (ord(byte) != 3): #Waiting for it to be one again byte = ser.read() #just keep checking #then it should loop around again def displayText(message): """ Display the Entry text value. """ global entryWidget #global widget_text if entryWidget.get().strip() == "": tkMessageBox.showerror("Wait a second", "Enter a text value, please") else: tkMessageBox.showinfo("Morse Code Entry Widget", "In Morse: " + message) #widget_text.delete(1,10) #widget_text.insert(message,1) #hmmm this doesn't seem to work :/ def morse(): """ Translate Entry text value to Morse. """ global widget_text #morse code array morseDict = { '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':'--..', ' ':'_', ',':'--..--', '.':'.-.-.-', '?': '..--..', ';': '-.-.-.', ':': '---...', "'": '.----.', '-': '-....-', '/': '-..-.', '(': '-.--.-', ')': '-.--.-', '_': '..--.-', } text = list(entryWidget.get().strip()) morse_list = list() #initialize to nothing for char in text: for key in morseDict: if (char.lower() == key): value = morseDict[key] morse_list.append(value) #keep adding values #print morse_list morse_out = '' #initialize to zero for item in morse_list: morse_out = morse_out + item + ' ' #space separates the letters morse_out = morse_out + '.-.-.-' #add period to end to signify end print 'Your text string in Morse Code is: \n' print morse_out #displayText(morse_out) #sendToLED(morse_out) def idle(parent): # # idle loop # wait = ser.inWaiting() if (wait != 0): # # read character # byte = ser.read() widget_text.config(state=NORMAL) #print byte,ord(byte) if (ord(byte) == 10): #sending a 10 gives it a new line # # CR # widget_text.insert(INSERT,'\n') if (int(float(widget_text.index(END))) > (NROWS+1)): widget_text.delete(1.0,2.0) #if (ord(byte) == 13): # # CR # #widget_text.insert(INSERT,'\n') #if (int(float(widget_text.index(END))) > (NROWS+1)): # widget_text.delete(1.0,2.0) elif (byte == 8): # # BS # widget_text.delete(INSERT+"-1c",INSERT) else: # # character # widget_text.insert(INSERT,byte) widget_text.config(state=DISABLED) #time.sleep(0.001) parent.after_idle(idle,parent) # # check command line arguments # if (len(sys.argv) != 3): print "command line: term.py serial_port speed" sys.exit() port = sys.argv[1] speed = int(sys.argv[2]) # # open serial port # ser = serial.Serial(port,speed) ser.setDTR() # # flush buffers # ser.flushInput() ser.flushOutput() # # set up UI - THIS IS WHERE I CAN ADD MORSE STUFF # root = Tk() root.bind('',key) root.title('term.py') # widget_quit = Button(root, text="quit",command=quit) widget_quit.pack() # widget_morse = Button(root, text="Convert To Morse", command=morse) widget_morse.pack() # address_frame = Frame(root) #Label(address_frame,text="port: "+port).pack(side='left') #Label(address_frame,text=" speed: "+str(speed)).pack(side='left') address_frame.pack() # widget_text = Text(root, bg='white', bd=5, width=NCOLS, height=NROWS, font=('arial',10,'bold')) #for output #widget_text.bind('',key) widget_text.config(state=DISABLED) widget_text.pack() # #Create an Entry Widget in textFrame - for entering text!! textFrame = Frame(root) entryWidget = Entry(textFrame) entryWidget["width"] = 50 entryWidget.pack(side=LEFT) textFrame.pack() # # begin event loop # root.after(100,idle,root) #I THINK THIS MAKES THE IDLE NOT HAPPEN... I.E. DOESN'T OUTPUT WHAT YOU'RE TYPING root.mainloop()