Embroidering a message with morse code

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:

../_images/morse_en.svg

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 draw_dash(needle, length, height):
 2    """Use the Turtle object ``needle`` to draw a dash.
 3    """
 4    needle.left(90)
 5    needle.forward(height)
 6    needle.right(90)
 7    needle.forward(3*length)
 8    needle.right(90)
 9    needle.forward(height)
10    needle.left(90)
11
12def draw_dot(needle, length, height):
13    """Use the Turtle object ``needle`` to draw a dot.
14    """
15    needle.left(90)
16    needle.forward(height)
17    needle.right(90)
18    needle.forward(length)
19    needle.right(90)
20    needle.forward(height)
21    needle.left(90)
22
23def draw_morse_symbol(symbol, needle, length, height):
24    """Use the Turtle object ``needle`` to draw a morse symbol (dot, dash or pause).
25    """
26    if symbol == ".":
27        draw_dot(needle, length, height)
28    elif symbol == "-":
29        draw_dash(needle, length, height)
30    elif symbol == " ":
31        needle.forward(length)

Now we have code that can draw each sign in a morse code, but we also want code to draw a whole message.

 1def draw_morse_code(morse_code, needle, length, height):
 2    """Use the Turtle object ``needle`` to draw morse code
 3    """
 4    # We want a small space at the beginning. This is not necessary, but makes the embroidery prettier.
 5    needle.forward(length/2)
 6
 7    # We can iterate over each letter in a string
 8    for morsebokstav in morse_code:
 9        draw_morse_symbol(morsebokstav, needle, length=length, height=height)
10        needle.forward(length)  # We add a unit-length space between each symbol
11
12    # We want a small space at the end also. This is not necessary, but makes the embroidery prettier.
13    needle.forward(length/2)

Let’s try this out by drawing SOS (... --- ...).

1needle = Turtle()
2with needle.running_stitch(30):
3    draw_morse_code("... --- ...", needle, 60, 200)
4
5needle.save("sos.svg")
SOS skrevet med morsekode.

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")
The text "hello_morse" as morse code.

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")
The text "hello world" as morse code.