⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 running.cpp

📁 该程序实现的是一个在织布厂用的提花机程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "Wea2.h"
//#include "JobList.h"
//#include "running.h"
//#include <conio.h>
//#include "define.h"
//extern CRITICAL_SECTION g_cs_run;
//extern BOOL Send(int state,int data);

CRunning::CRunning()
{
	m_data_i=0;
	m_data_o=0;
	m_data_error=0;
	m_bFisrtBackward=1;
/*	m_data_i=new BYTE*[SAVED_LINES];
	m_data_o=new BYTE*[SAVED_LINES];
	if(m_data_i==NULL || m_data_o==NULL)
	{
			AfxMessageBox("Out of memory");
			return;
	}
	for(int i=0;i<SAVED_LINES;i++)
	{
		m_data_i[i]=new BYTE[2688];
		m_data_o[i]=new BYTE[2688];
		if(m_data_i[i]==NULL || m_data_o[i]==NULL)
		{
			AfxMessageBox("Out of memory");
			return;
		}
	}
*/	m_pJob=NULL;
	m_bFinished=FALSE;
	InitialMap();
	InitialWriterPointer();

	m_weao._bit.ready=0;
	m_weao._bit.rpt_end=1;
	m_weao._bit.job_end=0;
	_outp(WEAVER_O,m_weao._byte);

	int p=MAP_O;
	MAPO map;
	map._bit.sdata=1;
	map._bit.sclk=1;
	map._bit.latch=1;
	_outp(p,map._byte);


	p=ABB_O;
	int d=0x3f;
	_outp(p,d);
	m_ind_i=0;
//	SendWRST();  
}

CRunning::~CRunning()
{
	if(m_data_last)
		delete m_data_last;
	if(m_data_i)
	{
		for(int i=0;i<SAVED_LINES;i++)
		{
			delete m_data_i[i];
		}
	}
	delete m_data_i;
	if(m_data_o)
	{
		for(int i=0;i<SAVED_LINES;i++)
		{
			delete m_data_o[i];
		}
	}
	delete m_data_o;
}

void CRunning::Initialize(CJobList* pJob)
{
//	SendWRST();
//	SendRRST();
	InitialMap();
	m_pJob=pJob;
	m_nWea=pJob->GetSize();
	if(m_nWea>0)
	{
		m_pWea=(CWea*)pJob->GetAt(m_WritePointer.WeaIndex._int);
		m_pWeaData=m_pWea->m_Wea3;
		m_nLineByte=pJob->m_Width;
		m_ErrorRecord=m_nLineByte;
		if(m_data_last==NULL)
			m_data_last=new BYTE[m_nLineByte];
		if(m_data_last==NULL)
		{
			AfxMessageBox("Out of memory");
			return;
		}

/*
	int d=1;
	_outp(FIFO_WE,d);
//  send number of bytes per line
	_outp(LINE_BYTE_LEN1,m_pJob->m_Width/256);
	Delay();
	_outp(LINE_BYTE_LEN0,m_pJob->m_Width%256);
*/	}
}


void CRunning::Delay()      //0.5mS
{
	for(int i=0;i<1000;i++)
		for(int j=0;j<100;j++)
		{
		}
}
void CRunning::SendWRST()
{
/*	int d,p;
	p=FIFO_WRST;
	d=0;
	_outp(p,d);    //wrst=0
	for(int i=0;i<3;i++)
	{
		_outp(FIFO_WCK,d);  //wck
	}
    d=1;
	p=FIFO_WRST;
	_outp(p,d);  //wrst=1
	p=FIFO_WCK;
	d=0;
	_outp(p,d);
*/
}

