#define PYNECT_DIRECT_USE_DLL      /* 使用动态库时定义 */
#include "pynect_direct.h"
#include <stdio.h>
#include <stdlib.h>

static int require(int code, const char* what)
{
    if (code != PYNECT_DIRECT_SUCCESS) {
        printf("[FAIL] %s: return code = %d\n", what, code);
        return 0;
    }
    return 1;
}

int main(void)
{
    PynectDirectDeviceInfo info;
    PynectDirectSelectedWavelengthRange sel;
    uint16_t count = 0, i;
    double* wavelengths = NULL;
    uint16_t* spectrum = NULL;
    double start_nm = 0.0, end_nm = 0.0;
    unsigned int integration = 0;
    int n;

    /* 1. 扫描设备 */
    n = pynect_direct_scan_devices();
    if (n <= 0) {
        printf("No device found.\n");
        system("pause");
        return 1;
    }
    printf("Found %d device(s).\n", n);

    /* 2. 打开设备（索引 0） */
    if (!require(pynect_direct_open_device(0), "open_device")) goto done;

    /* 3. 读取设备信息 */
    if (require(pynect_direct_get_device_info(&info), "get_device_info")) {
        printf("Protocol: %s\n", info.protocol_name);
        printf("Model:    %s\n", info.model);
        printf("Serial:   %s\n", info.serial_number);
        printf("Hardware: %s\n", info.hardware_version);
    }

    /* 4. 数据点数 */
    if (!require(pynect_direct_get_wavelength_count(&count), "get_wavelength_count")) goto done;
    printf("Wavelength count: %u\n", count);
    if (count == 0) goto done;

    /* 5. 波长范围（全量波长数组首尾，必然正确） */
    if (!require(pynect_direct_get_full_wavelength_range(&start_nm, &end_nm), "get_full_wavelength_range")) goto done;
    printf("Wavelength range: %.2f ~ %.2f nm\n", start_nm, end_nm);

    /* 6. 分配数组 */
    wavelengths = (double*)malloc(count * sizeof(double));
    spectrum = (uint16_t*)malloc(count * sizeof(uint16_t));
    if (!wavelengths || !spectrum) {
        printf("Out of memory.\n");
        goto done;
    }

    /* 7. 读取波长坐标 */
    if (!require(pynect_direct_get_full_wavelength_data(wavelengths, count), "get_full_wavelength_data")) goto done;

    /* 8. 设置并读取积分时间（临时，不写入固化配置） */
    if (require(pynect_direct_get_integration_time(&integration), "get_integration_time"))
        printf("Current integration time: %u us\n", integration);
    pynect_direct_set_integration_time(50000);   /* 50 ms */

    /* 9. 采集光谱 */
    if (!require(pynect_direct_get_full_spectrum_data(spectrum, count), "get_full_spectrum_data")) goto done;

    /* 10. 两列打印：波长 + 对应强度（前 5 点 + 后 5 点） */
    printf("%6s  %13s  %9s\n", "Index", "Wavelength(nm)", "Intensity");
    printf("%6s  %13s  %9s\n", "-----", "--------------", "---------");
    for (i = 0; i < 5 && i < count; i++)
        printf("%6u  %13.3f  %9u\n", i, wavelengths[i], spectrum[i]);
    if (count > 10) printf("...  (total %u points)\n", count);
    for (i = count > 5 ? count - 5 : 0; i < count; i++)
        printf("%6u  %13.3f  %9u\n", i, wavelengths[i], spectrum[i]);

    /* 11. 指定波段（上位机截取，不修改设备） */
    if (start_nm < 400.0 && end_nm > 800.0) {
        if (require(pynect_direct_set_selected_wavelength_range(400.0, 800.0), "set_selected_range")) {
            if (require(pynect_direct_get_selected_wavelength_range(&sel), "get_selected_range")) {
                printf("Selected: pixels %u..%u, count=%u, actual %.2f..%.2f nm\n",
                       sel.start_pixel, sel.end_pixel, sel.count,
                       sel.actual_start_nm, sel.actual_end_nm);
            }
        }
    } else {
        printf("Device does not cover 400-800 nm, skip selected range.\n");
    }

    /* 12. 关闭设备 */
done:
    free(wavelengths);
    free(spectrum);
    pynect_direct_close_device();
    printf("Done.\n");
    system("pause");
    return 0;
}
