浏览代码

学习模式

sat23 3 年之前
父节点
当前提交
f6f5f413e8

+ 1 - 1
FieldTestTool/FieldTestTool/Global.cpp

@@ -33,7 +33,7 @@ namespace Global
     // 3:  三代版本-Moka自动化;
     // 3:  三代版本-Moka自动化;
     // 4:  四代版本-Moka自动化;
     // 4:  四代版本-Moka自动化;
     INT g_nVersion = 0;
     INT g_nVersion = 0;
-
+    BOOL g_bLearn = FALSE;
 
 
 	//////////////////////////////////////////////////////////////////////////
 	//////////////////////////////////////////////////////////////////////////
 	// 全局函数;
 	// 全局函数;

+ 1 - 0
FieldTestTool/FieldTestTool/Global.h

@@ -57,6 +57,7 @@ namespace Global
 	extern int g_nSysZoomRatio;
 	extern int g_nSysZoomRatio;
 	extern TCHAR g_szVersion[MAX_PATH];
 	extern TCHAR g_szVersion[MAX_PATH];
     extern INT g_nVersion;  // ²âÊÔ¾«Áé°æ±¾;
     extern INT g_nVersion;  // ²âÊÔ¾«Áé°æ±¾;
+    extern BOOL g_bLearn;
 
 
 	//////////////////////////////////////////////////////////////////////////
 	//////////////////////////////////////////////////////////////////////////
 	// È«¾Öº¯Êý;
 	// È«¾Öº¯Êý;

+ 25 - 9
FieldTestTool/FieldTestTool/MainFrm.cpp

@@ -758,22 +758,31 @@ void CMainFrame::OnInfraedVersionCombobox()
     CMFCRibbonComboBox* pVersionCombobox = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_RIBBON_INFRAED_VERSION_COMBOBOX));
     CMFCRibbonComboBox* pVersionCombobox = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_RIBBON_INFRAED_VERSION_COMBOBOX));
     if ( pVersionCombobox )
     if ( pVersionCombobox )
     {
     {
-        int nCurSel = pVersionCombobox->GetCurSel();
-        if ( nCurSel != CB_ERR )
+        Global::g_nVersion = pVersionCombobox->GetCurSel();
+        if (Global::g_nVersion != CB_ERR )
         {
         {
-            Global::g_nVersion = nCurSel;
+            Global::g_nVersion++;
 #if _MSC_VER <= 1500
 #if _MSC_VER <= 1500
-            if ( nCurSel == 1 )
+            if (Global::g_nVersion == 1 )
+                WritePrivateProfileString("TestWizard", "Ver", "1", Global::g_szConfig);
+            else if (Global::g_nVersion == 2 )
                 WritePrivateProfileString("TestWizard", "Ver", "2", Global::g_szConfig);
                 WritePrivateProfileString("TestWizard", "Ver", "2", Global::g_szConfig);
-            else if ( nCurSel == 2 )
+            else if (Global::g_nVersion == 3 )
                 WritePrivateProfileString("TestWizard", "Ver", "3", Global::g_szConfig);
                 WritePrivateProfileString("TestWizard", "Ver", "3", Global::g_szConfig);
-            else if ( nCurSel == 3 )
+            else if (Global::g_nVersion == 4)
                 WritePrivateProfileString("TestWizard", "Ver", "4", Global::g_szConfig);
                 WritePrivateProfileString("TestWizard", "Ver", "4", Global::g_szConfig);
-            else
-                WritePrivateProfileString("TestWizard", "Ver", "1", Global::g_szConfig);
 #else
 #else
             // C++11直接用lambda表示式处理;
             // C++11直接用lambda表示式处理;
-            WritePrivateProfileString("TestWizard", "Ver", [&](){if ( nCurSel == 1)return "2";else if (nCurSel == 2)return "3";else if (nCurSel == 3)return "4";else return "1";}, Global::g_szConfig);
+            WritePrivateProfileString("TestWizard", "Ver", [](int nVer)->LPCSTR{
+                if (nVer == 1)
+                    return _T("1");
+                else if (nVer == 2)
+                    return _T("2");
+                else if (nVer == 3)
+                    return _T("3");
+                else if (nVer == 4)
+                    return _T("4");
+                }(Global::g_nVersion), Global::g_szConfig);
 #endif
 #endif
 
 
         }
         }
@@ -1369,6 +1378,13 @@ BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
             // 必须退出,否则执行2次;
             // 必须退出,否则执行2次;
             return TRUE;
             return TRUE;
         }
         }
