Skip to content

Instantly share code, notes, and snippets.

@lupillow
Created October 25, 2022 00:17
Show Gist options
  • Save lupillow/2d45db6dc4d5fc599f4ef5f4dae3f15d to your computer and use it in GitHub Desktop.
Save lupillow/2d45db6dc4d5fc599f4ef5f4dae3f15d to your computer and use it in GitHub Desktop.

Revisions

  1. lupillow created this gist Oct 25, 2022.
    7 changes: 7 additions & 0 deletions discretes_message.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    DISCRETES_MESSAGE
    -----------------


    A [Pen](https://codepen.io/nadiemedicejose/pen/GRdVOvg) by [Jose Estrada](https://codepen.io/nadiemedicejose) on [CodePen](https://codepen.io).

    [License](https://codepen.io/license/pen/GRdVOvg).
    88 changes: 88 additions & 0 deletions script.babel
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    const hex2bin = (hex) => parseInt(hex, 16).toString(2).padStart(8, "0");

    const swap = (value) => {
    let result = value
    .match(/.{1,2}/g)
    .reverse()
    .map((split) => hex2bin(split))
    .join("");

    result = result
    .match(/.{1,8}/g)
    .reverse()
    .join("");
    result = result
    .match(/.{1,1}/g)
    .reverse()
    .join("");

    return result;
    };

    const decodeMessage = (message) => {
    const DISCRETES_MESSAGE = message.slice(16, -8);
    console.debug(DISCRETES_MESSAGE);

    const MESSAGE_HEADER = DISCRETES_MESSAGE.slice(0, 8);
    const FUNCTIONAL_STATUS = DISCRETES_MESSAGE.slice(8, 16);
    const CREW_ALERTING = DISCRETES_MESSAGE.slice(16, 64);
    const PTT = DISCRETES_MESSAGE.slice(64, 80);
    const MIC_SELECTOR = DISCRETES_MESSAGE.slice(80, 104);
    const INPUT_DISCRETES = DISCRETES_MESSAGE.slice(104, 112);

    const MESSAGE = {
    MESSAGE_HEADER,
    FUNCTIONAL_STATUS,
    CREW_ALERTING,
    PTT,
    MIC_SELECTOR,
    INPUT_DISCRETES
    };

    const SELECTED_MIC = MESSAGE.MIC_SELECTOR.slice(0, 8);
    const MIC_ON_CAB = hex2bin(MESSAGE.MIC_SELECTOR.slice(8, 16)).slice(6, 8);

    const MIC_SELECTION = swap(SELECTED_MIC);

    return {
    MIC_ON_VHF_L: MIC_SELECTION.slice(5, 6),
    MIC_ON_VHF_C: MIC_SELECTION.slice(6, 7),
    MIC_ON_VHF_R: MIC_SELECTION.slice(7, 8),
    MIC_ON_HF_L: MIC_SELECTION.slice(8, 9),
    MIC_ON_HF_R: MIC_SELECTION.slice(9, 10),
    MIC_ON_SAT_1: MIC_SELECTION.slice(10, 11),
    MIC_ON_SAT_2: MIC_SELECTION.slice(11, 12),
    MIC_ON_FLT: MIC_SELECTION.slice(12, 13),
    MIC_ON_CAB: MIC_ON_CAB
    };
    };

    // Test Cases
    const tc1_discretes =
    "883e00629fc121c90000000003030303000000000000000000000000000000000000000000000001000000000000000500000020000000150000000100000000415f8535";

    const tc2_discretes =
    "b0d30062af3c21210000000003030303000000000000000000000000000000000000000000000001000000000000000500000040000000150000000300000000b61fe50d";

    const tc3_discretes =
    "da6d0062bf1af3290000000003030303000000000000000000000000000000000000000000000001000000040000000600002400000000160000000800000000df04b561";

    const tc4_discretes =
    "16f60062d6327ab90000000003030303000000200000000000000000000000000000000000000001000000100000000500000040000000150000001300000000ed8ae043";

    const tc5_discretes =
    "21bf0062da4fa031000000000303030300000000000000000000000000000000000000000000000100000080000000050000004000000015000000140000000072a3703c";

    const tc6_discretes =
    "3a610062e3b557790000000003030303000000010000000000000000000000000000000000000001000000000000000900000000000000150000001a000000000df2207c";

    const tc7_discretes =
    "9abd0021cd4aba790000000003030303000000000000000000000000000000000000000000000001000000000000000500000000000000150000000000000000225bf1e0";

    console.log("Test Case 1", decodeMessage(tc1_discretes));
    console.log("Test Case 2", decodeMessage(tc2_discretes));
    console.log("Test Case 3", decodeMessage(tc3_discretes));
    console.log("Test Case 4", decodeMessage(tc4_discretes));
    console.log("Test Case 5", decodeMessage(tc5_discretes));
    console.log("Test Case 6", decodeMessage(tc6_discretes));
    console.log("Test Case 7", decodeMessage(tc7_discretes));