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 group = 0;
        NativeLongByReference pDiVal = new NativeLongByReference();

        // 读取第一组数字量输入状态
        rtn = NMC_GetDI(devHandle, group, pDiVal);
        checkError(rtn, "NMC_GetDI");
        long diValue = pDiVal.getValue().longValue();
        System.out.println("DI Group " + group + " value: " + diValue);

        // 设置第一组数字量输出
        NativeLong doVal = new NativeLong(1); // 设置 DO0 为高电平
        rtn = NMC_SetDO(devHandle, group, doVal);
        checkError(rtn, "NMC_SetDO");

        // 设置限位开关触发电平为高电平
        rtn = NMC_MtLmtSns(axisHandle, (short) 1, (short) 1);
        checkError(rtn, "NMC_MtLmtSns");

        // 按位读取数字量输入状态
        ShortByReference pBitVal = new ShortByReference();
        short bitIndex = 1;
        rtn = NMC_GetDIBit(devHandle, bitIndex, pBitVal);
        checkError(rtn, "NMC_GetDIBit");
        System.out.println("DI Bit " + bitIndex + " value: " + pBitVal.getValue());

        // 按位设置数字量输出状态
        bitIndex = 2;
        rtn = NMC_SetDOBit(devHandle, bitIndex, (short) 0); // DO2 = 0
        checkError(rtn, "NMC_SetDOBit");

        // 获取 DO 第一组输出值
        NativeLongByReference pDoVal = new NativeLongByReference();
        rtn = NMC_GetDOGroup(devHandle, pDoVal, group);
        checkError(rtn, "NMC_GetDOGroup");
        long doValue = pDoVal.getValue().longValue();
        System.out.println("DO Group " + group + " value: " + doValue);

        // 分别获得位 DO 值
        System.out.println("Bit values of DO Group:");
        for (int i = 0; i < 32; i++) {
            short bitVal = (short) ((doValue >> i) & 1);
            System.out.println("  DO" + i + ": " + bitVal);
        }

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

    }
}