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

📄 usbdigitview.cpp

📁 进行USB通讯的源码 所用芯片为EZ-USB2131Q
💻 CPP
字号:
// USBDigitView.cpp : implementation file
//

#include "stdafx.h"
#include "USB.h"
#include "USBDigitView.h"
#include "USBDoc.h"
#include "UserDef.h"
#include "Main.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//---------------------------------------------------------
//引用CUSBDoc.cpp中定义的变量
extern BOOL bDataProcessing;                //数据处理标志                   
extern BOOL bDeviceRun;                     //采集线程运行标志               
extern int m_nDrawPointNum;                 //单通道点数 画图使用            
extern WORD ADBuffer[MAX_SEGMENT][16384];   // 缓冲队列                      
extern int nChannelCount;                   // 通道数                        
extern int DrawIndex;                       // 保存一屏数据索引              
extern int CurrentIndex;                    // AD数据处理线程当前缓冲区索引号
extern float VoltRange;                     //电压范围                       
extern float OffsetVolt;                    //电压范围一半                   
extern Control_Mode m_Control;              //控制参数传输 
extern int m_MiddleLsb[32];                 //是偏移码值,即原码减去LSB_HALF后的值,即在原点上下浮动的值
                  
//----------------------------------------------------------
BOOL bDigitRedraw;                          //数据重绘标志
int m_ProcessDigitalMode;                   //数据显示方式
//---------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CUSBDigitView

IMPLEMENT_DYNCREATE(CUSBDigitView, CScrollView)

CUSBDigitView::CUSBDigitView()
{
   m_DigitalDataShowMode=0;
   m_oldIndex=0;
}

CUSBDigitView::~CUSBDigitView()
{
}


BEGIN_MESSAGE_MAP(CUSBDigitView, CScrollView)
	//{{AFX_MSG_MAP(CUSBDigitView)
	ON_WM_VSCROLL()
	ON_WM_CREATE()
	ON_BN_CLICKED(IDM_DigitalVoltShowMode, OnDigitalVoltShowMode)
	ON_WM_CANCELMODE()
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUSBDigitView drawing



void CUSBDigitView::OnDraw(CDC* pDC)
{
	// TODO: add draw code here
   //CUSBDoc* pDC=GetDocument();
   //ASSERT(pDC);
   ShowDigit(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CUSBDigitView diagnostics

#ifdef _DEBUG
void CUSBDigitView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CUSBDigitView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
CUSBDoc* CUSBDigitView::GetDocument()
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CUSBDoc)));
	return (CUSBDoc*)m_pDocument;

}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CUSBDigitView message handlers

void CUSBDigitView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	if(lHint==1)  // 如果是数字方式,就执行父类的OnUpdate来调用其OnDraw
	{
		bDigitRedraw = TRUE;
		CScrollView::OnUpdate(pSender, lHint, pHint);
	}

}

void CUSBDigitView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	RedrawWindow();  
	CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}



int CUSBDigitView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CScrollView::OnCreate(lpCreateStruct) == -1)
		return -1;
	// TODO: Add your specialized creation code here
	
	return 0;
}

void CUSBDigitView::OnInitialUpdate() 
{
	CScrollView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
	// MSDN use this function to initialize a view’s 
	//scrolling limits based on the document size. 
	CSize sizeTotal;
	sizeTotal.cx = 2230; sizeTotal.cy = 1024+64;
	SetScrollSizes(MM_TEXT, sizeTotal);
	
	CUSBDoc* pDoc = (CUSBDoc*)GetDocument();//取得视图句柄,以便在DOC中的线程使用
	pDoc->m_hWndDigit=m_hWnd; 
}


void CUSBDigitView::ShowDigit(CDC *pDC)
{
    bDataProcessing = TRUE; // 表示正在处理数据,采集或处理线程不应该操作此段缓冲区    
	
	CRect rect;
	CString string;
	//新设置窗口大小
	GetClientRect(&rect);
	//打印
	OnPrepareDC(pDC);
	pDC->DPtoLP(&rect);    
	
	int x, y, i;  
	char str[100];
	x=0; y=rect.top-20;      // 确定始坐标
	ULONG Index=rect.top*8;  // 用滚动的象素点作为数组索引
	if(!bDeviceRun) 
	{
		DrawIndex=m_oldIndex;//如果停止采集则使用上一屏数据
	}
	m_oldIndex=DrawIndex;    //保存本次索引号以便停止时使用

	for(i=0; i<320; i++) 
	{
		
		if(m_DigitalDataShowMode==0)
		{
			sprintf(str, "%d", (((ADBuffer[DrawIndex][Index+i])^0x2000)&0x3fff)-LSB_HALF-m_MiddleLsb[(Index+i)%nChannelCount]);
		       // AfxMessageBox ("prosses end", NULL,MB_OK);

		}
		if (m_DigitalDataShowMode==1)
		{
			sprintf(str, "%x", ((((ADBuffer[DrawIndex][Index+i])^0x2000)&0x3fff)-LSB_HALF-m_MiddleLsb[(Index+i)%nChannelCount])&0xffff);
		}
		if (m_DigitalDataShowMode==2)
		{
			sprintf(str, "%7.2f", (((((ADBuffer[DrawIndex][Index+i])^0x2000)&0x3fff)-m_MiddleLsb[(Index+i)%nChannelCount])*PerLsbVolt-OffsetVolt));
		}
		
		if((i)%nChannelCount==0) 
		{
			x=0;  y=y+20;
		}
		else
		{
			if (m_DigitalDataShowMode==2)
			{
				x=x+70;
			}	
			else
			{
				x=x+50;
			}
		}
		if ( y == 0 )
		{
			string.Format("Ch%d ", i % nChannelCount + m_Control.FirstChannel ) ;
			pDC->TextOut( x, y, string  );
		}
		else
			pDC->TextOut(x, y, str);
	}
	
	if(bDigitRedraw)
	{
		CurrentIndex++;
		if(CurrentIndex==MAX_SEGMENT) CurrentIndex=0;
	}
	bDigitRedraw=FALSE;
	bDataProcessing = FALSE; // 表示数据已处理完,采集或处理线程可以操作此段缓冲区
}

void CUSBDigitView::OnDigitalVoltShowMode() 
{

}



void CUSBDigitView::OnCancelMode() 
{
	CScrollView::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}


BOOL CUSBDigitView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style=cs.style^WS_HSCROLL;	
	return CScrollView::PreCreateWindow(cs);
}



void CUSBDigitView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
}

⌨️ 快捷键说明

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