Header
Product Comparison: USB I2C | USB SPI | USB GPIO
  • My Account
  • Shopping Cart
  • Products
  • My Account
  • About
  • Products
  • PC-I2C/SPI/GPIO Adapters
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

D:/_sources/u2c_12/ControlPanel/I2cWriteBar.cpp

Go to the documentation of this file.
00001 // Copyright (C)2006 Diolan ( http://www.diolan.com )
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation
00006 //
00007 // This program is distributed in the hope that it will be useful,
00008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 // GNU General Public License for more details.
00011 
00012 
00013 #include "stdafx.h"
00014 #include "controlpanel.h"
00015 #include "I2cWriteBar.h"
00016 
00017 
00018 #ifdef _DEBUG
00019 #define new DEBUG_NEW
00020 #undef THIS_FILE
00021 static char THIS_FILE[] = __FILE__;
00022 #endif
00023 
00025 // CI2cWriteBar dialog
00026 
00027 
00028 CI2cWriteBar::CI2cWriteBar()
00029         : CDialogBar()
00030 {
00031         //{{AFX_DATA_INIT(CI2cWriteBar)
00032         m_strSlaveAddr = _T("");
00033         m_strData = _T("");
00034         m_strMemAddr = _T("");
00035         m_nMemAddrLen = -1;
00036         //}}AFX_DATA_INIT
00037 }
00038 
00039 
00040 void CI2cWriteBar::DoDataExchange(CDataExchange* pDX)
00041 {
00042         //{{AFX_DATA_MAP(CI2cWriteBar)
00043         DDX_Control(pDX, IDC_WRITE_SLAVE_ADDR, m_ctrlSlaveAddr);
00044         DDX_Control(pDX, IDC_WRITE_MEM_ADDR, m_ctrlMemAddr);
00045         DDX_Control(pDX, IDC_WRITE_DATA, m_ctrlData);
00046         DDX_CBString(pDX, IDC_WRITE_SLAVE_ADDR, m_strSlaveAddr);
00047         DDX_Text(pDX, IDC_WRITE_DATA, m_strData);
00048         DDX_Text(pDX, IDC_WRITE_MEM_ADDR, m_strMemAddr);
00049         DDX_CBIndex(pDX, IDC_WRITE_MEM_ADDR_LEN, m_nMemAddrLen);
00050         //}}AFX_DATA_MAP
00051 }
00052 
00053 
00054 BOOL CI2cWriteBar::Create(CWnd* pParentWnd)
00055 {
00056         if (!CDialogBar::Create(pParentWnd, IDD, CBRS_TOP | CBRS_TOOLTIPS | CBRS_FLYBY |
00057                 CBRS_GRIPPER, IDD))
00058         {
00059                 TRACE("CDialogBar::Create failed in CI2cWriteBar::Create");
00060                 return FALSE;
00061         }
00062         SetWindowText("I2cWrite Bar");
00063         m_nMemAddrLen = 0;
00064         UpdateData(FALSE);
00065         return TRUE;
00066 
00067 }
00068 
00069 BEGIN_MESSAGE_MAP(CI2cWriteBar, CDialogBar)
00070         //{{AFX_MSG_MAP(CI2cWriteBar)
00071         //}}AFX_MSG_MAP
00072         ON_UPDATE_COMMAND_UI(IDC_I2C_WRITE, OnUpdateI2cWrite)
00073 END_MESSAGE_MAP()
00074 
00076 // CI2cWriteBar message handlers
00077 
00078 void CI2cWriteBar::SaveCurrentValuesInHistory()
00079 {
00080         UpdateData(TRUE);
00081         int CurIndex;
00082         if (CB_ERR != (CurIndex = m_ctrlData.FindStringExact(-1, m_strData)))
00083                 m_ctrlData.DeleteString(CurIndex);
00084         m_ctrlData.InsertString(0, m_strData);
00085         if (CB_ERR != (CurIndex = m_ctrlMemAddr.FindStringExact(-1, m_strMemAddr)))
00086                 m_ctrlMemAddr.DeleteString(CurIndex);
00087         m_ctrlMemAddr.InsertString(0, m_strMemAddr);
00088         if (CB_ERR == m_ctrlSlaveAddr.FindStringExact(-1, m_strSlaveAddr))
00089                 m_ctrlSlaveAddr.AddString(m_strSlaveAddr);
00090         UpdateData(FALSE);
00091 }
00092 
00093 void CI2cWriteBar::FillSlaveList(PU2C_SLAVE_ADDR_LIST pSlaveList)
00094 {
00095         m_ctrlSlaveAddr.ResetContent();
00096 
00097         for (int i = 0; i < pSlaveList->nDeviceNumber; i++)
00098         {
00099                 BYTE nCurAddr = pSlaveList->List[i];
00100                 CString curAddr;
00101                 curAddr.Format("%02X", nCurAddr);
00102                 m_ctrlSlaveAddr.AddString(curAddr);
00103         }
00104 
00105 }
00106 
00107 
00108 BOOL CI2cWriteBar::InitTransaction(PU2C_TRANSACTION pTransaction)
00109 {
00110         UpdateData(TRUE);
00111         char *endp;
00112         // initialization of the slave device address
00113         m_strSlaveAddr.TrimLeft();
00114         int SlaveAddr = strtol(m_strSlaveAddr, &endp, 16);
00115         if ((*endp) || m_strSlaveAddr.IsEmpty() || (SlaveAddr < 0) || (SlaveAddr > 0xFF))
00116         {
00117                 MessageBox("Bad value has been used as slave address.\nPlease provide value in range from 0 to FF");
00118                 GetDlgItem(IDC_WRITE_SLAVE_ADDR)->SetFocus();
00119                 return FALSE;
00120         }
00121         pTransaction->nSlaveDeviceAddress = (BYTE)SlaveAddr; 
00122         pTransaction->nMemoryAddressLength = (BYTE)m_nMemAddrLen;
00123         if (pTransaction->nMemoryAddressLength != 0)
00124         {
00125                 // initialization of the memory address
00126                 m_strMemAddr.TrimLeft();
00127                 pTransaction->nMemoryAddress = strtoul(m_strMemAddr, &endp, 16);
00128                 if ((*endp) || m_strMemAddr.IsEmpty() 
00129                         || ((pTransaction->nMemoryAddress > 0xFF) && (pTransaction->nMemoryAddressLength == 1))
00130                         || ((pTransaction->nMemoryAddress > 0xFFFF) && (pTransaction->nMemoryAddressLength == 2))
00131                         )
00132                 {
00133                         switch (pTransaction->nMemoryAddressLength)
00134                         {
00135                         case 1:
00136                                 MessageBox("Bad value has been used as memory address.\nPlease provide value in range from 0 to FF");
00137                                 break;
00138                         case 2:
00139                                 MessageBox("Bad value has been used as memory address.\nPlease provide value in range from 0 to FFFF");
00140                                 break;
00141                         case 4:
00142                                 MessageBox("Bad value has been used as memory address.\nPlease provide value in range from 0 to FFFFFFFF");
00143                                 break;
00144                         default:
00145                                 ASSERT(FALSE);
00146                                 break;
00147                         }
00148                         GetDlgItem(IDC_WRITE_MEM_ADDR)->SetFocus();
00149                         return FALSE;
00150                 }
00151         }
00152         USHORT i = 0;
00153         CString strItem;
00154         unsigned int value;
00155         for (strItem = m_strData.SpanExcluding(" "); strItem.GetLength() && (i < 256); strItem =m_strData.SpanExcluding(" "))
00156         {
00157                 value = strtoul(strItem.GetBuffer(0), &endp, 16);
00158                 if ((*endp) || (value > 0xFF))
00159                 {
00160                         MessageBox("Bad value has been used as data");
00161                         GetDlgItem(IDC_WRITE_DATA)->SetFocus();
00162                         return FALSE;
00163                 }
00164                 pTransaction->Buffer[i++] = (BYTE)value;
00165                 m_strData = m_strData.Mid(strItem.GetLength());
00166                 m_strData.TrimLeft();
00167         }
00168         pTransaction->nBufferLength = i;
00169 
00170         SaveCurrentValuesInHistory();
00171         return TRUE;
00172 
00173 }
00174 
00175 void CI2cWriteBar::EnableControls(bool bEnable)
00176 {
00177         m_bEnableControls = bEnable;
00178         if(bEnable)
00179         {
00180                 GetDlgItem(IDC_WRITE_SLAVE_ADDR)->EnableWindow(TRUE);
00181                 GetDlgItem(IDC_WRITE_MEM_ADDR_LEN)->EnableWindow(TRUE);
00182                 GetDlgItem(IDC_WRITE_MEM_ADDR)->EnableWindow(TRUE);
00183                 GetDlgItem(IDC_WRITE_DATA)->EnableWindow(TRUE);
00184         }
00185         else
00186         {
00187                 GetDlgItem(IDC_WRITE_SLAVE_ADDR)->EnableWindow(FALSE);
00188                 GetDlgItem(IDC_WRITE_MEM_ADDR_LEN)->EnableWindow(FALSE);
00189                 GetDlgItem(IDC_WRITE_MEM_ADDR)->EnableWindow(FALSE);
00190                 GetDlgItem(IDC_WRITE_DATA)->EnableWindow(FALSE);
00191         }
00192 }
00193 
00194 void CI2cWriteBar::OnUpdateI2cWrite (CCmdUI* pCmdUI)
00195 {
00196         if(m_bEnableControls)
00197                 pCmdUI->Enable(TRUE);
00198         else
00199                 pCmdUI->Enable(FALSE);
00200 }
00201 
  • Products
  • PC-I2C/SPI/GPIO Adapters
  • Comparison
  • PC-I2C/SPI/GPIO Adapter DLN-1
  • USB-I2C/SPI/GPIO Adapter DLN-2
  • Multiprotocol Master DLN-4M
  • Multiprotocol Slave DLN-4S
  • Downloads
  • Software
  • Documentation
  • SW Tools & Examples
  • Development Boards
  • Open Source Projects
  • Discontinued Products
  • API Documentation
  • Downloads
  • SW Tools and Examples
footer top footer bottom
© Copyright Diolan 2006-2012