[Swlug] Brag time - Morse code sending on a Raspberry Pi Pico
Rhys Sage
rhys_sage at yahoo.com
Thu Apr 21 00:22:14 UTC 2022
Thanks, Ivan.
I did not know about the dictionary function. I was looking into clearing up the muddle by putting everything into either a two-dimensional array or a single array in the format "a.-" and have the search going for a letter's presence in each line of the array then extracting all but the letter from that row of the array to convert to pin pulses.
I did think also about linked lists. Truth be told I always avoided linked lists when I did C++ and Pascal because I didn't understand them. That was something that Dr Evans back in the late 80s in West Glam Institute of HE never really managed to get into my skull all that thoroughly.
I'm going to have to look up some of the stuff in the program you took the time to write for me. Quite a bit I've not encountered yet, in my teach-myself methods. I guess you spotted the bug in my original code? It would go out of range and send incorrect morse codes. My brag was a little premature!
With the dictionary, it's much easier to add the extra things like numbers, commas and periods.
The eventual aim with this code is for it to work with a radio transmitter, sending data back via open Morse Code. I might use a CB radio frequency for this since nobody uses CB any more. I'm thinking of sending data like: station identifier, temperature, humidity, altitude, location (from one or other of the satellite location constallations) and various pollution readings, possibly including radiaton readings.
The analogue to digital audio converter that I ordered has arrived. I have yet to implement that. It's hard finding time these days with my work day that starts at work at 5:45am and goes on til 4:45pm.
Rhys Sage
On Wednesday, 20 April 2022, 12:47:56 GMT-4, Ivan Wright <quidsup at gmail.com> wrote:
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
>
>
More information about the Swlug
mailing list