import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static lib.mtn_lib20.*;
import static lib.mtn_lib20_com.*;
import static lib.mtn_lib20_ext.*;
import static lib.mtn_lib20_oem.*;
import static lib.mtn_lib20_err.*;
import com.sun.jna.*;
import com.sun.jna.ptr.*;

public class Main {
    static {
        String libPath = System.getProperty("user.dir") + "\\lib";
        System.setProperty("java.library.path", libPath);
        System.load(libPath + "\\msvcr100.dll");
        System.load(libPath + "\\msvcp100.dll");
        System.load(libPath + "\\nmc_lib20.dll");
    }
    /**
     * 检查函数调用结果，处理错误
     */
    public static void checkError(short result, String operation) {
        if (result != RTN_CMD_SUCCESS) {
            System.err.printf("操作 %s 时发生错误 错误代码: %d%n", operation, result);
        }
    }
    
    public static void main(String[] args) {
        short rtn = 0;// 指令返回值
        short devNum = 0;// 设备序号，从0开始
        short axisNum = 0;// 轴号，从0开始

        // 句柄
        HAND devHandle = new HAND();// 设备句柄
        HAND axisHandle = new HAND();// 轴句柄
        HAND crdHandle = new HAND();// 坐标系句柄
        PHAND pDevHandle = new PHAND();//设备句柄指针
        PHAND pAxisHandle = new PHAND();//轴句柄指针
        PHAND pCrdHandle = new PHAND();// 坐标系句柄指针

        
        // 1. 打开设备
        rtn = NMC_DevOpen(devNum, pDevHandle);
        checkError(rtn, "NMC_DevOpen");
        devHandle = pDevHandle.getHandle();

        // 2. 打开轴
        rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle);
        checkError(rtn, "NMC_MtOpen");
        axisHandle = pAxisHandle.getHandle();

        // 3. 轴使能
        rtn = NMC_MtSetSvOn(axisHandle);
        checkError(rtn, "NMC_MtSetSvOn");

    /**
    * 正式测试部分
    */
        // a. 定义起始位置
        NativeLong[] startPos = new NativeLong[]{
            new NativeLong(0), 
            new NativeLong(0)
        };

        // b. **关键步骤：确保功能已关闭**
        // 在设置任何参数前，先关闭线性位置比较，避免“激活中”的错误
        System.out.println("=== 确保功能已关闭 ===");
        short ch = 0; // 通道号
        rtn = NMC_LinearCompOnOff(devHandle, (short)0, ch); // 0: 关闭
        checkError(rtn, "NMC_LinearCompOnOff (Ensure Off)");
        if (rtn == RTN_CMD_SUCCESS) {
            System.out.println("✅ 已确保线性位置比较功能处于关闭状态");
        }

        // c. 设置线性位置比较参数
        // 参数：一维，轴掩码0x3（第一个轴和第二轴），位置源为规划位置，**按等间距输出 (cmpMode=1)**，脉宽为0.0009秒
        System.out.println("\n=== 设置线性位置比较参数 ===");
        short dimens = 1;
        short axisMask = 0x3;
        short src = 1;
        short cmpMode = 1; // ✅ 关键：必须设置为1（等间距输出），否则 NMC_LinearCompInterval 会报错612
        double gateTime = 0.0009;

        rtn = NMC_LinearCompSetParam(devHandle, dimens, axisMask, src, cmpMode, gateTime, ch);
        checkError(rtn, "NMC_LinearCompSetParam");
        if (rtn == RTN_CMD_SUCCESS) {
            System.out.println("✅ NMC_LinearCompSetParam 执行成功");
        } else {
            System.err.println("❌ NMC_LinearCompSetParam 失败，后续操作无法进行。");
            return;
        }

        // d. 设置等间距输出数据
        // 间隔1000个脉冲，输出5个
        System.out.println("\n=== 设置等间距输出数据 ===");
        rtn = NMC_LinearCompInterval(devHandle, startPos, new NativeLong(1000), new NativeLong(5), ch);
        checkError(rtn, "NMC_LinearCompInterval");
        if (rtn == RTN_CMD_SUCCESS) {
            System.out.println("✅ NMC_LinearCompInterval 执行成功");
        }

        // e. 启动线性位置比较输出
        System.out.println("\n=== 启动线性位置比较 ===");
        rtn = NMC_LinearCompOnOff(devHandle, (short)1, ch); // 1: 打开
        checkError(rtn, "NMC_LinearCompOnOff (Start)");
        if (rtn == RTN_CMD_SUCCESS) {
            System.out.println("✅ NMC_LinearCompOnOff (启动) 执行成功");
        }

        // f. 执行运动，触发位置比较
        // 运动第一轴
        System.out.println("\n=== 执行运动并触发比较 ===");
        System.out.println("  开始执行 PTP 绝对运动...");
        rtn = NMC_MtMovePtpAbs(axisHandle, 1.0, 1.0, 0.0, 0.0, 5.0, (short)0, new NativeLong(100000L));
        checkError(rtn, "NMC_MtMovePtpAbs");
        if (rtn == RTN_CMD_SUCCESS) {
            System.out.println("✅ NMC_MtMovePtpAbs 指令已发送");
        }

        // g. 等待运动完成
        System.out.println("  等待运动完成...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("✅ 轴运动已完成");

        // h. 停止线性位置比较输出
        System.out.println("\n=== 停止线性位置比较 ===");
        rtn = NMC_LinearCompOnOff(devHandle, (short)0, ch); // 0: 关闭
        checkError(rtn, "NMC_LinearCompOnOff (Stop)");
        if (rtn == RTN_CMD_SUCCESS) {
            System.out.println("✅ NMC_LinearCompOnOff (停止) 执行成功");
        }


    /**
     * 结束测试，关闭设备
    */
        rtn = NMC_MtClose(pAxisHandle);
        checkError(rtn, "NMC_MtClose");
        rtn = NMC_DevClose(pDevHandle);
        checkError(rtn, "NMC_DevClose");

    }
}