using System;
using Dll_CSharp_VSensor;
using System.Collections.Generic;
using System.Threading;
namespace Demo.ExternalTrigger
{
    public class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //获取相对路径
                string path = System.IO.Directory.GetCurrentDirectory();
                //实例化设备
                VSensorDLL VSensor = new VSensorDLL();
                //获取设备列表
                List<VSensorCameraInfoCLR> list = new List<VSensorCameraInfoCLR>(20);
                for (int i = 0; i < 20; i++)
                {
                    list.Add(new VSensorCameraInfoCLR());
                }
                int num = 0;
                VSensor.GetDeviceList(ref list, ref num);
                for (int i = 0; i < num; i++)
                {
                    Console.WriteLine("device num {0} : list[{1}] = {2}\n", i, i, list[i].CameraName);
                }
                //连接第一台设备
                int status = VSensor.DeviceConnect(0);
                if (status != 0)
                {
                    Console.WriteLine("connect device failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设备参数初始化，仅上电时需要进行此操作，初始化需要等待15s
                Console.WriteLine("waiting for parameter init...\n");
                status = VSensor.DeviceParameterInit();
                if (status != 0)
                {
                    Console.WriteLine("parameter init failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //切换为线模式(初始化默认为面模式)
                status = VSensor.SetRestructionMode(1);
                if (status != 0)
                {
                    Console.WriteLine("set restruction mode failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设备参数初始化，切换线面重建模式都要初始化等待具体等待时间看设备型号
                Console.WriteLine("waiting for parameter init...\n");
                status = VSensor.DeviceParameterInit();
                if (status != 0)
                {
                    Console.WriteLine("parameter init failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设置为硬件触发模式(默认为预览模式)
                status = VSensor.SetCaptureMode(2);
                if (status != 0)
                {
                    Console.WriteLine("set capture mode failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设置投影等级
                status = VSensor.SetProjectLight(1);
                if (status != 0)
                {
                    Console.WriteLine("set project light failed, status = {0}\n", status);
                    Console.ReadLine();
                }
/*
                //设置外部触发模式为计数器模式
                status = VSensor.SetExternalTriggerMode(1);
                if (status != 0)
                {
                    Console.WriteLine("set external trigger mode failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设置外部触发延时
                status = VSensor.SetExternalTriggerDelay(0);
                if (status != 0)
                {
                    Console.WriteLine("set external trigger delay failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设置外部触发种类
                status = VSensor.SetExternalTriggerType(0);
                if (status != 0)
                {
                    Console.WriteLine("set external trigger type failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //设置外部触发计数器触发数量设定值
                status = VSensor.SetExternalTriggerCounterTriggers(512);
                if (status != 0)
                {
                    Console.WriteLine("set external trigger counter triggers failed, status = {0}\n", status);
                    Console.ReadLine();
                }
*/
                //设置缓存区大小
                status = VSensor.SetCloudBufferNum(10);
                if (status != 0)
                {
                    Console.WriteLine("set cloud buffer num failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                //预留时间进行硬件触发采集
                Console.WriteLine("wait external trigger...\n");
                Thread.Sleep(5000);
                //获取缓存区触发的点云
                Console.WriteLine("get cloud buffer...\n");
                int width = 0;
                int height = 0;
                VSensor.GetImageResolution(ref width, ref height);
                List<VSensorResultCLR> resultList = new List<VSensorResultCLR>(10);
                for (int i = 0; i < 10; i++)
                {
                    resultList.Add(new VSensorResultCLR(width, height));
                }
                int resultNum = 0;
                VSensor.GetCloudBuffer(ref resultList, ref resultNum);
                if (status != 0)
                {
                    Console.WriteLine("get cloud buffer failed, status = {0}\n", status);
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("getted {0} frames of data\n", resultNum);
                    //保存点云为ply格式
                    for (int i = 0; i < resultNum; i++)
                    {
                        string varString = Convert.ToString(i);
                        string str = path + @"\test" + varString + ".ply";
                        status = VSensor.Save3DCloud(str, resultList[i], 0);
                        if (status != 0)
                        {
                            Console.WriteLine("save 3D points failed, status = {0}\n", status);
                            Console.ReadLine();
                        }
                    }
                }
                //反初始化设备
                status = VSensor.DeviceUnInit();
                if (status != 0)
                {
                    Console.WriteLine("device release failed, status = {0}\n", status);
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ex:{ex}");
            }
            Console.WriteLine("run success");
            Console.ReadLine();
            Console.ReadKey();
        }
    }
}