+        /* 放子窗口处理;
+        else if (pMsg->wParam == VK_F5)
+        {
+            Global::g_bLearn = !Global::g_bLearn;
+            MessageBox(Global::g_bLearn ? _T("开启学习红外") : _T("关闭红外学习"), _T("提示"), MB_OK);
+            return TRUE;
+        }*/
     }
     }
     return CMDIFrameWndEx::PreTranslateMessage(pMsg);
     return CMDIFrameWndEx::PreTranslateMessage(pMsg);
 }
 }

+ 171 - 35
FieldTestTool/FieldTestTool/PropertiesWnd.cpp

@@ -54,11 +54,25 @@ COLORREF HexColor2RGB(std::string strHexColor)
 
 
     return RGB(r, g, b);
     return RGB(r, g, b);
 }
 }
+
+std::string RGBColor2Hex(DWORD dwRGB)
+{
+    DWORD r = GetRValue(dwRGB);
+    DWORD g = GetGValue(dwRGB);
+    DWORD b = GetBValue(dwRGB);
+
+    TCHAR szHex[8] = {0};
+    _stprintf_s(szHex, _T("%02X%02X%02X"), r, g, b);
+
+    return std::string(szHex);
+}
+
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
 // CResourceViewBar
 // CResourceViewBar
-
+KeyInfo* CPropertiesWnd::m_pKeyInfo = NULL;
 CPropertiesWnd::CPropertiesWnd()
 CPropertiesWnd::CPropertiesWnd()
 {
 {
+    m_dwThreadId = 0;
 }
 }
 
 
 CPropertiesWnd::~CPropertiesWnd()
 CPropertiesWnd::~CPropertiesWnd()
@@ -182,9 +196,8 @@ void CPropertiesWnd::OnSignalBtnClick(UINT uId) // 
         CString text;
         CString text;
         pWnd->GetWindowText(text);
         pWnd->GetWindowText(text);
         KeyInfo *pKey = m_map_key.find(id)->second;
         KeyInfo *pKey = m_map_key.find(id)->second;
-        // 发送红外信号;
-        //SendInfraedSignal(pKey);
-        LearnInfraedSignal(pKey);
+        // 发送红外信号或学习;
+        Global::g_bLearn ? LearnInfraedSignal(uId, pKey) : SendInfraedSignal(pKey);
     }
     }
 }
 }
 
 
