00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "stdafx.h"
00014 #include "controlpanel.h"
00015 #include "SpiBar.h"
00016 #include ".\spibar.h"
00017
00018
00019 #ifdef _DEBUG
00020 #define new DEBUG_NEW
00021 #undef THIS_FILE
00022 static char THIS_FILE[] = __FILE__;
00023 #endif
00024
00026
00027
00028
00029 CSpiBar::CSpiBar()
00030 : CDialogBar()
00031 ,m_bEnableControls(false)
00032 {
00033
00034 m_Length = 0;
00035 m_strData = _T("");
00036
00037 }
00038
00039
00040 void CSpiBar::DoDataExchange(CDataExchange* pDX)
00041 {
00042
00043 DDX_Text(pDX, IDC_SPI_LENGTH, m_Length);
00044 DDX_Text(pDX, IDC_SPI_DATA, m_strData);
00045
00046 }
00047
00048
00049 BOOL CSpiBar::Create(CWnd* pParentWnd)
00050 {
00051 if (!CDialogBar::Create(pParentWnd, IDD, CBRS_TOP | CBRS_TOOLTIPS | CBRS_FLYBY |
00052 CBRS_GRIPPER, IDD))
00053 {
00054 TRACE("CDialogBar::Create failed in CSpiBar::Create");
00055 return FALSE;
00056 }
00057 SetWindowText("SPI Bar");
00058
00059
00060 UpdateData(FALSE);
00061 return TRUE;
00062 }
00063
00064 BEGIN_MESSAGE_MAP(CSpiBar, CDialogBar)
00065
00066
00067 ON_UPDATE_COMMAND_UI(IDC_SPI_READ_WRITE, OnUpdateButton)
00068 ON_UPDATE_COMMAND_UI(IDC_SPI_WRITE, OnUpdateButton)
00069 ON_UPDATE_COMMAND_UI(IDC_SPI_READ, OnUpdateButton)
00070 END_MESSAGE_MAP()
00071
00073
00074
00075 void CSpiBar::EnableControls(bool bEnable)
00076 {
00077 m_bEnableControls = bEnable;
00078 if(bEnable)
00079 {
00080 GetDlgItem(IDC_SPI_LENGTH)->EnableWindow(TRUE);
00081 GetDlgItem(IDC_SPI_DATA)->EnableWindow(TRUE);
00082 }
00083 else
00084 {
00085 GetDlgItem(IDC_SPI_LENGTH)->EnableWindow(FALSE);
00086 GetDlgItem(IDC_SPI_DATA)->EnableWindow(FALSE);
00087 }
00088
00089 }
00090
00091 void CSpiBar::OnUpdateButton (CCmdUI* pCmdUI)
00092 {
00093 if(m_bEnableControls)
00094 pCmdUI->Enable(TRUE);
00095 else
00096 pCmdUI->Enable(FALSE);
00097 }
00098
00099 bool CSpiBar::GetLength(unsigned short* pLength)
00100 {
00101 UpdateData(TRUE);
00102 if ((m_Length <= 0) || (m_Length > 256))
00103 {
00104 MessageBox("Bad value has been used as length.\nPlease provide value in range from 1 to 256");
00105 GetDlgItem(IDC_SPI_LENGTH)->SetFocus();
00106 return false;
00107 }
00108 *pLength = (unsigned short)m_Length;
00109 return true;
00110 }
00111
00112 bool CSpiBar::GetOutData(BYTE* pBuffer, unsigned short Length)
00113 {
00114 UpdateData(TRUE);
00115 USHORT i = 0;
00116 char *endp;
00117 CString strItem;
00118 unsigned int value;
00119 for (strItem = m_strData.SpanExcluding(" "); strItem.GetLength() && (i < Length); strItem =m_strData.SpanExcluding(" "))
00120 {
00121 value = strtoul(strItem.GetBuffer(0), &endp, 16);
00122 if ((*endp) || (value > 0xFF))
00123 {
00124 MessageBox("Bad value has been used as data");
00125 GetDlgItem(IDC_SPI_DATA)->SetFocus();
00126 return false;
00127 }
00128 pBuffer[i++] = (BYTE)value;
00129 m_strData = m_strData.Mid(strItem.GetLength());
00130 m_strData.TrimLeft();
00131 }
00132 if (strItem.GetLength() || (i != Length))
00133 {
00134 MessageBox("Data length mismatch");
00135 GetDlgItem(IDC_SPI_DATA)->SetFocus();
00136 return false;
00137 }
00138 return true;
00139 }
00140
00141
00142