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");
    }
    // 错误处理工具类
    private static class ErrorHandler {
        public static void checkError(short rtn) {
            if (rtn != 0) {
                throw new RuntimeException(String.format("操作失败，错误代码: %d", rtn));
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("PVT连续运动功能测试开始...");

        try {
            short devNum = 0;                       // 设备序号，从0开始
            short axisNum = 0;                      // 轴号从0开始
            PHAND pDevHandle = new PHAND();         // 设备句柄
            PHAND pAxisHandle = new PHAND();        // 轴句柄
            
            // 打开控制器
            short rtn = NMC_DevOpen(devNum, pDevHandle);
            ErrorHandler.checkError(rtn);
            System.out.println("打开设备 - 成功");
            HAND devHandle = pDevHandle.getHandle();

            // 打开轴,并获得轴句柄
            rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle);
            ErrorHandler.checkError(rtn);
            System.out.printf("打开轴%d - 成功\n", axisNum);

            // 打开轴使能
            HAND axisHandle = pAxisHandle.getHandle();
            rtn = NMC_MtSetSvOn(axisHandle);
            ErrorHandler.checkError(rtn);
            System.out.printf("轴%d伺服使能 - 成功\n", axisNum);

            // 1. 设置为PVT模式
            rtn = NMC_MtSetPrfMode(axisHandle,MT_PVT_PRF_MODE);
            ErrorHandler.checkError(rtn);
            System.out.println("设置PVT模式 - 成功");

            // 2. 设置PVT运动参数
            TPvtPara pvtPrm = new TPvtPara();
            pvtPrm.abruptDec = 1;   // 急停减速度
            pvtPrm.smoothDec = 2;   // 平滑减速度
            pvtPrm.dataMode = 1;    // 数据模式
            pvtPrm.loopCount = 0;   // 循环次数，0表示不循环
            
            rtn = NMC_MtPvtSetPara(axisHandle, pvtPrm);
            ErrorHandler.checkError(rtn);
            System.out.println("设置PVT运动参数 - 成功");

            // 3. 清除PVT缓冲区
            rtn = NMC_MtPvtBufClr(axisHandle);
            ErrorHandler.checkError(rtn);
            System.out.println("清除PVT缓冲区 - 成功");

            // 4. 准备PVT数据
            // 位置数组
            double[] pPosArray = {0, 19800, 21800};
            // 时间数组(根据速度计算)
            double[] pTimeArray = {0, 9900, 10900}; // 时间 = 位置/速度
            // 速度数组
            double[] pVelArray = {0, 2, 0};

            // 4.准备数据,点数较少，直接调用OEM_PvtContinuousData接口
            // short count = 3;
            // double pvtCPos[8] = {0,19800,21800};
            // double pvtCVel[8] = {0,2,0};
            // double pvtCPercent[8] = {60,60,0};
            // double pvtCVelMax[8] = {10,2,2};
            // double pvtCAcc[8] = {0.01,0.02,0.02};
            // double pvtCDec[8] = {0.01,0.02,0.02};
            // rtn = OEM_PvtContinuousData(g_hAx[ax],count,pvtCPos,pvtCVel,pvtCPercent,pvtCVelMax,pvtCAcc,pvtCDec);
            
            // 使用批量添加PVT数据接口
            rtn = NMC_MtPvtData(axisHandle, (short)3, pPosArray, pTimeArray, pVelArray);
            ErrorHandler.checkError(rtn);
            System.out.println("添加PVT数据 - 成功");

            // 5. 开始PVT运动
            rtn = NMC_MtPvtStartMtn(axisHandle, (short)0, null);
            ErrorHandler.checkError(rtn);
            System.out.println("开始PVT运动 - 成功");

            // 等待运动完成
            ShortByReference axisSts = new ShortByReference();
            while (true) {
                rtn = NMC_MtGetSts(axisHandle, axisSts);
                ErrorHandler.checkError(rtn);
                if ((axisSts.getValue() & BIT_AXIS_BUSY) == 0) {
                    break;  // 运动完成
                }
                Thread.sleep(100);  // 避免CPU占用过高
            }
            System.out.println("PVT运动完成");

        } catch (Exception e) {
            System.err.println("测试过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            System.out.println("\n释放资源...");
            // 实际应用中需要添加关闭轴和设备的代码
            // NMC_MtClose(pAxisHandle.getHandle());
            // NMC_DevClose(pDevHandle.getHandle());
        }
        
        System.out.println("PVT连续运动功能测试完成");
    }
}