📄 i2ctestdlg.cpp
字号:
// I2cTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "I2cTest.h"
#include "I2cTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include "qita.h"
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()
/////////////////////////////////////////////////////////////////////////////
// CI2cTestDlg dialog
CI2cTestDlg::CI2cTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CI2cTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CI2cTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CI2cTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CI2cTestDlg)
DDX_Control(pDX, IDC_BUTTON_OPT1, m_opt1);
DDX_Control(pDX, IDC_LIST_TEST_STATUS, m_list_status);
DDX_Control(pDX, IDC_LIST_BIT, m_list_bit);
DDX_Control(pDX, IDC_LIST_ADDRESS, m_list_address);
DDX_Control(pDX, IDC_BUTTON_SOLENOID, m_solenoid);
DDX_Control(pDX, IDC_BUTTON_RED, m_red);
DDX_Control(pDX, IDC_BUTTON_GREEN, m_green);
DDX_Control(pDX, IDC_BUTTON_EXT_CARD, m_ext_card);
DDX_Control(pDX, IDC_BUTTON_COUPLER_RELAY, m_coupler_relay);
DDX_Control(pDX, IDC_BUTTON_BASE_RELAY, m_base_relay);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CI2cTestDlg, CDialog)
//{{AFX_MSG_MAP(CI2cTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_BASE_RELAY, OnButtonBaseRelay)
ON_BN_CLICKED(IDC_CLEAR_MESSAGE, OnClearMessage)
ON_BN_CLICKED(IDC_BUTTON_COUPLER_RELAY, OnButtonCouplerRelay)
ON_BN_CLICKED(IDC_BUTTON_EXT_CARD, OnButtonExtCard)
ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet)
ON_BN_CLICKED(IDC_BUTTON_GREEN, OnButtonGreen)
ON_BN_CLICKED(IDC_BUTTON_RED, OnButtonRed)
ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
ON_BN_CLICKED(IDC_BUTTON_SET0, OnButtonSet0)
ON_BN_CLICKED(IDC_BUTTON_SET1, OnButtonSet1)
ON_BN_CLICKED(IDC_BUTTON_SOLENOID, OnButtonSolenoid)
ON_BN_CLICKED(IDC_BUTTON_OPT1, OnButtonOpt1)
ON_BN_CLICKED(IDC_BUTTON_RESET_ALL, OnButtonResetAll)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CI2cTestDlg message handlers
BOOL CI2cTestDlg::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
// TODO: Add extra initialization here
char addr[7][8]={"&H42","&H46","&H48","&H70","&H72","&H74","&H76"};
char bitnum[9][6]={"ALL","0","1","2","3","4","5","6","7"};
int i;
for(i=0;i<=6;i++)
m_list_address.AddString(addr[i]);
for(i=0;i<=8;i++)
m_list_bit.AddString(bitnum[i]);
//*********************************************************************************
return TRUE; // return TRUE unless you set the focus to a control
}
void CI2cTestDlg::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 CI2cTestDlg::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 CI2cTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CI2cTestDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
BOOL CI2cTestDlg::Get_Current_Sel()
{
int addr_index=m_list_address.GetCurSel();
selected_bitnum=m_list_bit.GetCurSel();
if(addr_index==LB_ERR||selected_bitnum==LB_ERR)
{ MessageBox("Select both address and Bitnum to execute!","Error!",MB_ICONEXCLAMATION+MB_OK);
return FALSE;
}
switch(addr_index)
{
case 0://42
selected_address=0x42;
break;
case 1://46
selected_address=0x46;
break;
case 2://48
selected_address=0x48;
break;
case 3://70
selected_address=0x70;
break;
case 4://72
selected_address=0x72;
break;
case 5://74
selected_address=0x74;
break;
case 6://76
selected_address=0x76;
break;
}
return TRUE;
}
void CI2cTestDlg::Show_Message()
{
m_list_status.AddString(MsgBuffer);
m_list_status.SetCurSel(m_list_status.GetCount()-1);
m_list_status.UpdateWindow();
return;
}
void CI2cTestDlg::OnClearMessage()
{
m_list_status.ResetContent();
ASSERT(m_list_status.GetCount() == 0);
return;
}
void CI2cTestDlg::OnButtonBaseRelay()
{
int status=Interface_Box.Read_port_bit(0x72,3);
if(status==1)
Interface_Box.I2c_bit(0,3,0x72);
if(status==0)
Interface_Box.I2c_bit(1,3,0x72);
status=Interface_Box.Read_port_bit(0x72,3);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_base_relay.SetWindowText("Base Relay is OFF");
if(status==0)
m_base_relay.SetWindowText("Base Relay is ON");
m_base_relay.UpdateWindow();
return; // TODO: Add your control notification handler code here
}
void CI2cTestDlg::OnButtonCouplerRelay()
{
int status=Interface_Box.Read_port_bit(0x72,5);
if(status==1)
Interface_Box.I2c_bit(0,5,0x72);
if(status==0)
Interface_Box.I2c_bit(1,5,0x72);
status=Interface_Box.Read_port_bit(0x72,5);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_coupler_relay.SetWindowText("Coupler Relay is OFF");
if(status==0)
m_coupler_relay.SetWindowText("Coupler Relay is ON");
m_coupler_relay.UpdateWindow();
return;
}
void CI2cTestDlg::OnButtonExtCard()
{
int status=Interface_Box.Read_port_bit(0x70,3);
if(status==1)
Interface_Box.I2c_bit(0,3,0x70);
if(status==0)
Interface_Box.I2c_bit(1,3,0x70);
status=Interface_Box.Read_port_bit(0x70,3);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_ext_card.SetWindowText("Ext Card is OFF");
if(status==0)
m_ext_card.SetWindowText("Ext Card is ON");
m_ext_card.UpdateWindow();
return;
}
void CI2cTestDlg::OnButtonGet()
{
if(!Get_Current_Sel())
return;
if(selected_bitnum==0)
{ MsgBuffer.Format("[0X%X:][%X],Status:",selected_address,Interface_Box.Read_port(selected_address));
MsgBuffer+=Interface_Box.Get_status();
}
else{
int Bit_status;
Bit_status=Interface_Box.Read_port_bit(selected_address,(selected_bitnum-1));
MsgBuffer.Format("[0X%X:][%d:][%d] Status:%s",selected_address,
selected_bitnum-1,Bit_status,Interface_Box.Get_status());
}
Show_Message();
return;
}
void CI2cTestDlg::OnButtonGreen()
{
int status=Interface_Box.Read_port_bit(0x72,1);
if(status==1)
Interface_Box.I2c_bit(0,1,0x72);
if(status==0)
Interface_Box.I2c_bit(1,1,0x72);
status=Interface_Box.Read_port_bit(0x72,1);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_green.SetWindowText("Green Lamp is OFF");
if(status==0)
m_green.SetWindowText("Green Lamp is ON");
m_green.UpdateWindow();
return;
}
void CI2cTestDlg::OnButtonRed()
{
int status=Interface_Box.Read_port_bit(0x72,0);
if(status==1)
Interface_Box.I2c_bit(0,0,0x72);
if(status==0)
Interface_Box.I2c_bit(1,0,0x72);
status=Interface_Box.Read_port_bit(0x72,0);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_red.SetWindowText("Red Lamp is OFF");
if(status==0)
m_red.SetWindowText("Red Lamp is ON");
m_red.UpdateWindow();
return;
}
void CI2cTestDlg::OnButtonRefresh()
{
//*******************************************************
int status;
//*******************************************************
status=Interface_Box.Read_port_bit(0x70,5);
if(status==1)
m_solenoid.SetWindowText("RF Solenoid is OFF");
if(status==0)
m_solenoid.SetWindowText("RF Solenoid is ON");
//********************************************************
status=Interface_Box.Read_port_bit(0x70,3);
if(status==1)
m_ext_card.SetWindowText("Ext Card is OFF");
if(status==0)
m_ext_card.SetWindowText("Ext Card is ON");
//*********************************************************
status=Interface_Box.Read_port_bit(0x72,0);
if(status==1)
m_red.SetWindowText("Red Lamp is OFF");
if(status==0)
m_red.SetWindowText("Red Lamp is ON");
//***********************************************************
status=Interface_Box.Read_port_bit(0x72,1);
if(status==1)
m_green.SetWindowText("Green Lamp is OFF");
if(status==0)
m_green.SetWindowText("Green Lamp is ON");
//**********************************************************
status=Interface_Box.Read_port_bit(0x72,5);
if(status==1)
m_coupler_relay.SetWindowText("Coupler Relay is OFF");
if(status==0)
m_coupler_relay.SetWindowText("Coupler Relay is ON");
//*************************************************************
status=Interface_Box.Read_port_bit(0x72,3);
if(status==1)
m_base_relay.SetWindowText("Base Relay is OFF");
if(status==0)
m_base_relay.SetWindowText("Base Relay is ON");
//*************************************************************
status=Interface_Box.Read_port_bit(0x46,0);
if(status==1)
m_opt1.SetWindowText("OPT1<>GND");
if(status==0)
m_opt1.SetWindowText("OPT1->GND");
return;
}
void CI2cTestDlg::OnButtonSet0()
{
int i;
if(!Get_Current_Sel())
return;
if(selected_address==0x74||selected_address==0x76)
{MessageBox("This address can only be read!","Error!",MB_ICONEXCLAMATION+MB_OK);
return;
}
if(selected_bitnum==0)
for(i=0;i<=7;i++)
{ Interface_Box.I2c_bit(0,i,selected_address);
MsgBuffer.Format("[0X%X:][%d:] Status:%s",selected_address,
i,Interface_Box.Get_status());
Show_Message();
Sleep(500);
}
else {Interface_Box.I2c_bit(0,selected_bitnum-1,selected_address);
MsgBuffer.Format("[0X%X:][%d:] Status:%s",selected_address,
selected_bitnum-1,Interface_Box.Get_status());
Show_Message();
}
return;
}
void CI2cTestDlg::OnButtonSet1()
{
int i;
if(!Get_Current_Sel())
return;
if(selected_address==0x74||selected_address==0x76)
{MessageBox("This address can only be read!","Error!",MB_ICONEXCLAMATION+MB_OK);
return;
}
if(selected_bitnum==0)
for(i=0;i<=7;i++)
{ Interface_Box.I2c_bit(1,i,selected_address);
MsgBuffer.Format("[0X%X:][%d:] Status:%s",selected_address,
i,Interface_Box.Get_status());
Show_Message();
Sleep(500);
}
else {Interface_Box.I2c_bit(1,selected_bitnum-1,selected_address);
MsgBuffer.Format("[0X%X:][%d:] Status:%s",selected_address,
selected_bitnum-1,Interface_Box.Get_status());
Show_Message();
}
return;
}
void CI2cTestDlg::OnButtonSolenoid()
{
int status=Interface_Box.Read_port_bit(0x70,5);
if(status==1)
Interface_Box.I2c_bit(0,5,0x70);
if(status==0)
Interface_Box.I2c_bit(1,5,0x70);
status=Interface_Box.Read_port_bit(0x70,5);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_solenoid.SetWindowText("RF Solenoid is OFF");
if(status==0)
m_solenoid.SetWindowText("RF Solenoid is ON");
m_solenoid.UpdateWindow();
return;
}
void CI2cTestDlg::OnButtonOpt1()
{
int status=Interface_Box.Read_port_bit(0x46,0);
if(status==1)
Interface_Box.I2c_bit(0,0,0x46);
if(status==0)
Interface_Box.I2c_bit(1,0,0x46);
status=Interface_Box.Read_port_bit(0x46,0);
MsgBuffer.Format("%s",Interface_Box.Get_status());
Show_Message();
if(status==1)
m_opt1.SetWindowText("OPT1<>GND");
if(status==0)
m_opt1.SetWindowText("OPT1->GND");
m_opt1.UpdateWindow();
return;
}
void CI2cTestDlg::OnButtonResetAll()
{
Interface_Box.clear(0x42);
MsgBuffer.Format("0X42:%s",Interface_Box.Get_status());
Show_Message();
Interface_Box.clear(0x46);
MsgBuffer.Format("0X46:%s",Interface_Box.Get_status());
Show_Message();
Interface_Box.clear(0x48);
MsgBuffer.Format("0X48:%s",Interface_Box.Get_status());
Show_Message();
Interface_Box.clear(0x70);
MsgBuffer.Format("0X70:%s",Interface_Box.Get_status());
Show_Message();
Interface_Box.clear(0x72);
MsgBuffer.Format("0X72:%s",Interface_Box.Get_status());
Show_Message();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -