Embroidering a message with morse code#
In this example, we’ll look at how we can translate messages to morse code and how we can make an embroidery pattern from these messages.
Drawing morse code#
We start by importing Turtle
from turtlethread
1from turtlethread import Turtle
The next step is to make code thad draws morse code with a turtle object. We start by defining how we draw a dot and a dash. What we know about morse code is:
A dot lasts for one time unit
A dash lasts for three time units
There is three time units between each sign
There is seven time units between each word
Based on this, we can draw dots and dashes with turtlethread
.
There are many ways we could do this, but we’ve selected the following:
Let’s start by making three functions: one to draw a dash, one to draw a dot and one which takes a morse symbol (dot, dash or space) and draws it.
1def tegn_strek(needle, lengde, høyde):
2 """Bruk skilpaddeobjektet ``needle`` for å tegne en strek.
3 """
4 needle.left(90)
5 needle.forward(høyde)
6 needle.right(90)
7 needle.forward(3*lengde)
8 needle.right(90)
9 needle.forward(høyde)
10 needle.left(90)
11
12def tegn_prikk(needle, lengde, høyde):
13 """Bruk skilpaddeobjektet ``needle`` for å tegne prikk.
14 """
15 needle.left(90)
16 needle.forward(høyde)
17 needle.right(90)
18 needle.forward(lengde)
19 needle.right(90)
20 needle.forward(høyde)
21 needle.left(90)
22
23def tegn_morse_tegn(tegn, needle, lengde, høyde):
24 """Bruk skilpaddeobjektet ``needle`` for å tegne et morsetegn (prikk, strek eller pause).
25 """
26 if tegn == ".":
27 tegn_prikk(needle, lengde, høyde)
28 elif tegn == "-":
29 tegn_strek(needle, lengde, høyde)
30 elif tegn == " ":
31 needle.forward(lengde)
Now we have code that can draw each sign in a morse code, but we also want code to draw a whole message.
1def tegn_morsekode(morsekode, needle, lengde, høyde):
2 """Bruk en skilpadde for å tegne morsekode
3 """
4 # Vi vil ha litt mellomrom på starten av strengen, dette er ikke nødvendig, men får det til å se pent ut
5 needle.forward(lengde/2)
6
7 # Vi kan iterere over hvert tegn i en tekststreng
8 for morsebokstav in morsekode:
9 draw_morse_symbol(morsebokstav, needle, lengde=lengde, høyde=høyde)
10 needle.forward(lengde) # Det er en lengdeenhet mellomrom mellom hvert tegn
11
12 # Vi vil ha litt mellomrom på slutten av strengen, dette er ikke nødvendig, men får det til å se pent ut
13 needle.forward(lengde/2)
Let’s try this out by drawing SOS (... --- ...
).
1needle = Turtle()
2with needle.running_stitch(30):
3 tegn_morsekode("... --- ...", needle, 60, 200)
4
5needle.save("sos.svg")
Converting text to morse code#
Next step is to make code that makes text into morse code. To do this, we use a dictionary that maps letters and symbols to morse code.
1MORSE_ALPHABET = {
2 'A': '.-', 'B': '-...', 'C': '-.-.',
3 'D': '-..', 'E': '.', 'F': '..-.',
4 'G': '--.', 'H': '....', 'I': '..',
5 'J': '.---', 'K': '-.-', 'L': '.-..',
6 'M': '--', 'N': '-.', 'O': '---',
7 'P': '.--.', 'Q': '--.-', 'R': '.-.',
8 'S': '...', 'T': '-', 'U': '..-',
9 'V': '...-', 'W': '.--', 'X': '-..-',
10 'Y': '-.--', 'Z': '--..', '1': '.----',
11 '2': '..---', '3': '...--', '4': '....-',
12 '5': '.....', '6': '-....', '7': '--...',
13 '8': '---..', '9': '----.', '0': '-----',
14 ',': '--..--', '.': '.-.-.-', '?': '..--..',
15 '/': '-..-.', '-': '-....-', '(': '-.--.',
16 ')': '-.--.-', ' ': ' '
17}
Next, we create a function that takes a text string as input and transforms it to morse code.
1def translate_text_to_morse(text):
2 morse_text = ""
3 for letter in text:
4 morse_text += MORSE_ALPHABET[letter.upper()]
5 morse_text += " "
6 return morse_text
7
8print(translate_text_to_morse("Hello morse"))
.... . .-.. .-.. --- -- --- .-. ... .
Drawing text as morse code#
We have now managed to write “Hello morse” with morse code!
Let’s use the draw_morse_code
function to draw this message.
1morse_code = translate_text_to_morse("Hello morse")
2needle = Turtle()
3with needle.running_stitch(30):
4 draw_morse_code(morse_code, needle, 60, 200)
5
6needle.save("hello_morse.svg")
Now we have a nice little message. Let’s end it all by making a function that takes a text string as input and uses a turtle object to draw the morse code that represents the string.
1def draw_morse_code_from_text(text, needle, length, height):
2 morsekode = translate_text_to_morse(text)
3 draw_morse_code(morsekode, needle, length, height)
4
5needle = Turtle()
6with needle.running_stitch(30):
7 draw_morse_code_from_text("Hello world", needle, 60, 200)
8
9needle.save("hello_world.jef")
10needle.save("hello_world.svg")