Last active
January 17, 2022 22:05
-
-
Save gongcastro/938ec4d8b4caaf049fc5b0abcf3a9862 to your computer and use it in GitHub Desktop.
Code for translating character strings to ligh blinks implemented in RaspberryPi Pico. Adapted from @printnplay's code: https://www.instructables.com/Implementation-of-Morse-Code-Raspberry-Pi-Pico/.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, PWM | |
from time import sleep | |
# Create a dictionary of Morse Code. s is for Short (or dots), l is for Long (or dashes) | |
MorseCodes = { | |
' ': '', | |
'a': 'sl', | |
'b': 'lsss', | |
'c': 'lsls', | |
'd': 'lss', | |
'e': 's', | |
'f': 'ssls', | |
'g': 'lls', | |
'h': 'ssss', | |
'i': 'ss', | |
'j': 'slll', | |
'k': 'lsl', | |
'l': 'slss', | |
'm': 'll', | |
'n': 'ls', | |
'o': 'lll', | |
'p': 'slls', | |
'q': 'llsl', | |
'r': 'sls', | |
's': 'sss', | |
't': 'l', | |
'u': 'ssl', | |
'v': 'sssl', | |
'w': 'sll', | |
'x': 'lssl', | |
'y': 'lsll', | |
'z': 'llss', | |
'1': 'sllll', | |
'2': 'sslll', | |
'3': 'sssll', | |
'4': 'ssssl', | |
'5': 'sssss', | |
'6': 'lssss', | |
'7': 'llsss', | |
'8': 'lllss', | |
'9': 'lllls', | |
'0': 'lllll' | |
} | |
led = Pin(15, Pin.OUT) | |
fast = 0.1 | |
slow = 0.4 | |
led.low() | |
def letterlookup(stringvalue): | |
for k in MorseCodes: | |
if MorseCodes[k] == stringvalue: | |
return k | |
return " " | |
def blinkletter(letter): | |
if letter != "": | |
currentletter = MorseCodes[letter] | |
if letter == " ": | |
sleep(0.6) | |
return | |
print(letter + " : " + currentletter) | |
for c in currentletter: | |
if (c == 'l'): | |
blinkspeed = slow | |
if (c =='s'): | |
blinkspeed = fast | |
led.high() | |
sleep(blinkspeed) | |
led.low() | |
sleep(blinkspeed) | |
sleep(0.6) | |
def blinkstring(string): | |
for c in string: | |
blinkletter(c) | |
sleep(0.5) | |
return "" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment