1. #!/usr/bin/python
  2. import RPi.GPIO as GPIO
  3. from time import sleep
  4. GPIO.setmode(GPIO.BCM)
  5. GPIO.setup(4, GPIO.OUT)
  6. chars = {'1': ".----", '2': "..---", '3': "...--", '4': "....-", '5': ".....",
  7. '6': "-....", '7': "--...", '8': "---..", '9': "----.", '0': "-----",
  8. 'A': ".-", 'B': "-...", 'C': "-.-.", 'D': "-..", 'E': ".",
  9. 'F': "..-.", 'G': "--.", 'H': "....", 'I': "..", 'J': ".---",
  10. 'K': "-.-", 'L': ".-..", 'M': "--", 'N': "-.", 'O': "---",
  11. 'P': ".--.", 'Q': "--.-", 'R': ".-.", 'S': "...", 'T': "-",
  12. 'U': "..-", 'V': "...-", 'W': ".--", 'X': "-..-", 'Y': "-.--",
  13. 'Z': "--..", '.': ".-.-.-", ',': "--..--", '?': "..--..", '\'':".----.",
  14. '!': "-.-.--", '/': "-..-.", '(': "-.--.", ')': "-.--.-", '&': ".-...",
  15. ':': "---...", ';': "-.-.-.", '=': "-...-", '+': ".-.-.", '-': "-....-",
  16. '_': "..--.-","\"": ".-..-.", '$': "...-..-",'@': ".--.-."}
  17. wpm = 0
  18. while wpm == 0:
  19. try:
  20. print "How fast do you want to send morse code (in words per minute)?"
  21. print "How fast do you want to send MORSE in words per minute?"
  22. print "The world record is at 75 wpm, but I can't read more than 10 wpm"
  23. wpm = float(raw_input('=> '))
  24. speed = 1.0/wpm*1.2
  25. except:
  26. print "This is not a valid number!"
  27. print
  28. while True:
  29. print "Type the text you want to send in morse and press enter"
  30. print "to end the program, type nothing and press enter"
  31. s = raw_input('=> ').upper()
  32. if len(s) == 0: break;
  33. for letter in s:
  34. if letter == " ":
  35. sleep(speed*5)
  36. print
  37. else:
  38. try:
  39. symbols = chars[letter]
  40. except:
  41. symbols = ""
  42. print letter, symbols
  43. for symbol in symbols:
  44. if symbol == ".":
  45. GPIO.output(4, True)
  46. sleep(speed)
  47. GPIO.output(4, False)
  48. sleep(speed)
  49. elif symbol == "-":
  50. GPIO.output(4, True)
  51. sleep(speed*3)
  52. GPIO.output(4, False)
  53. sleep(speed)
  54. sleep(speed*2)
  55. print
  56. GPIO.cleanup()