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("电子凸轮运动测试开始...");

        try {
            short devNum = 0;
            short[] axisNums = {0, 1};
            PHAND pDevHandle = new PHAND();
            
            // 打开设备
            short rtn = NMC_DevOpen(devNum, pDevHandle);
            ErrorHandler.checkError(rtn);
            System.out.println("打开设备 - 成功");

            // 打开两个轴
            HAND devHandle = pDevHandle.getHandle();
            HAND[] axisHandles = new HAND[2];
            for (short i = 0; i < 2; i++) {
                PHAND pAxisHandle = new PHAND();
                rtn = NMC_MtOpen(devHandle, axisNums[i], pAxisHandle);
                ErrorHandler.checkError(rtn);
                System.out.printf("打开轴%d - 成功\n", axisNums[i]);
                axisHandles[i] = pAxisHandle.getHandle();

                rtn = NMC_MtSetSvOn(axisHandles[i]);
                ErrorHandler.checkError(rtn);
                System.out.printf("轴%d伺服使能 - 成功\n", axisNums[i]);
            }

            // 设置轴0为JOG运动模式
            rtn = NMC_MtMoveJog(axisHandles[0], 1.0, 1.0, 50.0, (short)0, (short)1);
            ErrorHandler.checkError(rtn);
            System.out.println("设置轴0为JOG运动模式 - 成功");

            // 设置轴1为跟随模式
            rtn = NMC_MtSetPrfMode(axisHandles[1], MT_FOLLOW_PRF_MODE);
            ErrorHandler.checkError(rtn);
            System.out.println("设置轴1为跟随模式 - 成功");
            
            // 清空FIFO
            rtn = NMC_MtFollowClear(axisHandles[1], (short)0);
            ErrorHandler.checkError(rtn);
            System.out.println("清除FIFO - 成功");
            
            // 设置主轴
            rtn = NMC_MtFollowSetMaster(axisHandles[1], (short)0, PROFILE_FOLLOW_MASTER_AXIS_PRF);
            ErrorHandler.checkError(rtn);
            System.out.println("设置主轴为轴0 - 成功");
            
            // 添加凸轮数据点（修正NativeLong使用）
            NativeLong masterpos = new NativeLong(20000);
            double slavepos = 10000.0;
            
            // 第一段凸轮曲线
            rtn = NMC_MtFollowPushData(axisHandles[1], masterpos, slavepos, (short)0, (short)0);
            ErrorHandler.checkError(rtn);
            System.out.println("添加第一段凸轮曲线 - 成功");
            
            // 第二段凸轮曲线（修正+=操作）
            masterpos = new NativeLong(masterpos.longValue() + 20000);
            slavepos += 20000.0;
            rtn = NMC_MtFollowPushData(axisHandles[1], masterpos, slavepos, (short)0, (short)0);
            ErrorHandler.checkError(rtn);
            System.out.println("添加第二段凸轮曲线 - 成功");
            
            // 第三段凸轮曲线
            masterpos = new NativeLong(masterpos.longValue() + 20000);
            slavepos += 10000.0;
            rtn = NMC_MtFollowPushData(axisHandles[1], masterpos, slavepos, (short)0, (short)0);
            ErrorHandler.checkError(rtn);
            System.out.println("添加第三段凸轮曲线 - 成功");
            
            // 设置循环次数（修正参数类型）
            rtn = NMC_MtFollowSetLoopCount(axisHandles[1], new NativeLong(0));
            ErrorHandler.checkError(rtn);
            System.out.println("设置无限循环 - 成功");
            
            // 设置事件触发（修正参数类型）
            rtn = NMC_MtFollowSetEvent(axisHandles[1], (short)2, (short)1, new NativeLong(50000));
            ErrorHandler.checkError(rtn);
            System.out.println("设置事件触发 - 成功");
            
            // 启动跟随运动
            short[] syncAxArray = new short[0];
            rtn = NMC_MtFollowStart(axisHandles[1], (short)0, syncAxArray);
            ErrorHandler.checkError(rtn);
            System.out.println("启动跟随运动 - 成功");
            
            // 等待2秒
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println("等待被中断");
            }
            
        } catch (Exception e) {
            System.err.println("测试过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            System.out.println("\n释放资源...");
        }
        
        System.out.println("电子凸轮运动测试完成");
    }
}