@@ -264,13 +277,23 @@ void CPropertiesWnd::LoadKeyInfo(std::vector<KeyInfo> &vtKeyInfo, TString path)
         {
         {
             //////////////////////////////////////////////////////////////////////////
             //////////////////////////////////////////////////////////////////////////
             // 获取属性;
             // 获取属性;
-            m_nCols = _tstoi(pXmlRoot->Attribute("cols") ? pXmlRoot->Attribute("cols") : "3");
-            m_nRowSpacing = _tstoi(pXmlRoot->Attribute("rowSpacing") ? pXmlRoot->Attribute("rowSpacing") : "5");
-            m_nColSpacing = _tstoi(pXmlRoot->Attribute("colSpacing") ? pXmlRoot->Attribute("colSpacing") : "5");
-            m_nTopMargin = _tstoi(pXmlRoot->Attribute("topMargin") ? pXmlRoot->Attribute("topMargin") : "8");
-            m_nLeftMargin = _tstoi(pXmlRoot->Attribute("leftMargin") ? pXmlRoot->Attribute("leftMargin") : "8");
-            int nWidth = _tstoi(pXmlRoot->Attribute("with") ? pXmlRoot->Attribute("with") : "58");
-            int nHeight = _tstoi(pXmlRoot->Attribute("height") ? pXmlRoot->Attribute("height") : "25");
+            m_curKeyLayout.cols = pXmlRoot->Attribute("cols") ? pXmlRoot->Attribute("cols") : "3";
+            m_curKeyLayout.with = pXmlRoot->Attribute("with") ? pXmlRoot->Attribute("with") : "58";
+            m_curKeyLayout.height = pXmlRoot->Attribute("height") ? pXmlRoot->Attribute("height") : "25";
+            m_curKeyLayout.rowSpacing = pXmlRoot->Attribute("rowSpacing") ? pXmlRoot->Attribute("rowSpacing") : "5";
+            m_curKeyLayout.colSpacing = pXmlRoot->Attribute("colSpacing") ? pXmlRoot->Attribute("colSpacing") : "5";
+            m_curKeyLayout.leftMargin = pXmlRoot->Attribute("leftMargin") ? pXmlRoot->Attribute("leftMargin") : "8";
+            m_curKeyLayout.topMargin = pXmlRoot->Attribute("topMargin") ? pXmlRoot->Attribute("topMargin") : "8";
+            m_curKeyLayout.ver = pXmlRoot->Attribute("ver") ? pXmlRoot->Attribute("ver") : "1.0.0.1";
+            m_curKeyLayout.desc = pXmlRoot->Attribute("desc") ? pXmlRoot->Attribute("desc") : "key info";
+
+            m_nCols = _tstoi(m_curKeyLayout.cols.c_str());
+            m_nRowSpacing = _tstoi(m_curKeyLayout.rowSpacing.c_str());
+            m_nColSpacing = _tstoi(m_curKeyLayout.colSpacing.c_str());
+            m_nTopMargin = _tstoi(m_curKeyLayout.topMargin.c_str());
+            m_nLeftMargin = _tstoi(m_curKeyLayout.leftMargin.c_str());
+            int nWidth = _tstoi(m_curKeyLayout.with.c_str());
+            int nHeight = _tstoi(m_curKeyLayout.height.c_str());
             //////////////////////////////////////////////////////////////////////////
             //////////////////////////////////////////////////////////////////////////
 
 
             // 子项;
             // 子项;
@@ -414,16 +437,59 @@ void CPropertiesWnd::SaveKeyInfo(std::vector<KeyInfo> &vtKeyInfo, TString path)
     pXmlRoot = doc.NewElement("KeyList");
     pXmlRoot = doc.NewElement("KeyList");
     if ( pXmlRoot )
     if ( pXmlRoot )
     {
     {
-        pXmlRoot->SetAttribute(_T("cols"), _T("4"));
-        pXmlRoot->SetAttribute(_T("with"), _T("60"));
-        pXmlRoot->SetAttribute(_T("height"), _T("25"));
-        pXmlRoot->SetAttribute(_T("rowSpacing"), _T("5"));
-        pXmlRoot->SetAttribute(_T("colSpacing"), _T("3"));
-        pXmlRoot->SetAttribute(_T("leftMargin"), _T("8"));
-        pXmlRoot->SetAttribute(_T("topMargin"), _T("8"));
-        pXmlRoot->SetAttribute(_T("ver"), _T("1.0.0.1"));
-        pXmlRoot->SetAttribute(_T("desc"), _T("key info"));
+        pXmlRoot->SetAttribute(_T("cols"), m_curKeyLayout.cols.c_str());
+        pXmlRoot->SetAttribute(_T("with"), m_curKeyLayout.with.c_str());
+        pXmlRoot->SetAttribute(_T("height"), m_curKeyLayout.height.c_str());
+        pXmlRoot->SetAttribute(_T("rowSpacing"), m_curKeyLayout.rowSpacing.c_str());
+        pXmlRoot->SetAttribute(_T("colSpacing"), m_curKeyLayout.colSpacing.c_str());
+        pXmlRoot->SetAttribute(_T("leftMargin"), m_curKeyLayout.leftMargin.c_str());
+        pXmlRoot->SetAttribute(_T("topMargin"), m_curKeyLayout.topMargin.c_str());
+        pXmlRoot->SetAttribute(_T("ver"), m_curKeyLayout.ver.c_str());
+        pXmlRoot->SetAttribute(_T("desc"), m_curKeyLayout.desc.c_str());
         doc.InsertEndChild(pXmlRoot);
         doc.InsertEndChild(pXmlRoot);
+
+        for (std::vector<KeyInfo>::iterator it = m_vtKeyInfo.begin(); it != m_vtKeyInfo.end(); it++ )
+        {
+            tinyxml2::XMLElement* pXmlElent = doc.NewElement("ITEM");
+            if ( pXmlElent )
+            {
+                pXmlElent->SetAttribute(_T("color"), RGBColor2Hex(it->color).c_str());
+                // 赋值各元素节点;
+                tinyxml2::XMLElement *pUse = doc.NewElement("USE");
+                pUse->SetText(1);
+                pXmlElent->InsertEndChild(pUse);
+
+                tinyxml2::XMLElement *pName = doc.NewElement("NAME");
+                pName->SetText(it->strName.c_str());
+                pXmlElent->InsertEndChild(pName);
+
+                tinyxml2::XMLElement *pDesc = doc.NewElement("DESC");
+                pDesc->SetText(it->strDesc.c_str());
+                pXmlElent->InsertEndChild(pDesc);
+
+                tinyxml2::XMLElement *pType = doc.NewElement("TYPE");
+                pType->SetText(it->strType.c_str());
+                pXmlElent->InsertEndChild(pType);
+
+                tinyxml2::XMLElement *pValue = doc.NewElement("VALUE");
+                pValue->SetText(it->strValue.c_str());
+                pXmlElent->InsertEndChild(pValue);
+
+                tinyxml2::XMLElement *pValue2 = doc.NewElement("VALUE2");
+                pValue2->SetText(it->strValue2.c_str());
+                pXmlElent->InsertEndChild(pValue2);
+
+                tinyxml2::XMLElement *pValue3 = doc.NewElement("VALUE3");
+                pValue3->SetText(it->strValue3.c_str());
+                pXmlElent->InsertEndChild(pValue3);
+
+                tinyxml2::XMLElement *pValue4 = doc.NewElement("VALUE4");
+                pValue4->SetText(it->strValue4.c_str());
+                pXmlElent->InsertEndChild(pValue4);
+
+                pXmlRoot->InsertEndChild(pXmlElent);
+            }
+        }
     }
     }
 
 
     doc.SaveFile(path.c_str());
     doc.SaveFile(path.c_str());
@@ -686,8 +752,9 @@ bool CPropertiesWnd::SendInfraedSignal(const KeyInfo *pKey)
     return false;
     return false;
 }
 }
 
 
