2023-07-29 10:34:04 +02:00
|
|
|
*write-morse-code.ps1*
|
|
|
|
================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-05-26 12:20:18 +02:00
|
|
|
This PowerShell script writes the given text in Morse code.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-07-29 10:15:44 +02:00
|
|
|
PS> ./write-morse-code.ps1 [[-text] <String>] [[-speed] <Int32>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-text <String>
|
|
|
|
Specifies the text to write
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-speed <Int32>
|
|
|
|
Specifies the speed of one time unit (100 ms per default)
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value 100
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-05-26 12:20:18 +02:00
|
|
|
PS> ./write-morse-code ABC
|
|
|
|
● ― ― ● ● ● ― ● ― ●
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2023-05-26 12:20:18 +02:00
|
|
|
Writes Morse code
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
2023-05-26 12:20:18 +02:00
|
|
|
This PowerShell script writes the given text in Morse code.
|
2022-11-17 20:02:26 +01:00
|
|
|
.PARAMETER text
|
|
|
|
Specifies the text to write
|
|
|
|
.PARAMETER speed
|
|
|
|
Specifies the speed of one time unit (100 ms per default)
|
|
|
|
.EXAMPLE
|
2023-05-26 12:20:18 +02:00
|
|
|
PS> ./write-morse-code ABC
|
|
|
|
● ― ― ● ● ● ― ● ― ●
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$text = "", [int]$speed = 100) # one time unit in milliseconds
|
|
|
|
|
|
|
|
function gap { param([int]$Length)
|
2023-05-26 12:20:18 +02:00
|
|
|
for ([int]$i = 0; $i -lt $Length; $i++) {
|
|
|
|
Write-Host " " -noNewline
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2023-05-26 12:20:18 +02:00
|
|
|
Start-Sleep -milliseconds ($Length * $speed)
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function dot {
|
2023-05-26 12:20:18 +02:00
|
|
|
Write-Host "●" -noNewline
|
|
|
|
Start-Sleep -milliseconds $speed # signal
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function dash {
|
2023-05-26 12:20:18 +02:00
|
|
|
Write-Host "―" -noNewline
|
|
|
|
Start-Sleep -milliseconds (3 * $speed) # signal
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function Char2MorseCode { param([string]$Char)
|
|
|
|
switch($Char) {
|
|
|
|
'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-05-26 12:20:18 +02: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 }
|
2022-11-17 20:02:26 +01:00
|
|
|
default { gap 7 } # medium gap (between words)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-05-26 12:20:18 +02:00
|
|
|
if ($text -eq "" ) { [string]$text = Read-Host "Enter text to write" }
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
[char[]]$ArrayOfChars = $text.ToUpper()
|
|
|
|
foreach($Char in $ArrayOfChars) {
|
|
|
|
Char2MorseCode $Char
|
|
|
|
}
|
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-07-29 10:34:04 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of write-morse-code.ps1 as of 07/29/2023 10:33:50)*
|