How to resolve the algorithm Morse code step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Morse code step by step in the XPL0 programming language

Table of Contents

Problem Statement

Morse code is one of the simplest and most versatile methods of telecommunication in existence. It has been in use for more than 175 years — longer than any other electronic encoding system.

Send a string as audible Morse code to an audio device   (e.g., the PC speaker).

As the standard Morse code does not contain all possible characters, you may either ignore unknown characters in the file, or indicate them somehow   (e.g. with a different pitch).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Morse code step by step in the XPL0 programming language

Source code in the xpl0 programming language

code ChOut=8, CrLf=9, Sound=39;
string 0;               \use zero-terminated strings

proc Morse(Msg);        \Output Msg string as audible International Morse code
char Msg;
char C, D;
int  Code, Vol;
[Code:= ["    ",                                        \space
        ".-.-..",".-..-.", "    ", "...-..-","    ",    \!"#$%
        "----.", ".----.", "-.--.-", "---..",  "    ",  \&'()*
        ".-.-.", "--..--", "-....-", ".-.-.-", "-..-.", \+,-./
        "-----", ".----",  "..---",  "...--",  "....-", \01234
        ".....", "-....",  "--...",  "---..",  "----.", \56789
        "---...","-.-.-.", "    ",   "-...-",  "    ",  \:;<=>
        "..--..",".--.-.",                              \?@
        ".-",    "-...",   "-.-.",   "-..",    ".",     \ABCDE
        "..-.",  "--.",    "....",   "..",     ".---",  \FGHIJ
        "-.-",   ".-..",   "--",     "-.",     "---",   \KLMNO
        ".--.",  "--.-",   ".-.",    "...",    "-",     \PQRST
        "..-",   "...-",   ".--",    "-..-",   "-.--",  \UVWXY
        "--.."];                                        \Z
Sound(0, 1, 1);                                 \sync, for consistent durations
repeat  C:= Msg(0);  Msg:= Msg+1;               \get character from message
        ChOut(0, C);                            \show the character
        if C>=^a & C<=^z then C:= C-$20;        \convert letters to uppercase
        Vol:= 1;                                \assume volume is on (not space)
        if C>=$21 & C<=^Z then D:= Code(C-$20)  \convert characters to code
        else [D:= Code(0);  Vol:= 0];           \space (or unsupported char)
        repeat  Sound(Vol, if D(0)=^- then 3 else 1, 1190000/600);      \600 Hz
                Sound(0, 1, 1);                 \gap between . and -
                ChOut(0, D(0));                 \show dots and dashes
                D:= D+1;                        \next dot or dash for character
        until   D(0) = 0;                       \string terminator
        Sound(0, 2, 1);                         \gap between letters (2+1=3)
        ChOut(0, ^ );                           \show gap
until   Msg(0) = 0;                             \string terminator
];

[Morse("SOS SOS SOS ");                         \something easy to recognize
CrLf(0);
Morse("Hello, world!");
]

  

You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the Ring programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the CLU programming language
You may also check:How to resolve the algorithm Additive primes step by step in the R programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Nim programming language