Skip to content

Commit

Permalink
added binary decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
katsaii committed Feb 10, 2024
1 parent 291e65c commit 4e8bd83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions morse-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<hr>
<button onclick="encodeMorse()">encode morse code</button>
<button onclick="decodeMorse()">decode morse code</button>
<button onclick="decodeMorseBinary()">decode binary signal</button>
<hr>
<textarea id="dest" cols="80" rows="5"></textarea>
<div id="dest-signal"></div>
Expand Down
18 changes: 14 additions & 4 deletions morse-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ const getCharSets = () => {
}

function writeMorseCodeData(morse, decoder) {
var binMorse = Sob.morseImplode(morse);
var morseBin = Sob.morseImplode(morse);
let sb = new Sob.HTMLBuilder;
sb.writeVoidTag("br");
sb.writeResultRichText("morse code binary signal", binMorse);
sb.writeResultRichText("morse code binary signal", morseBin);
sb.writeTag(["pre style=\"overflow-x : scroll\"", "code"], sb => {
let rulerN = 0;
let ruler = "";
for (let i = binMorse.length - 1; i >= 0; i -= 1) {
for (let i = morseBin.length - 1; i >= 0; i -= 1) {
ruler += rulerN;
rulerN += 1;
}
sb.write(ruler);
sb.writeVoidTag("br");
sb.writeVoidTag("br");
sb.write(binMorse.replaceAll("1", "▓").replaceAll("0", "˽"));
sb.write(morseBin.replaceAll("1", "▓").replaceAll("0", "˽"));
sb.writeVoidTag("br");
sb.write(morse.map(morseWord => morseWord.map(morseLetter => {
const letter = decoder.get(morseLetter) ?? "?";
Expand Down Expand Up @@ -85,4 +85,14 @@ function decodeMorse() {
console.log(text);
setTextContent(text);
writeMorseCodeData(morse, decoder);
}

function decodeMorseBinary() {
const morseBin = getTextContent();
const decoder = Sob.createMorseDecoder(...getCharSets());
const morse = Sob.morseExplode(morseBin);
const text = Sob.morseDecode(decoder, morse);
console.log(text);
setTextContent(text);
writeMorseCodeData(morse, decoder);
}

0 comments on commit 4e8bd83

Please sign in to comment.