-void CPropertiesWnd::LearnInfraedSignal(KeyInfo *pKey)
+void CPropertiesWnd::LearnInfraedSignal(UINT uId, KeyInfo *pKey)
 {
 {
+    m_pKeyInfo = pKey;
     CMainFrame *pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
     CMainFrame *pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
     if ( !pMainWnd )
     if ( !pMainWnd )
         return;
         return;
@@ -715,8 +782,7 @@ void CPropertiesWnd::LearnInfraedSignal(KeyInfo *pKey)
         // 发送学习红外信号;
         // 发送学习红外信号;
         if ( pMainWnd->m_SerialTW.WriteSync(szSendData, dwSendLen) )
         if ( pMainWnd->m_SerialTW.WriteSync(szSendData, dwSendLen) )
         {
         {
-            memset(szSendData, 0, MAX_SENDDATA);
-            pMainWnd->m_SerialTW.ReadSync(szSendData, MAX_SENDDATA);
+            // 自动化的红外不返回,如果没有收到任务红外,会等待10多秒后自动结束学习状态;
             TRACE("发送单信号成功\n");
             TRACE("发送单信号成功\n");
         }
         }
         else
         else
@@ -724,21 +790,71 @@ void CPropertiesWnd::LearnInfraedSignal(KeyInfo *pKey)
             TRACE("发送单信号失败\n");
             TRACE("发送单信号失败\n");
         }
         }
     }
     }
+  
+    if (m_dwThreadId)
+    {
+        PostThreadMessage(m_dwThreadId, 10, uId, 0);
+    }
+}
 
 
-#if 1 
-    // 要另起线程后台读取,否则UI会卡住,体验不好;
-    Sleep(1500);
-    // 等待学习的红外信号发出;
-    DWORD dwRealRead = 0;
-    memset(szSendData, 0, MAX_SENDDATA);
-    if ( dwRealRead = pMainWnd->m_SerialTW.ReadSync(szSendData, MAX_SENDDATA) )
+DWORD WINAPI CPropertiesWnd::ThreadLearn(LPVOID lpParam)
+{
+    CPropertiesWnd* pthis = (CPropertiesWnd*)lpParam;
+    CMainFrame* pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
+    if (!pthis || !pMainWnd)
+        return 0;
+
+    DWORD dwSendLen = 0;
+    byte szSendData[MAX_SENDDATA] = { 0 };
+    while (Global::g_bLearn)
     {
     {
-        if ( Global::g_nVersion == 1 )
-            pKey->strValue =(TCHAR*)szSendData;
-        else if ( Global::g_nVersion == 2 )
-            pKey->strValue2 = utils::BytesToHexString(szSendData, dwRealRead, ' ');
+        MSG msg;
+        if (GetMessage(&msg, NULL, 0, 0))
+        {
+            if (msg.message == 10)
+            {
+                DWORD dwTickCount = ::GetTickCount64();
+                while (GetTickCount64() - dwTickCount < 60000)
+                {
+                    // 等待学习的红外信号发出;
+                    DWORD dwRealRead = 0;
+                    memset((char*)szSendData, 0, MAX_SENDDATA);
+                    if (dwRealRead = pMainWnd->m_SerialTW.ReadSync(szSendData, MAX_SENDDATA))
+                    {
+                        if (m_pKeyInfo)
+                        {
+                            if (Global::g_nVersion == 1)
+                                m_pKeyInfo->strValue = (TCHAR*)szSendData;
+                            else if (Global::g_nVersion == 2)
+                            {
+                                std::string val = utils::BytesToHexString(szSendData, dwRealRead, ' ');
+                                //if (_tcsncmp("fd fd 30 03 88 99 aa 34 ff ff ff ff ff ff ff ff", val.c_str(), 16))
+                                if ( _tcsncmp("FD FD 30 03 88 99 AA 34 FF FF FF FF FF FF FF FF", val.c_str(), 16) )
+                                {
+                                    m_pKeyInfo->strValue2 = val;
+                                }
+                                else
+                                {
+                                    break;
+                                }
+                            }
+
+                            m_pKeyInfo->pbtnInfo->pBTN->SetFaceColor(RGB(255, 255, 255), TRUE);
+                        }
+
+                        break;
+                    }
+
+                    Sleep(200);
+                }
+            }
+        }
     }
     }
-#endif
+
+    OutputDebugString(_T("线程结束"));
+    pthis->m_dwThreadId = 0;
+
+    return 0;
 }
 }
 
 
 // 如果CDockablePane无控件,改变大小时背景会刷新失败;
 // 如果CDockablePane无控件,改变大小时背景会刷新失败;
