Skip to content

Instantly share code, notes, and snippets.

@kepsic
Last active October 19, 2016 07:30
Show Gist options
  • Save kepsic/ae965fafb355c49e51104cf9c1a40c2c to your computer and use it in GitHub Desktop.
Save kepsic/ae965fafb355c49e51104cf9c1a40c2c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#ref http://effbot.org/pyfaq/how-do-i-get-a-single-keypress-at-a-time.htm
#ref https://en.wikipedia.org/wiki/NATO_phonetic_alphabet
import termios, fcntl, sys, os
alfabet={
"1":{"a":"One","m":".----"},
"2":{"a":"Two","m":"..---"},
"3":{"a":"Three","m":"...--"},
"4":{"a":"Four","m":"....-"},
"5":{"a":"Five","m":"....."},
"6":{"a":"Six","m":"-...."},
"7":{"a":"Seven","m":"--..."},
"8":{"a":"Eight","m":"---.."},
"9":{"a":"Nine","m":"----."},
"0":{"a":"Zero","m":"-----"},
"A":{"a":"Alfa","m":".-"},
"B":{"a":"Bravo","m":"-..."},
"C":{"a":"Charlie","m":"-.-.-"},
"D":{"a":"Delta","m":"-.."},
"E":{"a":"Echo","m":"."},
"F":{"a":"Foxtrot","m":"..-."},
"G":{"a":"Golf","m":"--."},
"H":{"a":"Hotel","m":"...."},
"I":{"a":"India","m":".."},
"J":{"a":"Juliet","m":".---"},
"K":{"a":"Kilo","m":"-.-"},
"L":{"a":"Lima","m":".-.."},
"M":{"a":"Mike","m":"--"},
"N":{"a":"November","m":"-."},
"O":{"a":"Oscar","m":"---"},
"P":{"a":"Papa","m":".--."},
"Q":{"a":"Quebec","m":"--.-"},
"R":{"a":"Romeo","m":".-."},
"S":{"a":"Sierra","m":"..."},
"T":{"a":"Tango","m":"-"},
"U":{"a":"Uniform","m":"..-"},
"V":{"a":"Victor","m":"...-"},
"W":{"a":"Whiskey","m":".--"},
"X":{"a":"X-ray","m":"-..-"},
"Y":{"a":"Yankee","m":"-.--"},
"Z":{"a":"Zulu","m":"--.."},
"Ä":{"a":"Ärni","m":".-"},
"Õ":{"a":"Õnne","m":"----"},
"Ö":{"a":"Ööbik","m":"----"},
"Ü":{"a":"Ülle","m":"..-"},
}
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
def main_loop():
try:
while 1:
try:
c = sys.stdin.read(1)
print("Got character {} Telephony: {}, Morse: {}".format(repr(c), alfabet[c.upper()]["a"], alfabet[c.upper()]["m"]))
except IOError:
pass
except KeyError:
print ""
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
if __name__ == "__main__":
main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment