Skip to content

Instantly share code, notes, and snippets.

@Crysknife007
Forked from m0xpd/Morse_Gmail.py
Last active December 15, 2015 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Crysknife007/5230847 to your computer and use it in GitHub Desktop.
Save Crysknife007/5230847 to your computer and use it in GitHub Desktop.
Morse code driver for Raspberry Pi. Prints text from a text file.
#!/usr/bin/env python
"""
BlinkTxt - Spike Snell - 2013
Modified from Morse_Gmail.py
by m0xpd December 2012
"""
# Import what we need
import time, sys
import RPi.GPIO as GPIO
""" Dictionary for Morse Code Characters """
morse={'a':'01','b':'1000','c':'1010','d':'100','e':'0','f':'0010','g':'110',
'h':'0000','i':'00','j':'0111','k':'101','l':'0100','m':'11',
'n':'10','o':'111','p':'0110','q':'1101','r':'010','s':'000','t':'1',
'u':'001','v':'0001','w':'011','x':'1001','y':'1011','z':'1100',
'0':'11111','1':'01111','2':'00111','3':'00011','4':'00001',
'5':'00000','6':'10000','7':'11000','8':'11100','9':'11110',
'.':'010101',',':'110011','?':'001100','/':'10010','@':'011010',
'!':'00110','\'':'011110','\"':'010010',' ':'',':':'111000',
';':'111000','*':'10001','-':'010101'}
# Last line are some kludges, there are no standard morse code
# Equivalents for these characters so substitute colon for semicolon
# And substitute asterisk for the equals sign, and dash for period
# Adjust Morse Speed by changing the DitLength
DitLength=0.1 # (seconds)
DahLength=3*DitLength
# Set Up the GPIO to use pin 5
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.OUT)
GPIO.output(5, False)
# A few function definitions
def SendDit(Length):
""" Sends a Dit """
KeyDown(Length)
Last_Element="dit"
return Last_Element
def SendDah(Length):
""" Sends a Dah """
KeyDown(Length)
Last_Element="dah"
return Last_Element
def SendSpace(Length):
""" Wait for inter-element space """
time.sleep(Length)
return
def KeyDown(Length):
""" Keys the TX """
# Set the output to Key Down
GPIO.output(5, True)
time.sleep(Length)
# Clear the output
GPIO.output(5, False)
return
def BlinkWord(Word):
for i in range(0,len(Word)):
morse_character=morse[Word[i].lower()]
for element in range(0,len(morse_character)):
if morse_character[element]=="0":
junk=SendDit(DitLength)
SendSpace(DitLength)
else:
junk=SendDah(DahLength)
SendSpace(DitLength)
SendSpace(DahLength)
time.sleep(1)
# Main Operating Loop
while True:
# Open a file
with open(sys.argv[1], "r") as f:
print "Opening file for blinking: ", f.name
# Open up the file and split up all the words into all_words
all_words = map(lambda l: l.split(" "), f.readlines())
# Blink out every word in all_words
for i in range(0,len(all_words)):
for s in all_words[i]:
# Print the word to the screen
print s.rstrip('\n')
# Blink the word to the light
BlinkWord(s.rstrip('\n'))
break
@Crysknife007
Copy link
Author

http://www.youtube.com/watch?v=Fxt3UvCt4R0

This is a sample video of a Raspberry Pi blinking out the full text of the Tao Te Ching written by Lao-tzu from a translation by S. Mitchell.

Full text available at: nrg0.org/t.txt

Pictures: minus.com/lzeqUnDviv1YE minus.com/l3e0estbnwaee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment