PowerShell/Scripts/write-morse-code.ps1

86 lines
2.9 KiB
PowerShell
Raw Normal View History

2020-12-21 18:03:36 +01:00
#!/snap/bin/powershell
# Syntax: ./write-morse-code.ps1 [<text>]
# Description: writes the given text in Morse code
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
param([String]$Text)
2020-12-22 09:24:30 +01:00
$OneTimeUnit = 100 # milliseconds
function gap() { param([Int]$Length)
for ($i = 1; $i -lt $Length; $i++) {
write-host " " -nonewline
}
start-sleep -milliseconds ($Length * $OneTimeUnit)
}
2020-12-21 18:03:36 +01:00
function dot() {
write-host "." -nonewline
2020-12-22 09:24:30 +01:00
start-sleep -milliseconds $OneTimeUnit # signal
2020-12-21 18:03:36 +01:00
}
function dash() {
2020-12-22 09:24:30 +01:00
write-host "_" -nonewline
start-sleep -milliseconds (3 * $OneTimeUnit) # signal
2020-12-21 18:03:36 +01:00
}
function Char2MorseCode() { param([String]$Char)
switch($Char) {
2020-12-22 09:24:30 +01:00
'A' { dot; gap 1; dash; gap 3 }
'B' { dash; gap 1; dot; gap 1; dot; gap 1; dot; gap 3 }
'C' { dash; gap 1; dot; gap 1; dash; gap 1; dot; gap 3 }
'D' { dash; gap 1; dot; gap 1; dot; gap 3 }
'E' { dot; gap 3 }
'F' { dot; gap 1; dot; gap 1; dash; gap 1; dot; gap 3 }
'G' { dash; gap 1; dash; gap 1; dot; gap 3 }
'H' { dot; gap 1; dot; gap 1; dot; gap 1; dot; gap 3 }
'I' { dot; gap 1; dot; gap 3 }
'J' { dot; gap 1; dash; gap 1; dash; gap 1; dash; gap 3 }
'K' { dash; gap 1; dot; gap 1; dash; gap 3 }
'L' { dot; gap 1; dash; gap 1; dot; gap 1; dot; gap 3 }
'M' { dash; gap 1; dash; gap 3 }
'N' { dash; gap 1; dot; gap 3 }
'O' { dash; gap 1; dash; gap 1; dash; gap 3 }
'P' { dot; gap 1; dash; gap 1; dash; gap 1; dot; gap 3 }
'Q' { dash; gap 1; dash; gap 1; dot; gap 1; dash; gap 3 }
'R' { dot; gap 1; dash; gap 1; dot; gap 3 }
'S' { dot; gap 1; dot; gap 1; dot; gap 3 }
'T' { dash; gap 3 }
'U' { dot; gap 1; dot; gap 1; dash; gap 3 }
'V' { dot; gap 1; dot; gap 1; dot; gap 1; dash; gap 3 }
'W' { dot; gap 1; dash; gap 1; dash; gap 3 }
'X' { dash; gap 1; dot; gap 1; dot; gap 1; dash; gap 3 }
'Y' { dash; gap 1; dot; gap 1; dash; gap 1; dash; gap 3 }
'Z' { dash; gap 1; dash; gap 1; dot; gap 1; dot; gap 3 }
'1' { dot; gap 1; dash; gap 1; dash; gap 1; dash; gap 1; dash; gap 3 }
'2' { dot; gap 1; dot; gap 1; dash; gap 1; dash; gap 1; dash; gap 3 }
'3' { dot; gap 1; dot; gap 1; dot; gap 1; dash; gap 1; dash; gap 3 }
'4' { dot; gap 1; dot; gap 1; dot; gap 1; dot; gap 1; dash; gap 3 }
'5' { dot; gap 1; dot; gap 1; dot; gap 1; dot; gap 1; dot; gap 3 }
'6' { dash; gap 1; dot; gap 1; dot; gap 1; dot; gap 1; dot; gap 3 }
'7' { dash; gap 1; dash; gap 1; dot; gap 1; dot; gap 1; dot; gap 3 }
'8' { dash; gap 1; dash; gap 1; dash; gap 1; dot; gap 1; dot; gap 3 }
'9' { dash; gap 1; dash; gap 1; dash; gap 1; dash; gap 1; dot; gap 3 }
'0' { dash; gap 1; dash; gap 1; dash; gap 1; dash; gap 1; dash; gap 3 }
default { gap 7 } # medium gap (between words)
2020-12-21 18:03:36 +01:00
}
}
try {
if ($Text -eq "" ) {
[String]$Text = read-host "Enter text to write"
}
[char[]]$ArrayOfChars = $Text.ToUpper()
foreach($Char in $ArrayOfChars) {
Char2MorseCode $Char
}
write-host ""
2020-12-22 09:24:30 +01:00
write-host ""
2020-12-21 18:03:36 +01:00
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}