BOOL CRunning::Forward1()
{   
	if(m_pWeaData==NULL)
		return FALSE;
	if(m_WritePointer.i._int<m_pWeaData->WeaSize.cy-1)  //not reach wea boundary
	{
		m_WritePointer.i._int++;
		return FALSE;
	}
	m_WritePointer.i._int=0;
	m_WritePointer.WeaRepeat._int++;//reach wea boundary
	if(m_pWea->m_nRepeat==0)//reach wea boundary, not finish wea repeat (infinit)
		return FALSE;
	if(m_WritePointer.WeaRepeat._int<m_pWea->m_nRepeat)
		return FALSE;            // not finish wea repeat
	if(m_WritePointer.WeaIndex._int<m_nWea-1)  //wea repeat finished, not finish one job
	{
		m_WritePointer.WeaIndex._int++;
		m_pWea=(CWea*)m_pJob->GetAt(m_WritePointer.WeaIndex._int);
		m_pWeaData=m_pWea->m_Wea3;
	//	m_pWeaData=m_pWea->m_Wea1;
		m_WritePointer.i._int=m_WritePointer.j._int=0;
		m_WritePointer.WeaRepeat._int=0;
		return FALSE;
	}
	{
		m_pWea=(CWea*)m_pJob->GetAt(0);  //one job repeat finished,
		m_pWeaData=m_pWea->m_Wea3;
	//	m_pWeaData=m_pWea->m_Wea1;
		m_WritePointer.i._int=m_WritePointer.j._int=0;
		m_WritePointer.WeaRepeat._int=0;
		m_WritePointer.WeaIndex._int=0;
		m_WritePointer.JobRepeat._int++;
	}
	if(m_pJob->m_nRepeat==0)
		return FALSE;
	if(m_WritePointer.JobRepeat._int>=m_pJob->m_nRepeat) //  finish job repeat
	{
		m_bFinished=TRUE;
		return TRUE;
	}
	else
		return FALSE;

}
void CRunning::Backward1()
{    
	if(m_WritePointer.i._int>0)  //not reach wea boundary
	{
		m_WritePointer.i._int--;
		return;
	}
	if(m_pWea->m_nRepeat==0)//reach boundary, not finish wea repeat
	{
		m_WritePointer.i._int=m_pWeaData->WeaSize.cy-1;
		return;
	}

	if(m_WritePointer.WeaRepeat._int>0)//reach boundary, not finish wea repeat
	{
		m_WritePointer.WeaRepeat._int--;
		m_WritePointer.i._int=m_pWeaData->WeaSize.cy-1;
		return;
	}
	if(m_WritePointer.WeaIndex._int>0)  //wea repeat finished, not finish one job
	{
		m_WritePointer.WeaIndex._int--;
		m_pWea=(CWea*)m_pJob->GetAt(m_WritePointer.WeaIndex._int);
		m_pWeaData=m_pWea->m_Wea3;
	//	m_pWeaData=m_pWea->m_Wea1;
		m_WritePointer.i._int=m_WritePointer.j._int=0;
		m_WritePointer.WeaRepeat._int=m_pWea->m_nRepeat;
		return;
	}
	if(m_WritePointer.JobRepeat._int>0 || m_pJob->m_nRepeat==0)  //one job finished, not finish job repeat
	{
		m_WritePointer.JobRepeat._int--;
		int n=m_pJob->GetSize();
		m_pWea=(CWea*)m_pJob->GetAt(n-1);
		m_pWeaData=m_pWea->m_Wea3;
		m_WritePointer.i._int=m_pWea->m_Size.cy-1;
		m_WritePointer.j._int=0;
		m_WritePointer.WeaRepeat._int=0;
		m_WritePointer.WeaIndex._int=n-1;
	}

}

void CRunning::SendOneLine()
{
/*	if(m_pWeaData==NULL || m_WritePointer.j>=m_nLineByte)
		return;
//	TRACE("\nLine %d ", m_WritePointer.i);
	_outp(LINE_BYTE_LEN1,m_pJob->m_Width/256);
	Delay1u();
	_outp(LINE_BYTE_LEN0,m_pJob->m_Width%256);
	BYTE buff;
	SendWRST();
	p=FIFO_WE;
	d=0;
	_outp(p,d);//---------------
//	p=CLR_READY;
//	_outp(p,d);
	p=FIFO_WCK;
	_outp(p,d); // dummy write
	for(int ii=0;ii<2;ii++)
	{
//		buff=m_WritePointer.i%256;    ////for check
//		_outp(p,buff);   //for check
		for(m_WritePointer.j=0;m_WritePointer.j<m_nLineByte;m_WritePointer.j++)
//		for(m_WritePointer.j=0;m_WritePointer.j<m_nLineByte-1;m_WritePointer.j++)  //for check
		{
			buff=m_pWeaData->Pic[m_WritePointer.i][m_WritePointer.j];
			_outp(p,buff);
		}

*/
//  for debug pick----------------
/*	for(int j=0;j<m_nLineByte;j++)
	{
		int e=(m_WritePointer.i % 8) +1;
		buff=1;
		for(int k=1;k<=e;k++)
			buff=buff*2;
		buff=buff-1;
		int port=FIFO_WCK;
		_outp(port,buff);
	}
*/
//  for debug data---------------

/*		int k=1;buff=0;
		for(int j=0;j<m_nLineByte;j++)
		{
			buff=buff | k;
			int port=FIFO_WCK;
			_outp(port,buff);
			k=k*2;
		}

//---------------------------------
//  for debug data---------------

		for(int j=0;j<m_nLineByte;j++)
		{
			buff=j % 256;
			int port=FIFO_WCK;
			_outp(port,buff);
		}

//---------------------------------
*/		m_WritePointer.j._int=0;

/*		_outp(p,m_pWeaData->Abb[m_WritePointer.i][0]);
		for (int i=1;i<=5;i++)
			_outp(p,m_pWeaData->ContInfo[m_WritePointer.i][i]);
	}
	for(ii=0;ii<200;ii++)
		_outp(p,d);
	p=FIFO_WE;
	d=1;
	_outp(p,d);//----------------------

	SendControl();

//	p=CLR_READY;
//	_outp(p,d);
//	printf("Line %d, Repeat: &d",m_WritePointer.i,m_WritePointer.WeaRepeat);
*/
}

