File size: 7,634 Bytes
2cc62f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html>
<head>
    <title>Morse Code Decoder</title>
    <style>
        body {
            font-family: sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }

        .container {
            width: 80%;
            max-width: 600px;
        }

        textarea, input {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background-color: #3e8e41;
        }

        #led {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: gray;
            margin-top: 10px;
        }

        .alphabet-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            margin-top: 20px;
        }

        .alphabet-letter {
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 1px solid #ccc;
            margin: 5px;
            cursor: pointer;
            user-select: none; /* Prevent text selection on click */
        }

        .alphabet-letter:hover {
            background-color: #f0f0f0;
        }
        #status {
            margin-top: 10px;
            font-style: italic;
        }

    </style>
</head>
<body>

    <div class="container">
        <h1>Morse Code Decoder</h1>

        <label for="morseInput">Morse Code Input:</label>
        <textarea id="morseInput" rows="4"></textarea>

        <button onclick="decodeMorse()">Decode</button>

        <label for="decodedText">Decoded Text:</label>
        <textarea id="decodedText" rows="4" readonly></textarea>

        <h2>LED Output</h2>
        <div id="led"></div>

        <h2>Alphabet Input</h2>
        <div class="alphabet-container" id="alphabetContainer">
        </div>

        <p id="status">Ready</p>
    </div>

    <script>
        const MORSE_CODE_DICT = {
            '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': '--..', '1': '.----', '2': '..---', '3': '...--',
            '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
            '9': '----.', '0': '-----', ', ': '--..--', '.': '.-.-.-', '?': '..--..',
            '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-'
        };

        const dotDuration = 200; // milliseconds
        const dashDuration = dotDuration * 3;
        const spaceDuration = dotDuration * 7;
        const letterSpaceDuration = dotDuration * 3;

        const morseInput = document.getElementById("morseInput");
        const decodedText = document.getElementById("decodedText");
        const led = document.getElementById("led");
        const alphabetContainer = document.getElementById("alphabetContainer");
        const status = document.getElementById("status");

        // Generate alphabet buttons
        const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for (let i = 0; i < alphabet.length; i++) {
            const letter = alphabet[i];
            const letterDiv = document.createElement("div");
            letterDiv.classList.add("alphabet-letter");
            letterDiv.textContent = letter;
            letterDiv.addEventListener("click", function() {
                if (MORSE_CODE_DICT[letter]) {
                   morseInput.value += MORSE_CODE_DICT[letter] + " "; // Add space after each letter
                } else {
                   alert("Morse Code not found for this Character")
                }
            });
            alphabetContainer.appendChild(letterDiv);
        }

        function decodeMorse() {
            const morseCode = morseInput.value.toUpperCase().trim();
            status.textContent = "Decoding..."; // Start decoding

            if (!morseCode) {
                status.textContent = "Please enter Morse code.";
                return;
            }

            try {
                const decoded = morseToText(morseCode);
                decodedText.value = decoded;
                status.textContent = "Decoding Complete" // Success!
                displayLED(morseCode);
            } catch (error) {
                decodedText.value = error; // Display Error
                status.textContent = `Error: ${error}`;
                turnOffLED(); // Turn off led
            }

        }

        function morseToText(morseCode) {
           morseCode = morseCode.replace("/", " / "); // Handle word separators
            const words = morseCode.split(" / ");
            let text = "";
            for (const word of words) {
                const letters = word.split(" ");
                for (const letter of letters) {
                    let found = false;
                    for (const key in MORSE_CODE_DICT) {
                        if (MORSE_CODE_DICT[key] === letter) {
                            text += key;
                            found = true;
                            break;
                        }
                    }
                    if (!found && letter) {
                        throw `Invalid Morse code: ${letter}`; // Throw exception for non found letters
                    }
                }
                text += " ";
            }
            return text.trim();
        }

        function displayLED(morseCode) {
            let i = 0;
            function animate() {
                if (i < morseCode.length) {
                    const symbol = morseCode[i];
                    if (symbol === '.') {
                        turnOnLED();
                      setTimeout(() => {
                            turnOffLED();
                            setTimeout(() => {
                                i++;
                                animate();
                            }, dotDuration);
                        }, dotDuration);
                    } else if (symbol === '-') {
                        turnOnLED();
                        setTimeout(() => {
                            turnOffLED();
                            setTimeout(() => {
                                i++;
                                animate();
                            }, dotDuration);
                        }, dashDuration);
                    } else if (symbol === ' ') {
                        setTimeout(() => {
                            i++;
                            animate();
                        }, letterSpaceDuration);
                    }
                    else if (symbol === '/') {
                        setTimeout(() => {
                            i++;
                            animate();
                        }, spaceDuration);
                    }

                } else {
                    turnOffLED();
                }
            }
            animate();
        }

        function turnOnLED() {
            led.style.backgroundColor = "yellow";
        }

        function turnOffLED() {
            led.style.backgroundColor = "gray";
        }
    </script>

</body>
</html>