First order of business is to import some libraries to help send/receive signals to/from the GPIO pins of the Raspberry Pi.
In [1]:
import RPi.GPIO as GPIO
import time
The following code will set the mode to a Broadcom pin layout (which seems to be what we want), and set up our GPIO pin 25 as an output pin.
In [2]:
GPIO.setmode(GPIO.BCM)
outpin = 25
GPIO.setup(outpin, GPIO.OUT)
In [3]:
GPIO.output(outpin, True) # Let there be light
In [4]:
GPIO.output(outpin, False) # Turn it off
In [23]:
timeunit = 0.1
dashtime = timeunit*3.0
dottime = timeunit
dotgap = timeunit
lettergap = timeunit*2.0
wordgap = timeunit*7.0
In [8]:
GPIO.output(outpin, False) # start with the LED off
time.sleep(1.0)
for i in range(0,2): # SOS, the hard way...
GPIO.output(outpin, True)
time.sleep(dottime)
GPIO.output(outpin, False)
time.sleep(dotgap)
GPIO.output(outpin, True)
time.sleep(dottime)
GPIO.output(outpin, False)
time.sleep(dotgap)
GPIO.output(outpin, True)
time.sleep(dottime)
GPIO.output(outpin, False)
time.sleep(lettergap)
GPIO.output(outpin, True)
time.sleep(dashtime)
GPIO.output(outpin, False)
time.sleep(dotgap)
GPIO.output(outpin, True)
time.sleep(dashtime)
GPIO.output(outpin, False)
time.sleep(dotgap)
GPIO.output(outpin, True)
time.sleep(dashtime)
GPIO.output(outpin, False)
time.sleep(lettergap)
GPIO.output(outpin, True)
time.sleep(dottime)
GPIO.output(outpin, False)
time.sleep(dotgap)
GPIO.output(outpin, True)
time.sleep(dottime)
GPIO.output(outpin, False)
time.sleep(dotgap)
GPIO.output(outpin, True)
time.sleep(dottime)
GPIO.output(outpin, False)
time.sleep(wordgap)
In [9]:
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': '--..',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
' ': ' ',
',': '--..--',
'.': '.-.-.-',
'?': '..--..',
';': '-.-.-.',
':': '---...',
"'": '.----.',
'-': '-....-',
'/': '-..-.',
'(': '-.--.-',
')': '-.--.-',
'_': '..--.-',
}
dotdashmap = {".": dottime, "-": dashtime, " ": wordgap}
In [10]:
morsedict['D']
Out[10]:
In [14]:
for itm in morsedict['D']:
print dotdashmap[itm],
In [15]:
def blink_letter(letter):
for s in morsedict[letter.upper()]:
GPIO.output(outpin, True)
time.sleep(dotdashmap[s])
GPIO.output(outpin, False)
time.sleep(dotgap)
time.sleep(lettergap) # end of letter time gap (for a total of 3x the dot unit.)
Let's break this down a bit before we see what happens when we call this function.
In [16]:
s = "q"
In [17]:
s.upper()
Out[17]:
In [20]:
morsedict[s.upper()]
Out[20]:
In [21]:
dotdashmap['-']
Out[21]:
In [24]:
blink_letter('q')
In [25]:
def blink_message(message):
for letter in message:
blink_letter(letter)
In [26]:
blink_message("Hello")
In [36]:
GPIO.cleanup()
In [ ]: