"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.homingMethodParametersMap = exports.statuswordToHomingStatus = exports.createHomingProcedureStatus = exports.homingProcedureStatusMessages = void 0;
/**
 * The homing procedure status is determined by the values of bits 10, 12, and 13 in Statusword 0x6041:
 * - Bit 10: Target reached
 * - Bit 12: Homing attained
 * - Bit 13: Homing error
 *
 * For more details, refer to the homing modes documentation:
 * {@link https://doc.synapticon.com/circulo_safe_motion/sw5.1/motion_control/operation_modes/homing_modes.html}
 */
exports.homingProcedureStatusMessages = [
    'Homing is performed',
    'Homing is interrupted or not started',
    'Homing confirmed, but target not yet reached',
    'Homing completed',
    'Error detected, motor still turning',
    'Error during homing, motor at standstill',
];
/**
 * Generates a homing procedure status object based on the provided operation status value.
 * The status is evaluated to determine the request status, which can be 'running', 'succeeded', or 'failed'.
 *
 * During a meeting with Saša R., we concluded the following regarding the status codes:
 * - 000: running (Homing is in progress) - The Halt bit is 0, the Homing operation start bit is 1,
 *   and the device is in the Operation Enabled state and Homing mode.
 * - 001: failed (Homing interrupted or not started) - Interruption occurs due to Quick Stop, for example.
 *   Not started refers to the Homing mode being set, but the Homing operation start bit remains 0.
 * - 010: succeeded (Homing confirmed, target not yet reached) - This status occurs when the device restores
 *   the Home Position upon loading the configuration (0x2005:02).
 * - 011: succeeded (Homing completed).
 * - 100: failed (Error detected, motor still turning) - The device does not stop automatically; it must be stopped by the master.
 * - 101: failed (Error during homing, motor at a standstill).
 *
 * @param statusword - The statusword used to determine the homing procedure status.
 * @returns An object that represents the determined status of the homing procedure.
 */
function createHomingProcedureStatus(statusword) {
    let request;
    const status = statuswordToHomingStatus(statusword);
    if (status === 0) {
        request = 'running';
    }
    else if (status === 1) {
        request = 'failed';
    }
    else if (status === 2 || status === 3) {
        request = 'succeeded';
    }
    else {
        request = 'failed';
    }
    return {
        request,
        status,
        statusMessage: exports.homingProcedureStatusMessages[status],
        statusword,
    };
}
exports.createHomingProcedureStatus = createHomingProcedureStatus;
/**
 * Converts a statusword value to the corresponding homing status number.
 * The homing status is derived from specific bits of the statusword.
 *
 * @param statusword - The statusword from which to extract the homing status.
 * @returns The homing status number, calculated from bits 10, 12 and 13 of the statusword.
 */
function statuswordToHomingStatus(statusword) {
    const bit_10 = (statusword & 0b10000000000) >> 10;
    const bits_12_13 = (statusword & 0b11000000000000) >> 11;
    return bits_12_13 | bit_10;
}
exports.statuswordToHomingStatus = statuswordToHomingStatus;
/**
 * Keep up to date with options in ESI file for 0x6098:00 (Homing Method).
 */
exports.homingMethodParametersMap = {
    '1': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '2': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '3': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '4': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '5': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '6': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '7': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '8': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '9': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '10': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '11': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '12': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '13': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '14': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '17': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '18': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '19': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '20': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '21': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '22': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '23': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '24': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '25': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '26': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '27': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '28': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '29': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '30': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
    ],
    '33': [
        [0x6099, 2],
        [0x609a, 0],
    ],
    '34': [
        [0x6099, 2],
        [0x609a, 0],
    ],
    '35': [],
    '37': [],
    '-1': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
        [0x2005, 3],
    ],
    '-2': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
        [0x2005, 3],
    ],
    '-3': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
        [0x2005, 3],
        [0x2005, 4],
    ],
    '-4': [
        [0x6099, 1],
        [0x6099, 2],
        [0x609a, 0],
        [0x2005, 3],
        [0x2005, 4],
    ],
};
//# sourceMappingURL=homing.js.map