From b2e0b8613c30fffffd7ecfcd8019703c4b5eedde Mon Sep 17 00:00:00 2001 From: mt3571 Date: Mon, 30 Nov 2020 10:37:20 +0000 Subject: [PATCH] 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;