📄 iicdlg.cpp
字号:
// CIIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "IIC.h"
#include "IICDlg.h"
#include "parallel.h"
#include <fstream>
#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)
//}}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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIICDlg dialog
CIICDlg::CIICDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIICDlg::IDD, pParent)
{
/*
for(int i=0;i<8;i++)
m_bit[i] = FALSE;
*/
//{{AFX_DATA_INIT(CIICDlg)
m_strRegName = _T("");
m_strIICAddr = _T("");
m_strRegAddr = _T("");
m_strRegValue = _T("");
m_strNote = _T("");
m_bHex = TRUE;
m_iWriteLines = 0;
m_iReadLines = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pBit[0] = &m_bit0;pBit[1] = &m_bit1;pBit[2] = &m_bit2;pBit[3] = &m_bit3;
pBit[4] = &m_bit4;pBit[5] = &m_bit5;pBit[6] = &m_bit6;pBit[7] = &m_bit7;
m_bSendAll = false;
pPort = NULL;
}
void CIICDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CIICDlg)
DDX_Control(pDX, IDC_EDIT_TMP_LINE, m_tmpLine);
DDX_Control(pDX, IDC_EDIT5, m_ReadBytes);
DDX_Control(pDX, IDC_SPIN1, m_spin1);
DDX_Control(pDX, IDC_EDIT_READ_LOG, m_ReadLog);
DDX_Control(pDX, IDC_EDIT_WRITE_LOG, m_WriteLog);
DDX_Control(pDX, IDC_BIT7, m_bit7);
DDX_Control(pDX, IDC_BIT6, m_bit6);
DDX_Control(pDX, IDC_BIT5, m_bit5);
DDX_Control(pDX, IDC_BIT4, m_bit4);
DDX_Control(pDX, IDC_BIT3, m_bit3);
DDX_Control(pDX, IDC_BIT2, m_bit2);
DDX_Control(pDX, IDC_BIT1, m_bit1);
DDX_Control(pDX, IDC_BIT0, m_bit0);
DDX_Control(pDX, IDC_SCROLLBAR1, m_scroll);
DDX_Control(pDX, IDC_LIST4, m_list);
DDX_Text(pDX, IDC_EDIT_STRING, m_strRegName);
DDX_Text(pDX, IDC_EDIT1, m_strIICAddr);
DDX_Text(pDX, IDC_EDIT2, m_strRegAddr);
DDX_Text(pDX, IDC_EDIT3, m_strRegValue);
DDX_Text(pDX, IDC_EDIT4, m_strNote);
DDX_Check(pDX, IDC_CHECK9, m_bHex);
//}}AFX_DATA_MAP
/*
DDX_Check(pDX, IDC_CHECK1, m_bit[0]);
DDX_Check(pDX, IDC_CHECK2, m_bit[1]);
DDX_Check(pDX, IDC_CHECK3, m_bit[2]);
DDX_Check(pDX, IDC_CHECK4, m_bit[3]);
DDX_Check(pDX, IDC_CHECK5, m_bit[4]);
DDX_Check(pDX, IDC_CHECK6, m_bit[5]);
DDX_Check(pDX, IDC_CHECK7, m_bit[6]);
DDX_Check(pDX, IDC_CHECK8, m_bit[7]);
*/
}
BEGIN_MESSAGE_MAP(CIICDlg, CDialog)
//{{AFX_MSG_MAP(CIICDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_MENU_FILE_SAVE, OnMenuFileSave)
ON_WM_HSCROLL()
ON_NOTIFY(NM_CLICK, IDC_LIST4, OnClickList4)
ON_NOTIFY(LVN_KEYDOWN, IDC_LIST4, OnKeydownList4)
ON_BN_CLICKED(IDC_CHECK9, OnCheck9)
ON_COMMAND(ID_MENU_FILE_OPEN, OnMenuFileOpen)
ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LIST4, OnEndlabeleditList4)
ON_EN_CHANGE(IDC_EDIT_IIC_ADDR, OnChangeEditIicAddr)
ON_EN_CHANGE(IDC_EDIT_NOTE, OnChangeEditNote)
ON_EN_CHANGE(IDC_EDIT_REG_ADDR, OnChangeEditRegAddr)
ON_EN_CHANGE(IDC_EDIT_REG_NAME, OnChangeEditRegName)
ON_EN_CHANGE(IDC_EDIT_REG_VALUE, OnChangeEditRegValue)
ON_COMMAND(ID_MENU_FILE_SAVE_AS, OnMenuFileSaveAs)
ON_COMMAND(ID_MENU_HELP, OnMenuHelp)
ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
ON_BN_CLICKED(IDC_BIT0, OnBit0)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_CLEAR_WRITE_LOG, OnClearWriteLog)
ON_BN_CLICKED(IDC_CLEAR_READ_LOG, OnClearReadLog)
ON_COMMAND(ID_MENU_FILE_OPEN_IML, OnMenuFileOpenIml)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
ON_BN_CLICKED(IDC_BUTTON_TX, OnButtonTx)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_TX_RX, OnButtonTxRx)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BIT1, OnBit0)
ON_BN_CLICKED(IDC_BIT2, OnBit0)
ON_BN_CLICKED(IDC_BIT3, OnBit0)
ON_BN_CLICKED(IDC_BIT4, OnBit0)
ON_BN_CLICKED(IDC_BIT5, OnBit0)
ON_BN_CLICKED(IDC_BIT6, OnBit0)
ON_BN_CLICKED(IDC_BIT7, OnBit0)
ON_NOTIFY(HDN_TRACK, IDC_LIST4, OnTrackList4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndLabelEdit)
/////////////////////////////////////////////////////////////////////////////
// CIICDlg message handlers
BOOL CIICDlg::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);
}
}
// 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
m_scroll.SetScrollRange(0, 255);
m_spin1.SetRange(1,255);
m_spin1.SetPos(2);
m_ReadBytes.SetWindowText("2");
OnClearWriteLog();
OnClearReadLog();
// TODO: Add extra initialization here
#if 1
pPort = new(parallel);
pPort->InitPort();
#endif
m_iIICAddr = 0x88;
m_iRegAddr = 0x01;
m_iRegValue = 0x80;
m_iScrollRow = 0;
m_list.m_bHex = m_bHex;
m_list.m_Data.AddRow();
m_list.Show();
ShowValue();
ResetBit();
m_bBusReady = pPort->CheckBus();
this->ShowBusState();
SetTimer(2,1000,NULL);
//int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1 );
return TRUE; // return TRUE unless you set the focus to a control
}
void CIICDlg::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 CIICDlg::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 CIICDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CIICDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if(pPort != NULL) delete pPort;
KillTimer(2);
return CDialog::DestroyWindow();
}
void CIICDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
INT max;
INT min;
INT nCurPos;
// Get current scroll position
nCurPos = pScrollBar->GetScrollPos();
// Get scroll range
pScrollBar->GetScrollRange(&min, &max);
// Determine which notification is being sent
switch (nSBCode)
{
case SB_LINEDOWN:
nCurPos += 1; // scroll arrow click - increment by 1
break;
case SB_LINEUP:
nCurPos -= 1; // scroll arrow click - decrement by 1
break;
case SB_PAGEDOWN: // scroll bar click - increment by 10
nCurPos += 10;
break;
case SB_PAGEUP: // scroll bar click - decrement by 10
nCurPos -= 10;
break;
case SB_THUMBTRACK: // follow the thumb
nCurPos = nPos;
break;
case SB_BOTTOM:
nCurPos = 0xFF;
break;
case SB_TOP:
nCurPos = 0;
break;
case SB_ENDSCROLL:
break;
default:
break;
}
if(nCurPos>max) nCurPos=max;
if(nCurPos<min) nCurPos=min;
pScrollBar->SetScrollPos(nCurPos);
SaveRegValue(nCurPos);
SendRowData(m_iScrollRow);
if(m_bHex)
m_strRegValue.Format("%02X",nCurPos);
else
m_strRegValue.Format("%03d",nCurPos);
for(int i =0;i<8;i++){
pBit[i]->SetBit((nCurPos>>i)&0x01);
}
UpdateData(FALSE);
// UpdateScroll();
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
pScrollBar->SetFocus();
}
void CIICDlg::OnClickList4(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = m_list.GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
SelectRow(nItem);
}
}
*pResult = 0;
}
void CIICDlg::OnKeydownList4(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
switch (pLVKeyDow->wVKey ){
case 0x28:
break;
case 0x26:
break;
default:
break;
}
*pResult = 0;
}
void CIICDlg::ShowValue()
{
m_iIICAddr = m_list.m_Data.GetValue(m_iScrollRow,0);
m_iRegAddr = m_list.m_Data.GetValue(m_iScrollRow,1);
m_iRegValue = m_list.m_Data.GetValue(m_iScrollRow);
m_strRegName = m_list.m_Data.GetName(m_iScrollRow);
m_strNote = m_list.m_Data.GetNote(m_iScrollRow);
CString str;
if(m_bHex)
str="%02X";
else
str="%03d";
m_strIICAddr.Format(str,m_iIICAddr);
m_strRegAddr.Format(str,m_iRegAddr);
m_strRegValue.Format(str,m_iRegValue);
m_scroll.SetScrollPos(m_iRegValue);
for(int i =0;i<8;i++){
pBit[i]->SetBit((m_iRegValue>>i)&0x01);
}
UpdateData(FALSE);
// m_list.SetSelectionMark(m_iScrollRow);
m_list.SetItemState(m_iScrollRow, LVIS_SELECTED, LVIS_SELECTED);
// SendRowData(m_iScrollRow);
}
void CIICDlg::ResetBit()
{
/* for(int i=0;i<8;i++)
m_bit[i] = 2;*/
UpdateData(FALSE);
}
void CIICDlg::OnCheck9()
{
// TODO: Add your control notification handler code here
UpdateData();
m_list.m_bHex = m_bHex;
ShowValue();
m_list.Show();
}
void CIICDlg::OnMenuFileOpen()
{
// TODO: Add your command handler code here
CString filename;
CFileDialog opendlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"文本文件|*.txt|All Files(*.*)|*.*||",NULL);
if(opendlg.DoModal()==IDOK)
filename=opendlg.GetPathName();
else
return;
CStdioFile file;
file.Open(filename,CFile::modeRead);
CString szLine("");
if (!file.ReadString(szLine)){
AfxMessageBox("打开错误!");
return;
}
szLine.TrimLeft();
szLine.TrimRight();
if(szLine.GetLength()!=2){
AfxMessageBox("文件格式错误!");
return;
}
/* int addr;
if(hexNumberToInt(szLine,addr))
m_iIICAddrDeafault = addr;
*/
CString addr = szLine + " ";//老版本文件第一行是器件地址
if(szLine.Compare("##") == 0)
addr = "";
m_list.m_Data.ClearAll();
while(file.ReadString(szLine)){
if(szLine.GetLength()<5) continue;
if(szLine.GetAt(0)=='#') continue;
szLine = addr + szLine;
szLine.TrimLeft();
m_list.m_Data.AddRow(szLine);
}
m_list.Show();
m_iScrollRow = 0;
ShowValue();
m_list.SetFocus();
m_FileName = filename;
}
void CIICDlg::OnMenuFileOpenIml()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -