// scan_example.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "conio.h"
//////////////////////////////////////////////////////////////////////////
#pragma comment(lib,"nmc_lib20.lib")
#include "mtn_lib20.h"
#include "mtn_lib20_ext.h"
#include "mtn_lib20_err.h"
#include "mtn_lib20_oem.h"

short ScanTest_Simple(HAND hDev);
short ScanTest_RectangleLoop(HAND hDev);

int _tmain(int argc, _TCHAR* argv[])
{
	HAND hDev;
	short rtn;
	short chn = 0;
	short xPos[1024];
	short yPos[1024];

	// 1.连接控制器
	rtn = NMC_DevOpen(0,&hDev);
	if(rtn != 0){ printf("DevOpen error = %d",rtn); return 1;}

	// 2.开启振镜功能
	rtn = OEM_ScanEn(hDev,1,chn);
	if(rtn != 0){ printf("OEM_ScanEn error = %d",rtn); return 1;}

	// 测试1：简单指令测试
	rtn = ScanTest_Simple(hDev);
	if(rtn != 0){ printf("ScanTest_Simple error = %d",rtn); return 1;}

	// 测试2：连续的矩形雕刻测试
	rtn = ScanTest_RectangleLoop(hDev);
	if(rtn != 0){ printf("ScanTest_RectangleLoop error = %d",rtn); return 1;}

	getch();
	return 0;
}



short ScanTest_RectangleLoop(HAND hDev)
{
	short rtn;
	short chn = 0;
	short xPos[1024];
	short yPos[1024];

	// 1.BufClr
	rtn = OEM_ScanBufClr(hDev,chn);
	if(rtn != 0){ printf("OEM_ScanBufClr error = %d",rtn); return 1;}

	// 2.压入指令,基本的能量设置 激光开关等
	rtn = OEM_ScanBufLaserOnOff(hDev,0,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserOnOff error = %d",rtn);return 1;}

	rtn = OEM_ScanBufDelay(hDev,100,chn);
	if(rtn != 0) {printf("OEM_ScanBufDelay error = %d",rtn);return 1;}

	rtn = OEM_ScanBufLaserSetOnOffDelay(hDev,0,0,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserSetOnOffDelay error = %d",rtn);return 1;}
	
	rtn = OEM_ScanBufLaserSetPowerPwm(hDev,2000,1000,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserSetPowerPwm error = %d",rtn);return 1;}

	rtn = OEM_ScanBufLaserOnOff(hDev,1,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserOnOff error = %d",rtn);return 1;}

	// 3.连续的矩形雕刻数据下压
	TOemScanSts sts;
	int pushCnt = 1;
	short dir = 0,loop=0,curPosX,curPosY;
	int step = 4;
	int posStart = 0,posMax = 5000;
	unsigned long cmdCnt = 0,startFlag=0;
	curPosX = posStart;
	curPosY = posStart;
	printf("\n矩形雕刻测试开始.... \n");
	for(;;)
	{
		pushCnt = 128;
		for(int k=0;k<pushCnt;k++)
		{
			switch(dir)
			{
			case 0: // x+
				{
					curPosX = curPosX + step;
					if(curPosX >= posMax)
					{
						dir = 1;
					}
				}
				break;
			case 1:// y+
				{
					curPosY = curPosY + step;
					if(curPosY >= posMax)
					{
						dir = 2;
					}
				}
				break;
			case 2:// x-
				{
					curPosX = curPosX - step;
					if(curPosX <= posStart)
					{
						dir = 3;
					}
				}
				break;
			case 3:// y-
				{
					curPosY = curPosY - step;
					if(curPosY <= posStart)
					{
						dir = 0;
						loop++;
						pushCnt = k;
					}
				}
				break;
			}
			xPos[k] = curPosX;
			yPos[k] = curPosY;
 
		}
		rtn = OEM_ScanBufMoveAbs(hDev,xPos,yPos,pushCnt,0);
		if(rtn != 0) {printf("OEM_ScanBufMoveAbs error = %d",rtn);return 1;}
		cmdCnt = cmdCnt + pushCnt;

		// 压入一定数据后启动加工
		if((startFlag == 0) && (cmdCnt > 10000))
		{
			startFlag=  1;
			rtn = OEM_ScanOnOff(hDev,1,0);
			if(rtn != 0) {printf("OEM_ScanOnOff error = %d",rtn);return 1;}
		}
		if(loop > 500){break;}	// 循环500次

		// 查询空间
		while(1)
		{
			OEM_ScanGetSts(hDev,&sts,0);
			if(sts.leftSpace > 22048)
			{
				break;
			}
			OEM_ScanGetSts(hDev,&sts,chn);
			printf("已经压入指令数=%d loop=%d 当前位置x=%d,y=%d \r",cmdCnt,loop,sts.xPos,sts.yPos);
		}

		OEM_ScanGetSts(hDev,&sts,chn);
		printf("已经压入指令数=%d loop=%d 当前位置x=%d,y=%d \r",cmdCnt,loop,sts.xPos,sts.yPos);
	}


	// 4.结束指令压入
	rtn = OEM_ScanEndList(hDev,chn);
	if(rtn != 0) {printf("OEM_ScanEndList error = %d",rtn);return 1;}

	printf("\n矩形雕刻测试完成.... \n\n");

	return 0;
}


short ScanTest_Simple(HAND hDev)
{
	short rtn;
	short chn = 0;
	short xPos[1024];
	short yPos[1024];

	// 1.BufClr
	printf("\n简单测试开始.... \n");
	rtn = OEM_ScanBufClr(hDev,chn);
	if(rtn != 0){ printf("OEM_ScanBufClr error = %d",rtn); return 1;}

	// 2.压入指令
	rtn = OEM_ScanBufLaserOnOff(hDev,0,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserOnOff error = %d",rtn);return 1;}

	rtn = OEM_ScanBufDelay(hDev,100,chn);
	if(rtn != 0) {printf("OEM_ScanBufDelay error = %d",rtn);return 1;}

	rtn = OEM_ScanBufLaserSetOnOffDelay(hDev,0,0,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserSetOnOffDelay error = %d",rtn);return 1;}
	
	rtn = OEM_ScanBufLaserSetPowerPwm(hDev,2000,1000,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserSetPowerPwm error = %d",rtn);return 1;}

	rtn = OEM_ScanBufLaserOnOff(hDev,1,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserOnOff error = %d",rtn);return 1;}

	for(int k=0;k<20;k++)
	{
		for(int i=0;i<1024;i++)
		{
			xPos[i] = (i+1) * 2;
			yPos[i] = (i+1) * 3;
		}
		rtn = OEM_ScanBufMoveAbs(hDev,xPos,yPos,1024,0);
		if(rtn != 0) {printf("OEM_ScanBufMoveAbs error = %d",rtn);return 1;}

		for(int i=0;i<1024;i++)
		{
			xPos[i] = (1023 - i+1) * 2;
			yPos[i] = (1023 - i+1) * 3;
		}
		rtn = OEM_ScanBufMoveAbs(hDev,xPos,yPos,1024,0);
		if(rtn != 0) {printf("OEM_ScanBufMoveAbs error = %d",rtn);return 1;}
	}

	rtn = OEM_ScanBufLaserOnOff(hDev,0,chn);
	if(rtn != 0) {printf("OEM_ScanBufLaserOnOff error = %d",rtn);return 1;}

	// 3.结束指令压入
	rtn = OEM_ScanEndList(hDev,chn);
	if(rtn != 0) {printf("OEM_ScanEndList error = %d",rtn);return 1;}

	// 4.启动Scan缓冲指令
	rtn = OEM_ScanOnOff(hDev,1,chn);
	if(rtn != 0) {printf("OEM_ScanOnOff error = %d",rtn);return 1;}

	// 5.状态检查
	TOemScanSts sts;
	while(1)
	{
		OEM_ScanGetSts(hDev,&sts,chn);
		if((sts.sts & BIT_SCAN_RUN) == 0)
		{
			break;
		}
		printf("当前位置x=%d,y=%d \r",sts.xPos,sts.yPos);
	}

	printf("\n简单测试完成.... \n\n");

	return 0;
}