[Swlug] Brag time - Morse code sending on a Raspberry Pi Pico

Ivan Wright quidsup at gmail.com
Wed Apr 20 16:47:44 UTC 2022


Hi Rhys,

That's a good go with Python scripting.
There's some features in Python that can make the code a bit simpler.
Starting from the top I have done:
Type declarations on the function SendLetter (aesthetic rather than
functionality).
Used a dictionary for the morse code instead of two lists. It's
an associative array which is good for storing key-value pair data.
Used a for in loop to read every character of MessageToSend. For in loops
can also be used to read all items in a list or dictionary.
Combined the two lines of print with a single f-string. Although I could
have also done print(char, morse.get(char)) to achieve the same result, but
f-strings are a very useful feature in Python and well worth reading up
about.

When taking a value from a dictionary you can use either the get function
or square brackets, e.g. SendLetter(morse[char])
The get function can have fallback value supplied in case you try using a
key which does not exist, e.g. SendLetter(morse.get(char, 'alternate
value'))

Ivan


from machine import Pin, Timer
import utime
led = Pin(25, Pin.OUT)
utimer = Timer()
led.value(1)
utime.sleep(1)
led.value(0)

def SendLetter(Letter: str) -> None:
    led.value(0)
    MaxLength = len(Letter)
    for MorseCounter in range (0,MaxLength):
        if Letter[MorseCounter] == '-':
            led.value(1)
            utime.sleep(1.5)
            led.value(0)
        else:
            led.value(1)
            utime.sleep(0.5)
            led.value(0)
    utime.sleep(1.5)

#Morse Code
morse = {
    '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': '--..',
}

char: str
MessageToSend: str = 'monty python'
MessageToSend = MessageToSend.lower()

for char in MessageToSend:
    if char != ' ':
        SendLetter(morse.get(char))
        print(f'{char} {morse.get(char)}')
    else:
        print('space')
        utime.sleep(3.5)

On Sat, 16 Apr 2022 at 19:25, Rhys Sage via Swlug <swlug at mailman.lug.org.uk>
wrote:

> It could do with some tidying and inclusion of Morse number & symbols  but
> this code seems to work on the Pi Pico for transmitting painfully slow
> Morse code via the onboard LED. I just felt like bragging about a Saturday
> morning's playing around.
>
>
> #!/usr/bin/env python3
> # -*- coding: utf-8 -*-
> """
> Created on Sat Apr 16 11:02:05 2022
> @author: mary
> """
> from machine import Pin, Timer
> import utime
> led = Pin(25, Pin.OUT)
> timer = Timer()
> led.value(1)
> utime.sleep(1)
> led.value(0)
> def SendLetter(Letter):
>     led.value(0)
>     MaxLength = len(Letter)
>     for MorseCounter in range (0,MaxLength):
>         if Letter[MorseCounter] == '-':
>             led.value(1)
>             utime.sleep(1.5)
>             led.value(0)
>         else:
>             led.value(1)
>             utime.sleep(0.5)
>             led.value(0)
>     utime.sleep(1.5)
> #Morse Code
> Alphabet =
> ['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']
> Morse = ['.-','-...','-.-.','-..','.','..-.','--.','--.','..','.---','-.-',
>          '.-..','--','-.','---','.--.',
>          '--.-','.-.','..','-','..-','...-','.--','-..-','-.--','--..']
> TimeUnit = 1 #in seconds)
> MessageToSend = 'monty python'
> MessageToSend = MessageToSend.lower()
> MaxNum = len(MessageToSend)
> for MessageCounter in range (0,MaxNum):
>     if MessageToSend[MessageCounter] != ' ':
>         SendLetter(Morse[MessageCounter])
>         print (MessageToSend[MessageCounter])
>         print (Morse[Alphabet.index(MessageToSend[MessageCounter])])
>     else:
>         print ('space')
>         utime.sleep(3.5)
>
>
>
>
> Rhys Sage
>
> --
> Swlug mailing list
> Swlug at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/swlug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/swlug/attachments/20220420/4662c93e/attachment.htm>


More information about the Swlug mailing list