From b2e0b8613c30fffffd7ecfcd8019703c4b5eedde Mon Sep 17 00:00:00 2001 From: mt3571 Date: Mon, 30 Nov 2020 10:37:20 +0000 Subject: [PATCH 1/4] First pass at making a ManchesterEncoding and Decoding --- src/core/operations/ManchesterDecode.mjs | 60 ++++++++++++++++++++++++ src/core/operations/ManchesterEncode.mjs | 55 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/core/operations/ManchesterDecode.mjs create mode 100644 src/core/operations/ManchesterEncode.mjs diff --git a/src/core/operations/ManchesterDecode.mjs b/src/core/operations/ManchesterDecode.mjs new file mode 100644 index 00000000..9f167562 --- /dev/null +++ b/src/core/operations/ManchesterDecode.mjs @@ -0,0 +1,60 @@ +/** + * @author mt3571 [mt3571@protonmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Manchester decoding operation + */ +class ManchesterDecode extends Operation { + + /** + * ManchesterDecode constructor + */ + constructor() { + super(); + + this.name = "Manchester Decode"; + this.module = "Encodings"; + this.description = ""; + this.infoURL = ""; + this.inputType = "binaryArray"; + this.outputType = "binaryArray"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const result = []; + + if (input.length % 2 != 0){ + throw new OperationError(`Length of an input should be a multiple of 2, the input is ${input.length} long.`); + } + + for (let i = 0; i < input.length; i +=2){ + const bit1 = input[i]; + const bit2 = input[i+1]; + + if (bit1 == 1 && bit2 == 0){ + result.push(0); + } else if (bit1 == 0 && bit2 == 1){ + result.push(1); + } else { + throw new OperationError(`Invalid input.`); + } + + } + + return result; + } + +} + +export default ManchesterDecode; diff --git a/src/core/operations/ManchesterEncode.mjs b/src/core/operations/ManchesterEncode.mjs new file mode 100644 index 00000000..406b235e --- /dev/null +++ b/src/core/operations/ManchesterEncode.mjs @@ -0,0 +1,55 @@ +/** + * @author mt3571 [mt3571@protonmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Manchester encoding operation + */ +class ManchesterEncode extends Operation { + + /** + * ManchesterEncode constructor + */ + constructor() { + super(); + + this.name = "Manchester Encode"; + this.module = "Encodings"; + this.description = ""; + this.infoURL = ""; + this.inputType = "binaryArray"; + this.outputType = "binaryArray"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const result = []; + + for (let i = 0; i < input.length; i ++){ + const bit = input[i]; + + if (bit == 0){ + result.push(1); + result.push(0); + } else { + result.push(0); + result.push(1); + } + + } + + return result; + } + +} + +export default ManchesterEncode; From ef177a7ba072f4f70eeee964ef18a67b5538f226 Mon Sep 17 00:00:00 2001 From: mt3571 Date: Mon, 30 Nov 2020 11:00:51 +0000 Subject: [PATCH 2/4] Tidied up the Manchester files --- src/core/operations/ManchesterDecode.mjs | 19 ++++++++++--------- src/core/operations/ManchesterEncode.mjs | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/core/operations/ManchesterDecode.mjs b/src/core/operations/ManchesterDecode.mjs index 9f167562..bfde046a 100644 --- a/src/core/operations/ManchesterDecode.mjs +++ b/src/core/operations/ManchesterDecode.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * Manchester decoding operation @@ -19,10 +20,10 @@ class ManchesterDecode extends Operation { this.name = "Manchester Decode"; this.module = "Encodings"; - this.description = ""; - this.infoURL = ""; - this.inputType = "binaryArray"; - this.outputType = "binaryArray"; + this.description = "Decodes data that has been encoded using the Manchester Encoding (also known as phase encoding). A 01 is converted to 1 and a 10 is converted to 0.

As every bit is encoded into two bits when using this encoding, inputs must be a multiple of 2 long."; + this.infoURL = "https://en.wikipedia.org/wiki/Manchester_code"; + this.inputType = "string"; + this.outputType = "string"; this.args = []; } @@ -32,7 +33,7 @@ class ManchesterDecode extends Operation { * @returns {string} */ run(input, args) { - const result = []; + const decoding = []; if (input.length % 2 != 0){ throw new OperationError(`Length of an input should be a multiple of 2, the input is ${input.length} long.`); @@ -43,16 +44,16 @@ class ManchesterDecode extends Operation { const bit2 = input[i+1]; if (bit1 == 1 && bit2 == 0){ - result.push(0); + decoding.push(0); } else if (bit1 == 0 && bit2 == 1){ - result.push(1); + decoding.push(1); } else { throw new OperationError(`Invalid input.`); } } - - return result; + const output = decoding.join(""); + return output; } } diff --git a/src/core/operations/ManchesterEncode.mjs b/src/core/operations/ManchesterEncode.mjs index 406b235e..55dc62fe 100644 --- a/src/core/operations/ManchesterEncode.mjs +++ b/src/core/operations/ManchesterEncode.mjs @@ -19,10 +19,10 @@ class ManchesterEncode extends Operation { this.name = "Manchester Encode"; this.module = "Encodings"; - this.description = ""; - this.infoURL = ""; - this.inputType = "binaryArray"; - this.outputType = "binaryArray"; + this.description = "Performs the Manchester encoding on the data (also known as phase encoding). A 1 is converted to 01 and a 0 is converted to 10. "; + this.infoURL = "https://en.wikipedia.org/wiki/Manchester_code"; + this.inputType = "string"; + this.outputType = "string"; this.args = []; } @@ -32,22 +32,22 @@ class ManchesterEncode extends Operation { * @returns {string} */ run(input, args) { - const result = []; + const encoding = []; for (let i = 0; i < input.length; i ++){ const bit = input[i]; if (bit == 0){ - result.push(1); - result.push(0); + encoding.push(1); + encoding.push(0); } else { - result.push(0); - result.push(1); + encoding.push(0); + encoding.push(1); } } - - return result; + const output = encoding.join(""); + return output; } } From 101bab2d15b1ed538318b3c49199ea0130e917c4 Mon Sep 17 00:00:00 2001 From: mt3571 Date: Mon, 30 Nov 2020 11:07:05 +0000 Subject: [PATCH 3/4] Added in another error check --- src/core/operations/ManchesterEncode.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/operations/ManchesterEncode.mjs b/src/core/operations/ManchesterEncode.mjs index 55dc62fe..765694f9 100644 --- a/src/core/operations/ManchesterEncode.mjs +++ b/src/core/operations/ManchesterEncode.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * Manchester encoding operation @@ -40,9 +41,11 @@ class ManchesterEncode extends Operation { if (bit == 0){ encoding.push(1); encoding.push(0); - } else { + } else if (bit == 1){ encoding.push(0); encoding.push(1); + } else { + throw new OperationError(`Invalid input character ${bit}. Input should be in binary.`); } } From 3340fa079e87bacc8b248b8ec41ec6187e792f63 Mon Sep 17 00:00:00 2001 From: mt3571 Date: Tue, 1 Dec 2020 14:09:59 +0000 Subject: [PATCH 4/4] Added in a check to allow Manchester encoded things to be detected by magic --- src/core/operations/ManchesterDecode.mjs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/core/operations/ManchesterDecode.mjs b/src/core/operations/ManchesterDecode.mjs index bfde046a..d71bfd53 100644 --- a/src/core/operations/ManchesterDecode.mjs +++ b/src/core/operations/ManchesterDecode.mjs @@ -25,6 +25,13 @@ class ManchesterDecode extends Operation { this.inputType = "string"; this.outputType = "string"; this.args = []; + this.checks = [ + { + pattern: "(01|10)*", + flags: "", + args: [] + } + ]; } /**