void CRunning::SendLines(int nLine)
{
	if(nLine==0 || m_pJob==NULL)
		return;
	for(int i=0;i<nLine;i++)
	{
		SendOneLine();
		Forward1();

		if(m_bFinished)
			return;
		if(m_pJob->m_nRepeat==0 || m_WritePointer.JobRepeat._int<m_pJob->m_nRepeat)
			continue;
		if(m_WritePointer.WeaRepeat._int<m_pWea->m_nRepeat)
			continue;
		if(m_WritePointer.WeaIndex._int<m_nWea)
			continue;
		if(m_WritePointer.i._int<m_pWeaData->WeaSize.cy)
			continue;
		break;
	}
}


void CRunning::OneLineToShift(BOOL bNot)
{
//for debug.  
/*	int k,buff,j;
		buff=0;k=2;
		for(j=0;j<4*8;j++)
		{
			buff=buff | k;
			k=k*2;
			ByteToMap(buff);
			if(buff==255)
			{
			buff=0;k=2;
			}
		}		
	for(int g=0;g<200;g++)
	{
	Delay1u();
	Delay1u();
	}

		buff=0;k=2;
		for(j=0;j<4*8;j++)
		{
			buff=buff | k;
			k=k*2;
			ByteToMap(buff);
			if(buff==255)
			{
			buff=0;k=2;
			}
		}		
	return;	
*/	if(m_pWeaData==NULL)
		return;
	BYTE data;
	for(m_WritePointer.j._int=0;m_WritePointer.j._int<m_nLineByte;m_WritePointer.j._int++)
	{
		int j=m_WritePointer.j._int;
		data=m_pWeaData->Pic[m_WritePointer.i._int][m_WritePointer.j._int];
		if(bNot)
			data=data^0xff;

		
		ByteToMap(data);
	}
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	for(m_WritePointer.j._int=0;m_WritePointer.j._int<m_nLineByte;m_WritePointer.j._int++)
	{
		data=m_pWeaData->Pic[m_WritePointer.i._int][m_WritePointer.j._int];
		if(bNot)
			data=data^0xff;
		ByteToMap(data);
	}
}


BOOL CRunning::OneLineToShiftCheck(BOOL bNot)
{
//	m_ind_j=0;
	if(m_pWeaData==NULL)
		return FALSE;
	BOOL OK=TRUE;
	BYTE data;
	for(m_WritePointer.j._int=0;m_WritePointer.j._int<m_nLineByte;m_WritePointer.j._int++)
	{
		data=m_pWeaData->Pic[m_WritePointer.i._int][m_WritePointer.j._int];
		if(bNot)
			data=data^0xff;
		ByteToMap(data);
	}
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
//	g_test.SendLatch();  //=======   check latch-register
//	g_test.SendEnable(2);//=======2check latch-register
//	g_running.DelayMs(1);//=======1check latch-register
	for(m_WritePointer.j._int=0;m_WritePointer.j._int<m_nLineByte;m_WritePointer.j._int++)
	{
		data=m_pWeaData->Pic[m_WritePointer.i._int][m_WritePointer.j._int];

		if(bNot)
			data=data^0xff;

//		if(ByteToMapCheck(data,m_WritePointer.j._int)==FALSE)
		if(ByteToMapCheck(data)==FALSE)
		{
			if(m_WritePointer.j._int>8)//delete
			{
				OK=FALSE;
			}
		}
	}
	if((OK==FALSE)&&(g_bDataLock))
	{
		StopRun();
//		WriteLines(m_ind_i);  //  deleted later
//		m_ErrorMap=GetErrorMap(m_ind_i);
		m_ErrorMap=GetErrorMap();
	}
//	m_ind_i++;
//	m_ind_i=m_ind_i%SAVED_LINES;
	return OK;
}
BOOL CRunning::OneLineToShiftCheckLast(BOOL bNot)
{
	if(m_pWeaData==NULL)
		return FALSE;
	BOOL OK=TRUE;
	BYTE data;

	for(m_WritePointer.j._int=0;m_WritePointer.j._int<m_nLineByte;m_WritePointer.j._int++)
	{
		data=m_pWeaData->Pic[m_WritePointer.i._int][m_WritePointer.j._int];

		if(bNot)
			data=data^0xff;

		if(ByteToMapCheckLast(data,m_WritePointer.j._int)==FALSE)
		{
		//	if(m_WritePointer.j._int>8)
			{
				OK=FALSE;
			}
		}
	}
	if((OK==FALSE)&&(g_bDataLock))
	{
		StopRun();
		m_ErrorMap=GetErrorMap();
	}
	return OK;
}

void CRunning::EnableToMap(int TimeWidth)
{
	int t=TimeWidth*2;
	int p=PC_ENABLE_L_O;
	int d=0;
	_outp(p,d);
	p=PC_ENABLE_H_O;
	_outp(p,d);
	for(int i=0;i<t;i++)
		g_running.Delay();
	p=PC_ENABLE_L_O;
	_outp(p,d);
}

void CRunning::OneLineToMap(BOOL bNot)
{
//	int s=_inp(SENSOR)&0x01;
//	TRACE("\nbefore send, sensor=%d",s);
	InitialMap();
	OneLineToShift(bNot);
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	LatchToMap();
//	SendControl();
	
	if(m_WritePointer.i._int==0)      
		m_weao._bit.rpt_end=0;  //reapt end
	else
		m_weao._bit.rpt_end=1;
	if(m_bFinished)
		m_weao._bit.job_end=1;  //job end
	else
		m_weao._bit.job_end=0;
//	m_weao._bit.ready=1;
	_outp(WEAVER_O,m_weao._byte);
	
	Record();   ///////  record

//	s=_inp(SENSOR)&0x01;
//	TRACE("\nafter send, sensor=%d",s);
}

BOOL CRunning::OneLineToMapCheck01(BOOL bAllOne)
{
	InitialMap();
	BOOL OK=TRUE;
	BYTE data;
	if(bAllOne)
		data=0xff;
	else
		data=0;
	for(int j=0;j<m_nLineByte;j++)
	{
		ByteToMap(data);
	}
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	for(j=0;j<m_nLineByte;j++)
	{

		if(ByteToMapCheck(data,j)==FALSE)
		{
			if(j>8)
			{
				OK=FALSE;
			}
		}
	}
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	LatchToMap();
	return OK;
}

BOOL CRunning::OneLineToMapCheck(BOOL bNot)
{
	InitialMap();
	BOOL OK=OneLineToShiftCheck(bNot); //send 2 data line to check
//	BOOL OK=OneLineToShiftCheckLast(bNot); //send 1 data line, check in next line
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	LatchToMap();
//	SendControl();
	
	if(m_WritePointer.i._int==0)      
		m_weao._bit.rpt_end=0;  //reapt end
	else
		m_weao._bit.rpt_end=1;
	if(m_bFinished)
		m_weao._bit.job_end=1;  //job end
	else
		m_weao._bit.job_end=0;
//_weao._bit.ready=1;
	_outp(WEAVER_O,m_weao._byte);
	Record();   ///////  record
	if(g_bDataLock)
		return OK;
	else
		return 1;
}
void CRunning::ByteToMap(BYTE data)
{
	int bit;
	int m=0x80;
	for(int i=0;i<8;i++)
	{
		bit=data&m;
		BitToMap(bit);
		m=m/2;
	}
}

/*BOOL CRunning::ByteToMapCheck(BYTE data)// old version, not used now
{
	BOOL OK=TRUE;
	int bit;
	int m=0x80;
	int error=0;
	for(int i=0;i<8;i++)
	{
		bit=data&m;
		if(bit!=0)
			bit=1;
		BitToMap(bit);
		m_data_o[m_ind_i][m_ind_j]=bit;
//		int bit_i=GetBit(0);    //  check latch_register data
		int bit_i=GetBit(1);    //  check shift_register data
		m_data_i[m_ind_i][m_ind_j]=bit_i;
		if(bit_i!=bit)     // methord 1: solftware compare
			error++;
//		if(GetError()==0)   // methord 2: hardware compare
//			error++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -