package com.example;

import static com.example.mtnlib.nmc_lib20.*;
import static com.example.mtnlib.nmc_lib20_err.*;
import static com.example.mtnlib.nmc_lib20_oem.*;
import static com.example.mtnlib.nmc_lib20_ext.*;
import static com.example.mtnlib.nmc_lib20_com.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import com.example.mtnlib.nmc_lib20_com.HAND;
import com.example.mtnlib.nmc_lib20_com.PHAND;
import com.sun.jna.*;
import com.sun.jna.ptr.*;

/**
 * Java 示例：使用 JNA 调用 NMC 库进行数据采集
 * 仿照 C++ "轴的专用IO设置" 测试用例
 */
public class Main {
    /**
     * 检查函数调用结果，处理错误
     */
    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");

    /**
    * 正式测试部分
    */

        // 定义要设置的电平（高电平触发）
        short HIGH_LEVEL = 1;
        short LOW_LEVEL = 0;

        //设置正负限位开关触发电平
	    rtn = NMC_MtLmtSns(axisHandle, HIGH_LEVEL, HIGH_LEVEL);
        checkError(rtn, "NMC_MtLmtSns");

        // 验证设置结果
        ShortByReference pPosLmtSns = new ShortByReference();
        ShortByReference pNegLmtSns = new ShortByReference();
        rtn = NMC_MtGetLmtSns(axisHandle, pPosLmtSns, pNegLmtSns);
        checkError(rtn, "NMC_MtGetLmtSns (Get)");

        short readPosLmtSns = pPosLmtSns.getValue();
        short readNegLmtSns = pNegLmtSns.getValue();

        System.out.printf("设置结果: 正向限位=%d, 负向限位=%d%n", HIGH_LEVEL, HIGH_LEVEL);
        System.out.printf("读取结果: 正向限位=%d, 负向限位=%d%n", readPosLmtSns, readNegLmtSns);
        System.out.println(readPosLmtSns == HIGH_LEVEL && readNegLmtSns == HIGH_LEVEL ? "✅ 限位触发电平设置验证通过" : "❌ 限位触发电平设置验证失败");

        //设置报警触发电平
	    rtn = NMC_MtAlarmSns(axisHandle, HIGH_LEVEL);
        checkError(rtn, "NMC_MtAlarmSns");

         // 验证设置结果
        ShortByReference pAlarmSns = new ShortByReference();
        rtn = NMC_MtGetAlarmSns(axisHandle, pAlarmSns);
        checkError(rtn, "NMC_MtGetAlarmSns (Get)");

        short readAlarmSns = pAlarmSns.getValue();

        System.out.printf("设置结果: 报警=%d%n", HIGH_LEVEL);
        System.out.printf("读取结果: 报警=%d%n", readAlarmSns);
        System.out.println(readAlarmSns == HIGH_LEVEL ? "✅ 报警触发电平设置验证通过" : "❌ 报警触发电平设置验证失败");

        //设置原点触发电平
	    rtn = NMC_MtHomeSns(axisHandle, HIGH_LEVEL);
        checkError(rtn, "NMC_MtHomeSns");

        // 验证设置结果
        ShortByReference pHomeSns = new ShortByReference();
        rtn = NMC_MtGetHomeSns(axisHandle, pHomeSns);
        checkError(rtn, "NMC_MtGetHomeSns (Get)");

        short readHomeSns = pHomeSns.getValue();

        System.out.printf("设置结果: 原点=%d%n", HIGH_LEVEL);
        System.out.printf("读取结果: 原点=%d%n", readHomeSns);
        System.out.println(readHomeSns == HIGH_LEVEL ? "✅ 原点触发电平设置验证通过" : "❌ 原点触发电平设置验证失败");


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

    }
}