lijinwen 3 months ago
parent
commit
a35408f3e8
4 changed files with 78 additions and 31 deletions
  1. 63 0
      BaseHelper/CommonHelper.cpp
  2. 5 25
      BaseHelper/CommonHelper.h
  3. 1 0
      ProtocolDecoder.vcxproj
  4. 9 6
      ProtocolUSB/USBDecoder.cpp

+ 63 - 0
BaseHelper/CommonHelper.cpp

@@ -0,0 +1,63 @@
+// // ******************************************************************
+// //       /\ /|       @File         CommonHelper.cpp
+// //       \ V/        @Brief
+// //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
+// //       /  |        @Creation      2024-05-31
+// //      /  \\        @Modified      2024-05-31
+// //    *(__\_\
+// // ******************************************************************
+
+#include "CommonHelper.h"
+   
+namespace Protocol
+{
+    // 移除数组中第一个元素,如果它与其他元素的差异最大
+    std::vector<int32_t> CommonHelper::RemoveFirstIfMostDifferent(const std::vector<int32_t>& array)
+    {
+        if (array.size() < 2)
+        {
+            throw std::invalid_argument("Array must contain at least two elements.");
+        }
+        int32_t first_value = array[0];
+        int32_t max_difference = 0;
+        int32_t most_different_index = 0;
+
+        // 找到与第一个元素差异最大的元素
+        for (int32_t i = 1; i < static_cast<int32_t>(array.size()); ++i)
+        {
+            int32_t difference = std::abs(array[i] - first_value);
+            if (difference > max_difference)
+            {
+                max_difference = difference;
+                most_different_index = i;
+            }
+        }
+
+        // 如果第一个元素与其他元素差异最大,则移除第一个元素
+        if (most_different_index == 0)
+        {
+            return std::vector<int32_t>(array.begin() + 1, array.end());
+        }
+
+        return array; // 如果第一个元素不是差异最大的,则返回原数组
+    }
+
+    uint8_t CommonHelper::SwapHighLowBits(uint8_t value)
+    {
+        // 获取低4位
+        uint8_t lowerNibble = value & 0x0F;
+        // 获取高4位
+        uint8_t higherNibble = (value >> 4) & 0x0F;
+        // 交换高低4位
+        return static_cast<uint8_t>(higherNibble | lowerNibble << 4);
+    }
+    uint8_t CommonHelper::ReverseOrderBits(uint8_t value)
+    {
+        value = SwapHighLowBits(value); //高低互换
+        value = static_cast<uint8_t>((value & 0xCC) >> 2 | (value & 0x33) << 2); //交换每对相邻的位
+        value = static_cast<uint8_t>((value & 0xAA) >> 1 | (value & 0x55) << 1); //交换每对相邻的位
+        return value;
+    }
+
+    
+}

+ 5 - 25
BaseHelper/CommonHelper.h

@@ -13,7 +13,6 @@
 #include <vector>
 
 #include "Logger.h"
-
 // 检查类型是否为指针
 template <typename T>
 struct IsPointer : std::false_type
@@ -24,7 +23,6 @@ template <typename T>
 struct IsPointer<T*> : std::true_type
 {
 };
-
 namespace Protocol
 {
     class CommonHelper
@@ -53,30 +51,12 @@ namespace Protocol
         static std::vector<T> ConvertPointerArrayToVector(T* array, size_t size);
         static uint8_t SwapHighLowBits(uint8_t value);
         static uint8_t ReverseOrderBits(uint8_t value);
+        static std::vector<int32_t> RemoveFirstIfMostDifferent(const std::vector<int32_t>& array);
+        //static bool NeedRemoveFirstIfMostDifferent(const int32_t* start_indexs, const int32_t count);
     };
-
-    inline uint8_t CommonHelper::ReverseOrderBits(uint8_t value)
-    {
-        value = SwapHighLowBits(value); //高低互换
-        value = (value & 0xCC) >> 2 | (value & 0x33) << 2; // 交换每对相邻的位
-        value = (value & 0xAA) >> 1 | (value & 0x55) << 1; // 交换每对相邻的位
-        return value;
-    }
-
-
-    inline uint8_t CommonHelper::SwapHighLowBits(uint8_t value)
-    {
-        // 获取低4位
-        uint8_t lowerNibble = value & 0x0F;
-        // 获取高4位
-        uint8_t higherNibble = (value >> 4) & 0x0F;
-        // 交换高低4位
-        return static_cast<uint8_t>(higherNibble | lowerNibble << 4);
-    }
-
     template <typename T>
-    std::vector<T> CommonHelper::ConvertPointerArrayToVector(T* array, size_t size)
-    {
+      auto CommonHelper::ConvertPointerArrayToVector(T* array, size_t size) -> std::vector<T>
+      {
         std::vector<T> vec;
         vec.reserve(size);  // 预分配内存以避免多次重新分配
 
@@ -110,5 +90,5 @@ namespace Protocol
 
         return vec;
     }
-
+   
 }

+ 1 - 0
ProtocolDecoder.vcxproj

@@ -157,6 +157,7 @@
         </Link>
     </ItemDefinitionGroup>
     <ItemGroup>
+        <ClCompile Include="BaseHelper\CommonHelper.cpp"/>
         <ClCompile Include="BaseHelper\DataCheckHelper.cpp">
             <RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
             <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>

+ 9 - 6
ProtocolUSB/USBDecoder.cpp

@@ -112,7 +112,7 @@ namespace Protocol
             {
                 //resultData.Add(new UsbDecodeResultCell(element));
                 resultEvents.emplace_back(element);
-                resultEvents[resultEvents.size() - 1].EventIndex = resultEvents.size();
+                resultEvents[resultEvents.size() - 1].EventIndex = static_cast<int64_t>(resultEvents.size());
             }
         }
         WriteLog(LogLevel::Level2, "ParseSingleData done");
@@ -122,7 +122,7 @@ namespace Protocol
     bool USBDecoder::CheckTimeDifferenceWithinThreshold(const double* array, int32_t size, double threshold)
     {
         //threshold = 0.15;
-        for (int i = 1; i < size; i++)
+        for (int i = 2; i < size; i++)
         {
             double difference = std::abs(array[i] - array[i - 1]);
             double percentage_difference = difference / array[i - 1];
@@ -179,6 +179,7 @@ namespace Protocol
         return CheckSyncSpansByArray(start_indexs, count, avg_spans);
     }
 
+
     bool USBDecoder::CheckSyncSpansByArray(const int32_t* start_indexs, const int32_t count,
                                            int32_t& avg_spans)
     {
@@ -186,18 +187,20 @@ namespace Protocol
         {
             return false;
         }
-        auto tmpSpans = new double[count - 1];
+
+        auto tmp_spans = new double[count - 1];
         for (int i = 1; i < count; i++)
         {
-            tmpSpans[i - 1] = static_cast<double>(start_indexs[i] - start_indexs[i - 1]);
+            tmp_spans[i - 1] = static_cast<double>(start_indexs[i] - start_indexs[i - 1]);
         }
+
         double total_spans = 0;
         for (int i = 0; i < count - 1; i++)
         {
-            total_spans += std::abs(tmpSpans[0]);
+            total_spans += std::abs(tmp_spans[2 + (i / 2)]);
         }
         avg_spans = static_cast<int>(total_spans / (count - 1));
-        return CheckTimeDifferenceWithinThreshold(tmpSpans, count - 1);
+        return CheckTimeDifferenceWithinThreshold(tmp_spans, count - 1);
     }
 
     bool USBDecoder::FindAllSyncs(Protocol::TwoLevelEdgePulse* node, std::vector<SYNC>& all_sync