- #!/usr/bin/python
- import RPi.GPIO as GPIO
- from time import sleep
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(4, GPIO.OUT)
- chars = {'1': ".----", '2': "..---", '3': "...--", '4': "....-", '5': ".....",
- '6': "-....", '7': "--...", '8': "---..", '9': "----.", '0': "-----",
- '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': "--..", '.': ".-.-.-", ',': "--..--", '?': "..--..", '\'':".----.",
- '!': "-.-.--", '/': "-..-.", '(': "-.--.", ')': "-.--.-", '&': ".-...",
- ':': "---...", ';': "-.-.-.", '=': "-...-", '+': ".-.-.", '-': "-....-",
- '_': "..--.-","\"": ".-..-.", '$': "...-..-",'@': ".--.-."}
- wpm = 0
- while wpm == 0:
- try:
- print "How fast do you want to send morse code (in words per minute)?"
- print "How fast do you want to send MORSE in words per minute?"
- print "The world record is at 75 wpm, but I can't read more than 10 wpm"
- wpm = float(raw_input('=> '))
- speed = 1.0/wpm*1.2
- except:
- print "This is not a valid number!"
- print
- while True:
- print "Type the text you want to send in morse and press enter"
- print "to end the program, type nothing and press enter"
- s = raw_input('=> ').upper()
- if len(s) == 0: break;
- for letter in s:
- if letter == " ":
- sleep(speed*5)
- print
- else:
- try:
- symbols = chars[letter]
- except:
- symbols = ""
- print letter, symbols
- for symbol in symbols:
- if symbol == ".":
- GPIO.output(4, True)
- sleep(speed)
- GPIO.output(4, False)
- sleep(speed)
- elif symbol == "-":
- GPIO.output(4, True)
- sleep(speed*3)
- GPIO.output(4, False)
- sleep(speed)
- sleep(speed*2)
- print
- GPIO.cleanup()