Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let message = "testing";
- let charList = "abcdefghijklmnopqrstuvwxyz";
- const morseMap: { [name: string]: string } = {
- 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: "__.."
- }
- let morseMapInverse: { [name: string]: string } = {};
- charList.split("").forEach(function (c, i) {
- morseMapInverse[morseMap[c]] = c;
- });
- morseMapInverse["-"] = " ";
- let dot = images.createImage(`
- . . . . .
- . . # . .
- . # # # .
- . . # . .
- . . . . .
- `);
- let dash = images.createImage(`
- . . . . .
- . . . . .
- # # # # #
- . . . . .
- . . . . .
- `);
- let clear = images.createImage(`
- . . . . .
- . . . . .
- . . . . .
- . . . . .
- . . . . .
- `);
- let timings: number[] = [];
- control.onEvent(1, 1, function () {
- timings.push(input.runningTime());
- led.toggle(0, 0);
- });
- control.onEvent(1, 2, function () {
- timings.push(input.runningTime());
- led.toggle(0, 0);
- });
- function decode(): string {
- let morse = "";
- let t = 0;
- let on = false;
- timings.forEach(function (value: number, index: number) {
- if (t == 0) {
- t = value;
- } else {
- let deltaT = value - t;
- t = value;
- if (deltaT < 0) {
- led.toggleAll();
- }
- if (index % 2 == 1) {
- if (deltaT < 250) {
- morse = morse + ".";
- } else {
- morse = morse + "_";
- }
- } else {
- if (deltaT > 500) {
- morse = morse + "-";
- }
- }
- }
- on = !on;
- });
- let result = "";
- morse.split("-").forEach(function (value: string, index: number) {
- if (morseMapInverse[value] != undefined) {
- result = result + morseMapInverse[value];
- } else {
- result = result + "#";
- }
- })
- return result;
- }
- input.onButtonPressed(Button.B, function () {
- basic.showString(decode());
- timings = [];
- })
- basic.forever(function () {
- /*message.split("").forEach(function (char, index) {
- basic.showString(char);
- if (char == " ") {
- basic.pause(3000);
- } else {
- morseMap[char].split("").forEach(function (s, i) {
- if (s == ".") {
- dot.showImage(0);
- basic.pause(1000);
- clear.showImage(0);
- } else {
- dash.showImage(0);
- basic.pause(1000);
- clear.showImage(0);
- }
- });
- }
- });
- basic.pause(5000);
- */
- })
Add Comment
Please, Sign In to add comment