@@ -770,3 +886,23 @@ void CPropertiesWnd::OnDestroy()
     // TODO: 在此处添加消息处理程序代码
     // TODO: 在此处添加消息处理程序代码
     ClearAllBTNInfo();
     ClearAllBTNInfo();
 }
 }
+
+
+BOOL CPropertiesWnd::PreTranslateMessage(MSG* pMsg)
+{
+    // TODO: 在此添加专用代码和/或调用基类
+    if (pMsg->message == WM_KEYDOWN)
+    {
+        if (pMsg->wParam == VK_F5)
+        {
+            Global::g_bLearn = !Global::g_bLearn;
+            if (m_dwThreadId == 0 && Global::g_bLearn)
+            {
+                CreateThread(NULL, 0, ThreadLearn, this, 0, &m_dwThreadId);
+            }
+            MessageBox(Global::g_bLearn ? _T("开启学习红外") : _T("关闭红外学习"), _T("提示"), MB_OK);
+            return TRUE;
+        }
+    }
+    return CDockablePane::PreTranslateMessage(pMsg);
+}

+ 22 - 2
FieldTestTool/FieldTestTool/PropertiesWnd.h

@@ -52,6 +52,20 @@ typedef struct __KEY_INFO__
     }
     }
 }KeyInfo, *pKeyInfo;
 }KeyInfo, *pKeyInfo;
 
 
+// 按键布局;
+typedef struct __KEY_LAYOUT__
+{
+    std::string cols;
+    std::string with;
+    std::string height;
+    std::string rowSpacing;
+    std::string colSpacing;
+    std::string leftMargin;
+    std::string topMargin;
+    std::string ver;
+    std::string desc;
+}KeyLayout, *pKeyLayout;
+
 class CPropertiesToolBar : public CMFCToolBar
 class CPropertiesToolBar : public CMFCToolBar
 {
 {
 public:
 public:
@@ -103,6 +117,10 @@ protected:
     std::map<UINT, KeyInfo*> m_map_key;
     std::map<UINT, KeyInfo*> m_map_key;
     std::vector<TString> m_vtTWSignal;
     std::vector<TString> m_vtTWSignal;
     std::vector<BTNInfo*> m_vtBTNInfo;
     std::vector<BTNInfo*> m_vtBTNInfo;
+    // 当前xml布局;
+    KeyLayout m_curKeyLayout;
+    // 当前学习的key信息;
+    static KeyInfo* m_pKeyInfo;
 
 
     // 实现
     // 实现
 public:
 public:
@@ -133,12 +151,14 @@ protected:
     void ClearAllBTNInfo();
     void ClearAllBTNInfo();
     // 自动根据Combobox选项长度设置宽度;
     // 自动根据Combobox选项长度设置宽度;
     void AutoSetDroppedWidth(CComboBox* pCombobox);
     void AutoSetDroppedWidth(CComboBox* pCombobox);
-
     bool SendInfraedSignal(const KeyInfo *pKey);
     bool SendInfraedSignal(const KeyInfo *pKey);
     // 学习红外键值;
     // 学习红外键值;
-    void LearnInfraedSignal(KeyInfo *pKey);
+    void LearnInfraedSignal(UINT uId, KeyInfo *pKey);
+    DWORD m_dwThreadId;
+    static DWORD WINAPI ThreadLearn(LPVOID lpParam);
 public:
 public:
     afx_msg void OnPaint();
     afx_msg void OnPaint();
     afx_msg void OnDestroy();
     afx_msg void OnDestroy();
+    virtual BOOL PreTranslateMessage(MSG* pMsg);
 };
 };