Created
February 14, 2023 22:33
-
-
Save juliaogris/f94ee72e72f995b1fa5fdd8b1eff3929 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"machine" | |
"time" | |
"unicode" | |
) | |
const ( | |
unit = time.Millisecond * 200 | |
) | |
var dur = map[rune]time.Duration{ | |
'.': 1, | |
'-': 3, | |
} | |
// moreseCode is a the [A-Z] subset of the morse code characters, see | |
// https://en.wikipedia.org/wiki/Morse_code#Letters,_numbers,_punctuation,_prosigns_for_Morse_code_and_non-Latin_variants | |
var morseCodes = map[rune]string{ | |
'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': "--..", | |
} | |
func main() { | |
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput}) | |
input := "hello world" | |
for _, r := range input { | |
blinkMorseCode(r) | |
} | |
} | |
// blinkMorseCode translates a character in [a-zA-Z ] to Morse code following the | |
// International Morse code (https://en.wikipedia.org/wiki/Morse_code#Representation,_timing,_and_speeds): | |
// | |
// short mark, dot or dit ( ▄ ): "dit duration" is one time unit long | |
// long mark, dash or dah ( ▄▄▄ ): three time units long | |
// inter-element gap between the dits and dahs within a character: one dot duration or one unit long | |
// short gap (between letters): three time units long | |
// medium gap (between words): seven time units long | |
func blinkMorseCode(r rune) { | |
if unicode.IsSpace(r) { | |
time.Sleep(4 * unit) // We have already slept 3 units at the end of the last morseCode. | |
return | |
} | |
morseCode := morseCodes[unicode.ToUpper(r)] | |
for _, r := range morseCode { | |
machine.LED.High() | |
time.Sleep(dur[r] * unit) | |
machine.LED.Low() | |
time.Sleep(unit) | |
} | |
time.Sleep(2 * unit) // We have already slept 1 unit at the end of the last dot/dash. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment