📄 procdlg.cpp
字号:
// ProcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Proc.h"
#include "ProcDlg.h"
#include "Program.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProcDlg dialog
CProcDlg::CProcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CProcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CProcDlg)
m_IPins = 0;
m_strChipID = _T("");
m_blVerify = FALSE;
m_strCurFile = _T("No File");
blOpenF = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CProcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProcDlg)
DDX_Control(pDX, IDC_PROGRESS, m_progress);
DDX_Radio(pDX, IDC_WMPIN28, m_IPins);
DDX_Text(pDX, IDC_DISPID, m_strChipID);
DDX_Check(pDX, IDC_VERIFY, m_blVerify);
DDX_Text(pDX, IDC_CURSTATUS, m_strCurFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProcDlg, CDialog)
//{{AFX_MSG_MAP(CProcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SAVEFILE, OnSavefile)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_WMPIN28, OnWmpin28)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_WGPIN64, OnWgpin64)
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
ON_BN_CLICKED(IDC_WMPIN40, OnWmpin40)
ON_BN_CLICKED(IDC_WGPIN40, OnWgpin40)
ON_BN_CLICKED(IDC_VERIFY, OnVerify)
ON_BN_CLICKED(IDC_WRITE, OnWrite)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_READID, OnReadid)
ON_EN_CHANGE(IDC_DISPID, OnDispid)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_STEPIT, OnStepIt)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProcDlg message handlers
BOOL CProcDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
m_CProgram.Tvia_InitBurner();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// Button close
// add ico picture into bottoN
m_btnR.SubclassDlgItem(IDC_READ, this);
m_btnR.SetIcon(IDI_CHIPRD);
m_btnR.SetActiveBgColor(RGB(246, 238, 230));
m_btnW.SubclassDlgItem(IDC_WRITE, this);
m_btnW.SetIcon(IDI_CHIPWR);
m_btnW.SetActiveBgColor(RGB(246, 238, 230));
m_btnID.SubclassDlgItem(IDC_CHIPID, this);
m_btnID.SetIcon(IDI_CHIPID);
m_btnID.SetActiveBgColor(RGB(246, 238, 230));
m_btnE.SubclassDlgItem(IDC_EXIT, this);
m_btnE.SetIcon(IDI_EXIT);
m_btnE.SetActiveBgColor(RGB(246, 238, 230));
m_btnO.SubclassDlgItem(IDC_OPENFILE, this);
m_btnO.SetIcon(IDI_OPENFILES);
m_btnO.SetActiveBgColor(RGB(246, 238, 230));
m_btnS.SubclassDlgItem(IDC_SAVEFILE, this);
m_btnS.SetIcon(IDI_SAVEFILES);
m_btnS.SetActiveBgColor(RGB(246, 238, 230));
m_progress.SetRange(0, 80); //set progress range and rate
m_progress.SetStep(10); // (100 - 0)/ 8
m_pDataBuf = (BYTE *)new BYTE[MAXBUFFER];
if (m_pDataBuf == NULL)
{
AfxMessageBox("Error : Allocate Memroy! ");
return TRUE;
}
WORD i=0;
for (i; i<MAXBUFFER; i++)
{
m_pDataBuf[i] = 0xff; //clear buffer
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CProcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CProcDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CProcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
static char BASED_CODE szFilter[] = "Data Files (*.bin; *.hex)|*.bin;*.hex||";
void CProcDlg::OnSavefile()
{
// TODO: Add your control notification handler code here
CFileDialog FDlg(FALSE,NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
if(IDOK == FDlg.DoModal())
{
CFile MyFile;
m_strCurFile = FDlg.GetPathName();
if( !MyFile.Open(m_strCurFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary) )
{
AfxMessageBox("Can't open the file!");
return ;
}
MyFile.Write(m_pDataBuf, wFileSize);
UpdateData(FALSE);
MyFile.Close();
}
}
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CProcDlg::OnExit()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void CProcDlg::OnWmpin28()
{
// TODO: Add your control notification handler code here
m_CProgram.UpdataStatus(WM100028PINS, 0x2000);
UpdateData();
}
void CProcDlg::OnWmpin40()
{
// TODO: Add your control notification handler code here
m_CProgram.UpdataStatus(WM100040PINS, 0x2000);
UpdateData();
}
void CProcDlg::OnWgpin40()
{
// TODO: Add your control notification handler code here
m_CProgram.UpdataStatus(WG200040PINS, 0x4000);
UpdateData();
}
void CProcDlg::OnWgpin64()
{
// TODO: Add your control notification handler code here
m_CProgram.UpdataStatus(WG200064PINS, 0x4000);
UpdateData();
}
void CProcDlg::OnRead()
{
// TODO: Add your control notification handler code here
wFileSize = m_CProgram.Tvia_ReadDataFromFlash8K(m_pDataBuf);
AfxMessageBox("Data have been read out from flash successfully!");
m_progress.SetPos(0); //Clear the progress to 0
m_strCurFile="No File";
UpdateData(FALSE);
}
DWORD CProcDlg::H2D(BYTE *s)
{
unsigned long l = 0UL;
register char c;
for ( ; (c = *s)!=0; s++)
{
if (c >= '0' && c <= '9')
{
l = l * 16 + c - '0';
}
else if (c >= 'A' && c <= 'F')
{
l = l * 16 + c - 'A' + 10;
}
else if (c >= 'a' && c <= 'f')
{
l = l * 16 + c - 'a' + 10;
}
}
return l;
}
char CProcDlg::Hex2Bin( WORD * pDataLength, BYTE * TmpDataBuf )
{
unsigned short i = 0;
char j = 0;
BYTE TmpS[] = {0};
struct HexFormat{
BYTE bLength;
WORD wFromAddr;
BYTE bEOF;
}DataFormat = {0, 0, 0};
while(i < (BUF1KBYTES-0x32))
{
if(TmpDataBuf[i++] == ':') //:
{
TmpS[0] = TmpDataBuf[i++];
TmpS[1] = TmpDataBuf[i++];
TmpS[2] = '\0';
DataFormat.bLength = (unsigned char)H2D(TmpS);
TmpS[0] = TmpDataBuf[i++];
TmpS[1] = TmpDataBuf[i++];
TmpS[2] = TmpDataBuf[i++];
TmpS[3] = TmpDataBuf[i++];
TmpS[4] = '\0';
DataFormat.wFromAddr = (unsigned short)H2D(TmpS);
TmpS[0] = TmpDataBuf[i++];
TmpS[1] = TmpDataBuf[i++];
TmpS[2] = '\0';
DataFormat.bEOF = (unsigned char)H2D(TmpS);
if(DataFormat.bEOF == 0x00)
{
for (j=0; j<DataFormat.bLength; j++)
{
TmpS[0] = TmpDataBuf[i++];
TmpS[1] = TmpDataBuf[i++];
TmpS[2] = '\0';
m_pDataBuf[DataFormat.wFromAddr++] = (unsigned char)H2D(TmpS);
* pDataLength = DataFormat.wFromAddr;
}
}
else
{
return -1;
}
}
}
j = 0;
for (; i<BUF1KBYTES; i++)
{
TmpDataBuf[j++] = TmpDataBuf[i];
}
return (j);
}
void CProcDlg::OnOpenfile()
{
// TODO: Add your control notification handler code here
CFileDialog FDlg(TRUE,NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
if ( IDOK == FDlg.DoModal() )
{
CFile MyFile;
// unsigned short i;
m_strCurFile = FDlg.GetPathName();
if ( !MyFile.Open(m_strCurFile, CFile::modeRead) )
{
AfxMessageBox("Cannot open the file!");
return ;
}
wFileSize = (WORD)MyFile.GetLength();
CString strExtName = FDlg.GetFileExt();
strExtName.MakeUpper();
if( strExtName == "HEX" ) //judge *.hex or *.bin
{
char bBufRemainedSize = 0;
BYTE * m_pHexDataBuf = (BYTE *) new BYTE [BUF1KBYTES];
if (m_pHexDataBuf == NULL)
{
AfxMessageBox("Error : Allocate Memroy! ");
return;
}
do{
MyFile.Read((BYTE *)m_pHexDataBuf+bBufRemainedSize, BUF1KBYTES-bBufRemainedSize);
bBufRemainedSize = Hex2Bin( &wFileSize, m_pHexDataBuf );
}while(bBufRemainedSize != -1);
delete[] m_pHexDataBuf;
m_pHexDataBuf = NULL;
}
else //*.bin
{
MyFile.Read((BYTE *)m_pDataBuf, wFileSize);
}
blOpenF = TRUE;
UpdateData(FALSE);
MyFile.Close();
}
}
void CProcDlg::OnVerify()
{
// TODO: Add your control notification handler code here
UpdateData();
}
void CProcDlg::OnWrite()
{
// TODO: Add your control notification handler code here
if(!blOpenF)
{
AfxMessageBox("First open file please!");
return;
}
BYTE bResult = m_CProgram.Tvia_WriteDataIntoFlash8K(wFileSize, m_pDataBuf);
switch (bResult)
{
case 0:
break;
case 1:
AfxMessageBox("Fail to erase the flash!");
return;
case 2:
AfxMessageBox("Error: The file is empty!");
return;
case 3:
default:
AfxMessageBox("Error: The file is too large!");
return ;
}
if (m_blVerify)
{
WORD wDiffNum=0;
unsigned char * TmpBuf = (unsigned char *)new char[MAXBUFFER];
if (TmpBuf == NULL)
{
AfxMessageBox("Error : Allocate Memroy! ");
return;
}
WORD Unvalid = m_CProgram.Tvia_ReadDataFromFlash8K(TmpBuf);
for (WORD i=0; i<wFileSize; i++)
{
if(TmpBuf[i] != m_pDataBuf[i])
wDiffNum++;
}
delete[] TmpBuf;
if(wDiffNum)
{
char Tmpbuff[40];
CString strDispDiff;
sprintf( Tmpbuff, "There are 0x%x bytes Error in verification", wDiffNum );
strDispDiff = Tmpbuff;
AfxMessageBox( strDispDiff );
// AfxMessageBox("There are %d bytes in verification", wDiffNum);
}
else
AfxMessageBox("Data have been wroten in flash successfully!\n\t and pass the verification!");
m_progress.SetPos(0); // Clear the progress to 0
return;
}
else
{
AfxMessageBox("Data have been wroten in flash successfully!");
m_progress.SetPos(0); // Clear the progress to 0
return;
}
}
void CProcDlg::OnDestroy()
{
CDialog::OnDestroy();
delete[] m_pDataBuf; //
m_pDataBuf = NULL;
}
void CProcDlg::OnReadid()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
BYTE IDBuf[3];
BYTE bResult = m_CProgram.Tvia_ReadChipID(IDBuf);
switch(bResult)
{
case WM100040PINS:
m_strChipID.Format("%2X %2X %2X",IDBuf[0],IDBuf[1],IDBuf[2]);
break;
case WM100028PINS:
m_strChipID.Format("%2X %2X",IDBuf[0],IDBuf[1]);
break;
case WG200040PINS:
case WG200064PINS:
default:
break;
}
UpdateData(FALSE);
}
void CProcDlg::OnDispid()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
LRESULT CProcDlg::OnStepIt(WPARAM wParam, LPARAM lParam)
{
m_progress.StepIt();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -