PowerShell/Scripts/write-morse-code.ps1

101 lines
3.9 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2023-01-06 20:14:05 +01:00
Writes Morse code
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2023-01-06 20:14:05 +01:00
This PowerShell script writes the given text in Morse code.
2021-10-16 16:50:10 +02:00
.PARAMETER text
Specifies the text to write
.PARAMETER speed
Specifies the speed of one time unit (100 ms per default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-01-06 20:14:05 +01:00
PS> ./write-morse-code ABC
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-09-06 21:42:04 +02:00
.NOTES
Author: Markus Fleschutz | License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-12-21 18:03:36 +01:00
2021-09-25 19:43:22 +02:00
param([string]$text = "", [int]$speed = 100) # one time unit in milliseconds
2020-12-21 18:03:36 +01:00
2021-01-27 15:12:17 +01:00
function gap { param([int]$Length)
2023-01-06 20:14:05 +01:00
for ([int]$i = 0; $i -lt $Length; $i++) {
Write-Host " " -noNewline
2020-12-22 09:24:30 +01:00
}
2023-01-06 20:14:05 +01:00
Start-Sleep -milliseconds ($Length * $speed)
2020-12-22 09:24:30 +01:00
}
2020-12-21 18:03:36 +01:00
2021-01-27 15:12:17 +01:00
function dot {
2023-01-06 20:14:05 +01:00
Write-Host "" -noNewline
Start-Sleep -milliseconds $speed # signal
2020-12-21 18:03:36 +01:00
}
2021-01-27 15:12:17 +01:00
function dash {
2023-01-06 20:14:05 +01:00
Write-Host "" -noNewline
Start-Sleep -milliseconds (3 * $speed) # signal
2020-12-21 18:03:36 +01:00
}
2021-01-27 15:12:17 +01:00
function Char2MorseCode { param([string]$Char)
2020-12-21 18:03:36 +01:00
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 }
2023-01-06 20:31:34 +01:00
'?' { dot; gap 1; dot; gap 1; dash; gap 1; dash; gap 1; dot; gap 1; dot; gap 3 }
'!' { dash; gap 1; dot; gap 1; dash; gap 1; dot; gap 1; dash; gap 1; dash; gap 3 }
'.' { dot; gap 1; dash; gap 1; dot; gap 1; dash; gap 1; dot; gap 1; dash; gap 3 }
',' { dash; gap 1; dash; gap 1; dot; gap 1; dot; gap 1; dash; gap 1; dash; gap 3 }
"'" { dash; gap 1; dot; gap 1; dash; gap 1; dot; gap 1; dash; gap 1; dot; gap 3 }
':' { dash; gap 1; dash; gap 1; dash; gap 1; dot; gap 1; dot; gap 1; dot; gap 3 }
'+' { dot; gap 1; dash; gap 1; dot; gap 1; dash; gap 1; dot; gap 3 }
'-' { dash; gap 1; dot; gap 1; dot; gap 1; dot; gap 1; dot; gap 1; dash; gap 3 }
'*' { dash; gap 1; dot; gap 1; dot; gap 1; dash; gap 3 }
'÷' { dash; gap 1; dot; gap 1; dot; gap 1; dash; gap 1; dot; gap 3 }
2020-12-22 09:24:30 +01:00
default { gap 7 } # medium gap (between words)
2020-12-21 18:03:36 +01:00
}
}
try {
2023-01-06 20:14:05 +01:00
if ($text -eq "" ) { [string]$text = Read-Host "Enter text to write" }
2021-09-16 20:19:10 +02:00
2021-09-25 19:43:22 +02:00
[char[]]$ArrayOfChars = $text.ToUpper()
2020-12-21 18:03:36 +01:00
foreach($Char in $ArrayOfChars) {
Char2MorseCode $Char
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2020-12-21 18:03:36 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-21 18:03:36 +01:00
exit 1
2023-01-06 20:14:05 +01:00
}