#include "VSensor.h" 
#include <signal.h> 
#include <direct.h>
#include <thread> 
#pragma comment(lib, "VSensorSDK.lib")
using namespace std;
using namespace VSENSOR;
int main()
{
    //获取相对路径
    char cwd[200];
    if (_getcwd(cwd, 200) == nullptr)
    {
        printf("get path failed\n");
    }
    //实例化设备
    VSensor* pVSensor = new VSensor();
    //获取设备列表
    VSensorCameraInfo cameraList[20];
    int num = 0;
    pVSensor->GetDeviceList(cameraList, &num);
    for (int i = 0; i < num; i++)
    {
        printf("device num %d : list[%d] = %s\n", i, i, cameraList[i].CameraName);
    }
    //连接设备，0为连接第一台设备  
    int status = pVSensor->DeviceConnect(0);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("connect device failed status = %d\n", status);
        system("pause");
    }
    //设备参数初始化，仅初次上电时需要进行此操作且该函数需要等待15s
    printf("waiting for parameter init...\n");
    status = pVSensor->DeviceParameterInit();
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("parameter init failed, status = %d\n", status);
        system("pause");
    }
    //切换为线模式(初始化默认为面模式)
    status = pVSensor->SetRestructionMode(emVSensorRestructionMode::MODE_LINE);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set restruction mode failed, status = %d\n", status);
        system("pause");
    }
    //设备参数初始化，切换线面重建模式都要初始化等待具体等待时间看设备型号
    printf("waiting for parameter init...\n");
    status = pVSensor->DeviceParameterInit();
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("parameter init failed, status = %d\n", status);
        system("pause");
    }
    //设置为硬件触发模式(默认为预览模式)
    status = pVSensor->SetCaptureMode(emVSensorCaptureMode::MODE_EXTERNAL);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set capture mode failed, status = %d\n", status);
        system("pause");
    }
    //设置投影等级
    status = pVSensor->SetProjectLight(1);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set project light failed, status = %d\n", status);
        system("pause");
    }
/*
    //设置外部触发模式为计数器模式
    status = pVSensor->SetExternalTriggerMode(emVSensorExternalTriggerMode::EXT_TRIG_MODE_COUNTER);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set external trigger mode failed, status = %d\n", status);
        system("pause");
    }
    //设置外部触发延时
    status = pVSensor->SetExternalTriggerDelay(0);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set external trigger delay failed, status = %d\n", status);
        system("pause");
    }
    //设置外部触发种类
    status = pVSensor->SetExternalTriggerType(emVSensorExternalTriggerType::EXT_TRIG_TYPE_LEADING_EDGE);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set external trigger type failed, status = %d\n", status);
        system("pause");
    }
    //设置外部触发计数器触发数量设定值
    status = pVSensor->SetExternalTriggerCounterTriggers(512);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set external trigger counter triggers failed, status = %d\n", status);
        system("pause");
    }
*/
    //设置缓存区大小
    status = pVSensor->SetCloudBufferNum(10);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set cloud buffer num failed, status = %d\n", status);
        system("pause");
    }
    //预留时间进行硬件触发采集
    printf("wait external trigger...\n");
    std::this_thread::sleep_for(std::chrono::milliseconds(5000));
    //获取缓存区触发的点云
    printf("get cloud buffer...\n");
    std::vector<VSensorResult> results;
    status = pVSensor->GetCloudBuffer(results);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("get cloud buffer failed, status = %d\n", status);
        system("pause");
    }
    else
    {
        printf("getted %zu frames of data\n", results.size());
        //保存无序点云为pcd格式
        for (int i = 0; i < results.size(); i++)
        {
            status = pVSensor->Save3DCloud(string(string(cwd) + "//test" + to_string(i + 1) + ".pcd"), &results[i], emVSensorPointType::POINT_TYPR_DISORDER);
            if (status != CAMERA_STATUS_SUCCESS)
            {
                printf("save 3D points failed, status = %d\n", status);
                system("pause");
            }
        }
    }
    //反初始化设备
    status = pVSensor->DeviceUnInit();
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("device release failed, status = %d\n", status);
        system("pause");
    }
    delete pVSensor;
    printf("run success\n");
    system("pause");
    return 0;
}