// sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "sample1.h" #include "sample1Dlg.h" #include "acces32.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) //}}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() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample1Dlg) m_blurb = _T(""); m_addressString = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); running = false; outPut = 0; outString = "0000"; inString = ""; for (int i = 0; i < 32 ; i++) { inString += "0"; } setBlurb(); m_addressString = "300"; } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample1Dlg) DDX_Control(pDX, IDC_BUTTON_START, m_startButton); DDX_Control(pDX, IDC_INPUT_PANE, m_input); DDX_Control(pDX, IDC_OUTPUT_PANE, m_output); DDX_Text(pDX, IDC_EDIT_INSTRUCTIONS, m_blurb); DDX_Text(pDX, IDC_EDIT1, m_addressString); DDV_MaxChars(pDX, m_addressString, 3); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog) //{{AFX_MSG_MAP(CSample1Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg message handlers BOOL CSample1Dlg::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 return TRUE; // return TRUE unless you set the focus to a control } void CSample1Dlg::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 CSample1Dlg::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(); } CDC *pDC; RECT clientRect; pDC = m_output.GetDC(); m_output.GetClientRect(&clientRect); pDC->SetBkColor(RGB(236, 223, 216)); pDC->DrawText(outString, &clientRect, DT_CENTER); pDC = m_input.GetDC(); pDC->SetBkColor(RGB(236, 223, 216)); m_input.GetClientRect(&clientRect); pDC->DrawText(inString, &clientRect, DT_CENTER); } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSample1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample1Dlg::OnButtonStart() { // TODO: Add your control notification handler code here char *buf; if(!running) //if we are actually starting the sample { UpdateData(true); buf = m_addressString.GetBuffer(5); sscanf(buf, "%x", &baseAddress); m_addressString.ReleaseBuffer(); SetTimer(1, 1000, NULL); running = true; } else { KillTimer(1); running = false; } } void CSample1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default char *buf; for (int i = 0; i < 4; i++) { inPut[i] = InPortB(baseAddress + i); } OutPortB(baseAddress, outPut); buf = outString.GetBuffer(5); for (i = 0; i < 4; i++) { if ((outPut >> i) % 2 == 1) buf[3 - i] = '1'; else buf[3 - i] = '0'; } outString.ReleaseBuffer(4); buf = inString.GetBuffer(33); for (int outer = 0; outer < 4; outer++) for (int inner = 0; inner < 8; inner ++) { buf[31 - ((outer * 8) + inner)] = ((inPut[outer] >> inner) % 2 == 1)?'1':'0'; } inString.ReleaseBuffer(32); switch(outPut) { case 0: outPut++; break; case 8: outPut = 0; break; default: outPut = outPut << 1; } UpdateData(false); Invalidate(); CDialog::OnTimer(nIDEvent); } void CSample1Dlg::setBlurb() { m_blurb = ""; m_blurb += "This sample program will sequentially walk a bit through all the relays. Each time it sets a new bit,\r\nthe inputs are read and the data is displayed. This demonstrates how to write to the relays and\r\nhow to use the inputs on the board."; }