What: Using a raspberry pi and some LEDs to send christmas poems in morse code
Why: Controlling LEDs with raspberry
How: Using RPi.GPIO library, some simple LEDs and a python script
Morse code
A basic morse code library for python can be found at github. Essentially, it maps lower case characters (no special ones) to a sequence of morse symbols.
It is a python dictionary with the characters as keys:
s='short' l='long' p='pause' alphabet={} alphabet['a']=[s, l] alphabet['b']=[l, s, s, s]
You can use it in any python program:
from morsecode import alphabet for character in text: code=alphabet[character] for dur in code: if dur=='short': # Do something here pass if dur=='long': # Do something here pass if dur=='short' or dur=='long': # Do something here pass if dur=='pause': # Do something here pass characterbreak() textbreak()
Translate text to LED signals
Using two LEDs, one can encode the dit (short symbol) and the other one the dah (long symbol) in morse code. You need additional breaks between characters and words. These can be encoded by not flashing any LED.
Putting it together with the morse alphabet and simple logic for processing arbitrary text (note: the script below does not check, if all characters used are present in the alphabet; just use lower case ones and comma and dot):
#!/usr/bin/python import RPi.GPIO as GPIO import time from morsecode import alphabet import sys GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.OUT) GPIO.setup(10, GPIO.OUT) if len(sys.argv)==1: print('you have to provide the text as argument') sys.exit(1) text=sys.argv[1].lower() dittime=0.25 def dit(): GPIO.output((4, 10), (GPIO.HIGH, GPIO.LOW)) time.sleep(dittime) def dah(): GPIO.output((4, 10), (GPIO.LOW, GPIO.HIGH)) time.sleep(3*dittime) def symbolbreak(): GPIO.output((4, 10), (GPIO.LOW, GPIO.LOW)) time.sleep(dittime) def pause(): GPIO.output((4, 10), (GPIO.LOW, GPIO.LOW)) time.sleep(4*dittime) def characterbreak(): GPIO.output((4, 10), (GPIO.LOW, GPIO.LOW)) time.sleep(3*dittime) def textbreak(): GPIO.output((4, 10), (GPIO.LOW, GPIO.LOW)) time.sleep(10*dittime) while True: for character in text: code=alphabet[character] for dur in code: if dur=='short': dit() if dur=='long': dah() if dur=='short' or dur=='long': symbolbreak() if dur=='pause': pause() characterbreak() textbreak() GPIO.cleanup()
You can start it with (assume you saved the above code in a file called morseflash.py and assigned execution rights):
./morseflash.py "I syng of a mayden that is makeles, \ kyng of alle kynges to here sone che ches. ..."
See it in action: