package com.example;

import static com.example.mtnlib.nmc_lib20.*;
import static com.example.mtnlib.nmc_lib20_ext.*;
import static com.example.mtnlib.nmc_lib20_oem.*;
import static com.example.mtnlib.nmc_lib20_err.*;
import com.example.mtnlib.nmc_lib20_com.*;
import com.example.mtnlib.nmc_lib20_ext.TAdvCaptureParam;
import com.sun.jna.ptr.*;
import com.sun.jna.NativeLong; 

public class Main {
    // 错误处理工具类
    private static class ErrorHandler {
        /**
         * 检查返回值并处理错误
         * @param rtn 函数返回值
         * @throws RuntimeException 如果返回值不是0(成功)
         */
        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;               // 设备序号，从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.println("打开轴 - 成功");
            
            HAND axisHandle = pAxisHandle.getHandle();
            
            // 打开轴使能(根据实际情况使用)
            rtn = NMC_MtSetSvOn(axisHandle);
            ErrorHandler.checkError(rtn);
            System.out.println("轴伺服使能 - 成功");
            
            // 设置高级捕获参数
            TAdvCaptureParam advCaptureParam = new TAdvCaptureParam();
            advCaptureParam.capPosIndex = 0;        // 捕获位置索引
            advCaptureParam.filter = 100;           // 滤波器设置
            advCaptureParam.trigIndex = 0;          // 触发索引
            advCaptureParam.trigSrc = CAPT_EX_SRC_GPI; // 触发源: GPI
            advCaptureParam.trigValue = 0;          // 触发值
            
            //设置高级捕获功能
            rtn = NMC_MtSetAdvCaptParam(devHandle, advCaptureParam, (short)0);
            ErrorHandler.checkError(rtn);
            System.out.println("设置高级捕获参数 - 成功");
            
            // 等待捕获完成
            System.out.println("等待捕获完成...");
            /************************* 此处省略轴的运动过程 *****************************/
            ShortByReference sts = new ShortByReference((short)0);
            NativeLong[] posArray = new NativeLong[2];
            posArray[0] = new NativeLong(0);        // 信号前沿触发位置
            posArray[1] = new NativeLong(0);        // 信号后沿触发位置

            while (true) {
                // 获取高级捕获位置
                rtn = NMC_MtGetAdvCaptPos(devHandle, sts, posArray, (short)0);
                ErrorHandler.checkError(rtn);
                
                // 检查状态是否完成 (0x3表示捕获完成)
                if ((sts.getValue() & 0x0003) != 0) {
                    System.out.println("捕获完成 - 前沿位置: " + posArray[0].longValue() +  ", 后沿位置: " + posArray[1].longValue());      //前后沿均已经捕获到
                    break;
                }
                
                // 短暂等待避免CPU占用过高
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    System.out.println("等待被中断");
                }
            }
            
        } catch (Exception e) {
            System.err.println("测试过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 确保资源被释放
            System.out.println("\n释放资源...");
        }
        
        System.out.println("高级捕获功能测试完成");
    }
}