SHOW:
|
|
- or go back to the newest paste.
| 1 | let message = "testing"; | |
| 2 | let charList = "abcdefghijklmnopqrstuvwxyz"; | |
| 3 | const morseMap: { [name: string]: string } = {
| |
| 4 | a: "._", | |
| 5 | b: "_...", | |
| 6 | c: "_._.", | |
| 7 | d: "_..", | |
| 8 | e: ".", | |
| 9 | f: ".._.", | |
| 10 | g: "__.", | |
| 11 | h: "....", | |
| 12 | i: "..", | |
| 13 | j: ".___", | |
| 14 | k: "_._", | |
| 15 | l: "._..", | |
| 16 | m: "__", | |
| 17 | n: "_.", | |
| 18 | o: "___", | |
| 19 | p: ".__.", | |
| 20 | q: "__._", | |
| 21 | r: "._.", | |
| 22 | s: "...", | |
| 23 | t: "_", | |
| 24 | u: ".._", | |
| 25 | v: "..._", | |
| 26 | w: ".__", | |
| 27 | x: "_.._", | |
| 28 | y: "_.__", | |
| 29 | z: "__.." | |
| 30 | } | |
| 31 | let morseMapInverse: { [name: string]: string } = {};
| |
| 32 | ||
| 33 | charList.split("").forEach(function (c, i) {
| |
| 34 | morseMapInverse[morseMap[c]] = c; | |
| 35 | }); | |
| 36 | ||
| 37 | morseMapInverse["-"] = " "; | |
| 38 | ||
| 39 | let dot = images.createImage(` | |
| 40 | . . . . . | |
| 41 | . . # . . | |
| 42 | . # # # . | |
| 43 | . . # . . | |
| 44 | . . . . . | |
| 45 | `); | |
| 46 | ||
| 47 | let dash = images.createImage(` | |
| 48 | . . . . . | |
| 49 | . . . . . | |
| 50 | # # # # # | |
| 51 | . . . . . | |
| 52 | . . . . . | |
| 53 | `); | |
| 54 | ||
| 55 | let clear = images.createImage(` | |
| 56 | . . . . . | |
| 57 | . . . . . | |
| 58 | . . . . . | |
| 59 | . . . . . | |
| 60 | . . . . . | |
| 61 | `); | |
| 62 | ||
| 63 | let timings: number[] = []; | |
| 64 | ||
| 65 | ||
| 66 | control.onEvent(1, 1, function () {
| |
| 67 | timings.push(input.runningTime()); | |
| 68 | led.toggle(0, 0); | |
| 69 | }); | |
| 70 | control.onEvent(1, 2, function () {
| |
| 71 | timings.push(input.runningTime()); | |
| 72 | led.toggle(0, 0); | |
| 73 | }); | |
| 74 | ||
| 75 | function decode(): string {
| |
| 76 | let morse = ""; | |
| 77 | let t = 0; | |
| 78 | let on = false; | |
| 79 | timings.forEach(function (value: number, index: number) {
| |
| 80 | if (t == 0) {
| |
| 81 | t = value; | |
| 82 | } else {
| |
| 83 | let deltaT = value - t; | |
| 84 | t = value; | |
| 85 | if (deltaT < 0) {
| |
| 86 | led.toggleAll(); | |
| 87 | } | |
| 88 | if (index % 2 == 1) {
| |
| 89 | if (deltaT < 250) {
| |
| 90 | morse = morse + "."; | |
| 91 | } else {
| |
| 92 | morse = morse + "_"; | |
| 93 | } | |
| 94 | } else {
| |
| 95 | if (deltaT > 500) {
| |
| 96 | morse = morse + "-"; | |
| 97 | } | |
| 98 | } | |
| 99 | } | |
| 100 | on = !on; | |
| 101 | }); | |
| 102 | let result = ""; | |
| 103 | morse.split("-").forEach(function (value: string, index: number) {
| |
| 104 | if (morseMapInverse[value] != undefined) {
| |
| 105 | result = result + morseMapInverse[value]; | |
| 106 | } else {
| |
| 107 | result = result + "#"; | |
| 108 | } | |
| 109 | }) | |
| 110 | return result; | |
| 111 | } | |
| 112 | ||
| 113 | input.onButtonPressed(Button.B, function () {
| |
| 114 | basic.showString(decode()); | |
| 115 | timings = []; | |
| 116 | }) | |
| 117 | basic.forever(function () {
| |
| 118 | ||
| 119 | /*message.split("").forEach(function (char, index) {
| |
| 120 | basic.showString(char); | |
| 121 | if (char == " ") {
| |
| 122 | basic.pause(3000); | |
| 123 | } else {
| |
| 124 | morseMap[char].split("").forEach(function (s, i) {
| |
| 125 | if (s == ".") {
| |
| 126 | dot.showImage(0); | |
| 127 | basic.pause(1000); | |
| 128 | clear.showImage(0); | |
| 129 | } else {
| |
| 130 | dash.showImage(0); | |
| 131 | basic.pause(1000); | |
| 132 | clear.showImage(0); | |
| 133 | } | |
| 134 | }); | |
| 135 | } | |
| 136 | }); | |
| 137 | basic.pause(5000); | |
| 138 | */ | |
| 139 | }) |