// sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "sample1.h" #include "sample1Dlg.h" #include "Com.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define CLEAR_TEXT 1 //the messages are just be pushed to the list box #define AQUIRING 2 //recieving the status of the inputs #define HELLO 3 //attampting to open the POD check for hello MSG #define CONFIRM 4 //we send a command we are awaiting a CR acknowledgement #define SEARCHING 5 //we are attempting to autodetect the POD #define FOUND 6 //POD was located while attempting to autodetect #define TIMER_ID 100 CSample1Dlg *Me; TCom *Commo; void ReadCom(const char *); ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample1Dlg) m_detectString = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); Commo = new TCom; Me = this; Commo->OnReadCom = &ReadCom; aquiring = false; connected = false; } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample1Dlg) DDX_Control(pDX, IDC_START_BTN, m_startBTN); DDX_Control(pDX, IDC_VERSION_BTN, m_versionBTN); DDX_Control(pDX, IDC_STOP_BTN, m_stopBTN); DDX_Control(pDX, IDC_SEND_BTN, m_sendBTN); DDX_Control(pDX, IDC_RESEND_BTN, m_resendBTN); DDX_Control(pDX, IDC_HELLO_BTN, m_helloBTN); DDX_Control(pDX, IDC_DETECT_BTN, m_detectBTN); DDX_Control(pDX, IDC_ADDRESS_BTN, m_addressBTN); DDX_Control(pDX, IDC_LIST1, m_messages); DDX_Control(pDX, IDC_KEY_LOW, m_keyLow); DDX_Control(pDX, IDC_KEY_HIGH, m_keyHigh); DDX_Control(pDX, IDC_OUTPUT0, m_outputArray[0]); DDX_Control(pDX, IDC_OUTPUT1, m_outputArray[1]); DDX_Control(pDX, IDC_OUTPUT2, m_outputArray[2]); DDX_Control(pDX, IDC_OUTPUT3, m_outputArray[3]); DDX_Control(pDX, IDC_OUTPUT4, m_outputArray[4]); DDX_Control(pDX, IDC_OUTPUT5, m_outputArray[5]); DDX_Control(pDX, IDC_OUTPUT6, m_outputArray[6]); DDX_Control(pDX, IDC_OUTPUT7, m_outputArray[7]); DDX_Control(pDX, IDC_INPUT0, m_inputArray[0]); DDX_Control(pDX, IDC_INPUT1, m_inputArray[1]); DDX_Control(pDX, IDC_INPUT2, m_inputArray[2]); DDX_Control(pDX, IDC_INPUT3, m_inputArray[3]); DDX_Control(pDX, IDC_INPUT4, m_inputArray[4]); DDX_Control(pDX, IDC_INPUT5, m_inputArray[5]); DDX_Control(pDX, IDC_INPUT6, m_inputArray[6]); DDX_Control(pDX, IDC_INPUT7, m_inputArray[7]); DDX_Control(pDX, IDC_SETTINGS_GROUP, m_settingsGroup); DDX_Control(pDX, IDC_AUTO_GROUP, m_autoGroup); DDX_Control(pDX, IDC_CHANGE_GROUP, m_changeGroup); DDX_Control(pDX, IDC_SEND_GROUP, m_sendGroup); DDX_Control(pDX, IDC_CONNECT_BTN, m_connectBTN); DDX_Control(pDX, IDC_ADDRESS_EDIT, m_addressEdit); DDX_Control(pDX, IDC_EDIT_PORT, m_editPort); DDX_Control(pDX, IDC_EDIT_SEND, m_editSend); DDX_Text(pDX, IDC_DETECT_LABEL, m_detectString); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog) //{{AFX_MSG_MAP(CSample1Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CONNECT_BTN, OnConnectButton) ON_BN_CLICKED(IDC_SEND_BTN, OnSendButton) ON_BN_CLICKED(IDC_HELLO_BTN, OnHelloButton) ON_BN_CLICKED(IDC_VERSION_BTN, OnVersionButton) ON_BN_CLICKED(IDC_RESEND_BTN, OnResendButton) ON_BN_CLICKED(IDC_ADDRESS_BTN, OnAddressButton) ON_BN_CLICKED(IDC_START_BTN, OnStartButton) ON_WM_TIMER() ON_BN_CLICKED(IDC_DETECT_BTN, OnDetectButton) ON_BN_CLICKED(IDC_STOP_BTN, OnStopButton) ON_BN_CLICKED(IDC_OUTPUT0, OnOutput0) ON_BN_CLICKED(IDC_OUTPUT1, OnOutput1) ON_BN_CLICKED(IDC_OUTPUT2, OnOutput2) ON_BN_CLICKED(IDC_OUTPUT3, OnOutput3) ON_BN_CLICKED(IDC_OUTPUT4, OnOutput4) ON_BN_CLICKED(IDC_OUTPUT5, OnOutput5) ON_BN_CLICKED(IDC_OUTPUT6, OnOutput6) ON_BN_CLICKED(IDC_OUTPUT7, OnOutput7) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg message handlers BOOL CSample1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 m_autoGroup.EnableWindow(FALSE); m_detectBTN.EnableWindow(FALSE); m_stopBTN.EnableWindow(FALSE); m_changeGroup.EnableWindow(FALSE); m_addressBTN.EnableWindow(FALSE); m_sendGroup.EnableWindow(FALSE); m_sendBTN.EnableWindow(FALSE); m_helloBTN.EnableWindow(FALSE); m_versionBTN.EnableWindow(FALSE); m_resendBTN.EnableWindow(FALSE); m_startBTN.EnableWindow(FALSE); m_addressEdit.EnableWindow(FALSE); m_editSend.EnableWindow(FALSE); inChar = outChar = 0; m_editPort.SetWindowText("5"); UpdateData(false); return TRUE; // return TRUE unless you set the focus to a control } // 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() { CDC *pDC; //this is going to point to each of the "LEDS" contexts so they can be filled RECT rect; //this is going to hold the dimensions of each of the "LEDS" CBrush onBrush(RGB(0x00, 0xff, 0x00)); //the on brush is green CBrush offBrush(RGB(0xff, 0x00, 0x00)); //the off brush is red pDC = m_keyHigh.GetDC(); m_keyHigh.GetClientRect(&rect); pDC->FillRect(&rect, &onBrush); pDC = m_keyLow.GetDC(); m_keyLow.GetClientRect(&rect); pDC->FillRect(&rect, &offBrush); for (int i = 0; i < 8; i++) //cycle through the inputs and outputs and set them to the right color { pDC = m_inputArray[i].GetDC(); m_inputArray[i].GetClientRect(&rect); if ((inChar >> i) % 2) pDC->FillRect(&rect, &onBrush); else pDC->FillRect(&rect, &offBrush); pDC = m_outputArray[i].GetDC(); m_outputArray[i].GetClientRect(&rect); if ((outChar >> i) % 2) pDC->FillRect(&rect, &onBrush); else pDC->FillRect(&rect, &offBrush); } 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 CSample1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample1Dlg::receiveMsg(const char *msg) { switch(mode) { case AQUIRING: if (strlen(msg) != 0) received(msg); break; case CLEAR_TEXT: if (strlen(msg) != 0) m_messages.SetTopIndex(m_messages.InsertString(-1, msg)); UpdateData(false); break; case HELLO: mode = CLEAR_TEXT; //we have recieved a message from the card so we can light up the DLG now m_changeGroup.EnableWindow(TRUE); m_addressBTN.EnableWindow(TRUE); m_sendGroup.EnableWindow(TRUE); m_sendBTN.EnableWindow(TRUE); m_helloBTN.EnableWindow(TRUE); m_versionBTN.EnableWindow(TRUE); m_resendBTN.EnableWindow(TRUE); m_startBTN.EnableWindow(TRUE); m_addressEdit.EnableWindow(TRUE); m_editSend.EnableWindow(TRUE); m_autoGroup.EnableWindow(FALSE); //we are connected to the pod so it doesn't make sense to autodetect m_detectBTN.EnableWindow(FALSE); m_startBTN.EnableWindow(FALSE); connected = true; break; case CONFIRM: mode = AQUIRING; break; case SEARCHING: mode = FOUND; connected = true; m_changeGroup.EnableWindow(TRUE); m_addressBTN.EnableWindow(TRUE); m_sendGroup.EnableWindow(TRUE); m_sendBTN.EnableWindow(TRUE); m_helloBTN.EnableWindow(TRUE); m_versionBTN.EnableWindow(TRUE); m_resendBTN.EnableWindow(TRUE); m_startBTN.EnableWindow(TRUE); m_addressEdit.EnableWindow(TRUE); m_editSend.EnableWindow(TRUE); m_autoGroup.EnableWindow(FALSE); //we are connected to the pod so it doesn't make sense to autodetect m_detectBTN.EnableWindow(FALSE); m_startBTN.EnableWindow(FALSE); break; } } void CSample1Dlg::OnConnectButton() { BYTE port; char buf[3]; if(Commo->Connected) { drop(); m_connectBTN.SetWindowText("Connect"); } else { UpdateData(true); /* buf = m_editPort.GetBuffer(4); sscanf(buf, "%d", &port); m_editPort.ReleaseBuffer(); */ m_editPort.GetWindowText(buf, 2); sscanf(buf, "%d", &port); mode = HELLO; if (Commo->OpenCom(port) == false) { MessageBox("Could not Open Com Port", "Error"); mode = CLEAR_TEXT; } else { Commo->WriteCom("H"); //since we have opened the com port, but there is a chance the POD is not on address 0 //we should open up the autodetect group for them m_autoGroup.EnableWindow(TRUE); m_detectBTN.EnableWindow(TRUE); m_stopBTN.EnableWindow(TRUE); m_connectBTN.SetWindowText("Disconnect"); } } } void CSample1Dlg::OnSendButton() { char buf[16]; //15 characters is far more than any valid POD command m_editSend.GetWindowText(buf, 15); m_editSend.SetFocus(); m_editSend.SetSel(0, -1, TRUE); Commo->WriteCom(buf); } void CSample1Dlg::OnVersionButton() { Commo->WriteCom("V"); } void CSample1Dlg::OnHelloButton() { Commo->WriteCom("H"); } void CSample1Dlg::OnResendButton() { Commo->WriteCom("N"); } void CSample1Dlg::OnAddressButton() { char command[10]; char buf[3]; m_addressEdit.GetWindowText(buf, 3); (strlen(buf) == 1) ? strcpy(command, "POD=0") : strcpy(command, "POD="); //the address must be two characters strcat(command, buf); Commo->WriteCom(command); } void CSample1Dlg::OnStartButton() { if(!aquiring) { aquiring = true; SetTimer(TIMER_ID, 50, NULL); m_startBTN.SetWindowText("STOP"); } else { aquiring = false; m_startBTN.SetWindowText("Start"); } } void CSample1Dlg::drop() { Commo->CloseCom(); //either we are closing the connection or it has been lost connected = false; m_autoGroup.EnableWindow(FALSE); m_detectBTN.EnableWindow(FALSE); m_stopBTN.EnableWindow(FALSE); m_changeGroup.EnableWindow(FALSE); m_addressBTN.EnableWindow(FALSE); m_sendGroup.EnableWindow(FALSE); m_sendBTN.EnableWindow(FALSE); m_helloBTN.EnableWindow(FALSE); m_versionBTN.EnableWindow(FALSE); m_resendBTN.EnableWindow(FALSE); m_startBTN.EnableWindow(FALSE); m_addressEdit.EnableWindow(FALSE); m_editSend.EnableWindow(FALSE); } void CSample1Dlg::received(const char *msg) { int temp; sscanf(msg, "%x", &temp); inChar = temp; Invalidate(false); if (aquiring) SetTimer(TIMER_ID, 10, NULL); } void CSample1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default KillTimer(TIMER_ID); // char command[3]; mode = CONFIRM; sendOutChar(); while (mode == CONFIRM) { ProcessDialogMessages(); } Commo->WriteCom("I"); CDialog::OnTimer(nIDEvent); } void CSample1Dlg::OnDetectButton() { mode = SEARCHING; CTimeSpan wait(0, 0 , 0, 1); CTime startTime; char command[7]; int count = 1; bool found = false; quitSearch = false; do { sprintf(command, "!%02X", count); Commo->WriteCom(command); m_detectString = "Searching at Address: "; m_detectString.Insert(strlen("Searching at Address: "), &(command[1])); startTime = CTime::GetCurrentTime(); UpdateData(false); Sleep(100); while ((mode!= FOUND) && (startTime + wait > CTime::GetCurrentTime())) { ProcessDialogMessages(); } if (mode == FOUND) found = true; else count ++; }while (count != 256 && found != true && quitSearch != true); if (found) { sprintf(command, "%02X", count); m_addressEdit.SetWindowText(command); mode = CLEAR_TEXT; } else if (count == 256) { MessageBox("All addresses searched. POD not found.", "No Pod"); } } void CSample1Dlg::OnStopButton() { quitSearch = true; } BOOL CSample1Dlg::ProcessDialogMessages() { Sleep(0); // Yield to other // system processes // Process the message queue MSG dlgMsg; if (::PeekMessage (&dlgMsg, NULL, 0, 0, PM_NOREMOVE)) // If there are // pending messages { if (!AfxGetApp ()->PumpMessage ()) { // Allow them to be // processed ::PostQuitMessage (0); // When no more // messages exist, // return WM_QUIT // should return 0 return TRUE; // Return the caller, // indicating that // we just processed // a Message and that // there may be more } } // Simulate idle processing. LONG lIdle = 0; while (AfxGetApp ()->OnIdle (lIdle++)); return FALSE; // Return to Caller, // saying that we // did not process // any messages } void CSample1Dlg::OnOutput0() { if(connected) { outChar = outChar ^ 0x1; Invalidate(FALSE); if (!aquiring) //if we are aquiring an async output would be bad instead we let the aquire loop do it sendOutChar(); } } void CSample1Dlg::OnOutput1() { if(connected) { outChar = outChar ^ 0x2; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::OnOutput2() { if(connected) { outChar = outChar ^ 0x4; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::OnOutput3() { if(connected) { outChar = outChar ^ 0x8; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::OnOutput4() { if(connected) { outChar = outChar ^ 0x10; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::OnOutput5() { if(connected) { outChar = outChar ^ 0x20; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::OnOutput6() { if(connected) { outChar = outChar ^ 0x40; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::OnOutput7() { if(connected) { outChar = outChar ^ 0x80; Invalidate(FALSE); if (!aquiring) sendOutChar(); } } void CSample1Dlg::sendOutChar() { char command[4]; sprintf(command, "o%02x", outChar); Commo->WriteCom(command); } void ReadCom(const char *data) { Me->receiveMsg(data); }