Browse Source

iic 解码 初版

lijinwen 2 months ago
parent
commit
e6b5c2745e

+ 121 - 93
BaseHelper/common_helper.cc

@@ -11,101 +11,129 @@
 
 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.");
-        }
-        const int32_t first_value = array[0];
-        int32_t max_difference = 0;
-        int32_t most_different_index = 0;
+	// 移除数组中第一个元素,如果它与其他元素的差异最大
+	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.");
+		}
+		const 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;
-            }
-        }
+		// 找到与第一个元素差异最大的元素
+		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());
-        }
+		// 如果第一个元素与其他元素差异最大,则移除第一个元素
+		if (most_different_index == 0)
+		{
+			return std::vector<int32_t>(array.begin() + 1, array.end());
+		}
 
-        return array; // 如果第一个元素不是差异最大的,则返回原数组
-    }
-    bool CommonHelper::CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node)
-    {
-        return node != nullptr && node->start_index >= 0 && node->end_index > 0 && node->LevelCount() > 0;
-    }
+		return array; // 如果第一个元素不是差异最大的,则返回原数组
+	}
+	bool CommonHelper::CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node)
+	{
+		return node != nullptr && node->start_index >= 0 && node->end_index > 0
+			&& node->LevelCount() > 0 && node->current_level != TwoLevelEdgePulseStatusType::None;
+	}
 
-    uint8_t CommonHelper::SwapHighLowBits(const uint8_t value)
-    {
-        // 获取低4位
-        const uint8_t lower_nibble = value & 0x0F;
-        // 获取高4位
-        const uint8_t higher_nibble = (value >> 4) & 0x0F;
-        // 交换高低4位
-        return static_cast<uint8_t>(higher_nibble | lower_nibble << 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;
-    }
-    bool CommonHelper::GetNodeByIndex(const int32_t index, TwoLevelEdgePulse* node, const int32_t time_out_by_s)
-    {
-        if (index < 0) return false;
-        if (index >= node->start_index && index <= node->end_index) return true;
-        if (TwoLevelEdgePulse* temp_node = node; index > temp_node->end_index)
-        {
-            temp_node++;
-            //var startTime = DateTime.Now;
-            const auto start_time = std::chrono::steady_clock::now();
-            while (CheckTwoLevelEdgePulseValid(temp_node))
-            {
-                if (index >= temp_node->start_index && index <= temp_node->end_index) return temp_node;
-                temp_node++;
-                if (auto end_time = std::chrono::steady_clock::now();
-                    std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count() > time_out_by_s)
-                {
-                    return false;
-                }
-            }
-        }
-        else if (index < temp_node->start_index)
-        {
-            temp_node--;
-            //var startTime = DateTime.Now;
-            const auto start_time = std::chrono::steady_clock::now();
-            while (CheckTwoLevelEdgePulseValid(temp_node))
-            {
-                if (index >= temp_node->start_index && index <= temp_node->end_index) return temp_node;
-                temp_node--;
-                if (auto end_time = std::chrono::steady_clock::now();
-                    std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count() > time_out_by_s)
-                {
-                    return false;
-                }
-            }
-        }
-        return false;
-    }
-    bool CommonHelper::FindNextNode(const int32_t index, TwoLevelEdgePulse* node, const int32_t time_out_by_s)
-    {
-        if (GetNodeByIndex(index, node, time_out_by_s) && CheckTwoLevelEdgePulseValid(++node))
-        {
-            return true;
-        }
-        return false;
-    }
+	uint8_t CommonHelper::SwapHighLowBits(const uint8_t value)
+	{
+		// 获取低4位
+		const uint8_t lower_nibble = value & 0x0F;
+		// 获取高4位
+		const uint8_t higher_nibble = (value >> 4) & 0x0F;
+		// 交换高低4位
+		return static_cast<uint8_t>(higher_nibble | lower_nibble << 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;
+	}
+	bool CommonHelper::GetNodeByIndex(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s)
+	{
+		if (index < 0) return false;
+		if (index >= node->start_index && index <= node->end_index) return true;
+
+		TwoLevelEdgePulse* temp_node = node;
+		if (index > temp_node->end_index)
+		{
+			const auto start_time = std::chrono::steady_clock::now();
+			while (CheckTwoLevelEdgePulseValid(temp_node))
+			{
+				if (index >= temp_node->start_index && index <= temp_node->end_index)
+				{
+					node = temp_node;
+					return true;
+				}
+				temp_node++;
+				if (auto end_time = std::chrono::steady_clock::now();
+					std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() > time_out_by_s)
+				{
+					return false;
+				}
+			}
+		}
+		else if (index < temp_node->start_index)
+		{
+			const auto start_time = std::chrono::steady_clock::now();
+			while (CheckTwoLevelEdgePulseValid(temp_node))
+			{
+				if (index >= temp_node->start_index && index <= temp_node->end_index)
+				{
+					node = temp_node;
+					return true;
+				}
+				temp_node--;
+				if (auto end_time = std::chrono::steady_clock::now();
+					std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() > time_out_by_s)
+				{
+					return false;
+				}
+			}
+		}
+		return false;
+	}
+	bool CommonHelper::FindNextNode(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s)
+	{
+		if (index < 0)
+		{
+			return false;
+		}
+		if (GetNodeByIndex(index, node))
+		{
+			node++;
+			if (CheckTwoLevelEdgePulseValid(node))
+			{
+				return true;
+			}
+		}
+		/*while (CheckTwoLevelEdgePulseValid(node) && index< node->start_index)
+		{
+			node++;
+			if (node->start_index > index)
+			{
+				break;
+			}
+		}
+		node++;
+		if (CheckTwoLevelEdgePulseValid(node) && node->start_index > index)
+		{
+			return true;
+		}*/
+		return false;
+	}
 }

+ 2 - 2
BaseHelper/common_helper.h

@@ -58,8 +58,8 @@ namespace Protocol
         static uint8_t ReverseOrderBits(uint8_t value);
         static std::vector<int32_t> RemoveFirstIfMostDifferent(const std::vector<int32_t>& array);
         static bool CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node);
-        static bool FindNextNode(const int32_t index, TwoLevelEdgePulse* node, const int32_t time_out_by_s = 2);
-        static bool GetNodeByIndex(const int32_t index, TwoLevelEdgePulse* node, const int32_t time_out_by_s = 2);
+        static bool FindNextNode(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s = 2);
+        static bool GetNodeByIndex(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s = 2);
         //static bool NeedRemoveFirstIfMostDifferent(const int32_t* start_indexs, const int32_t count);
     };
 

+ 12 - 26
ProtocolIIC/iic_decode_result.h

@@ -29,64 +29,50 @@ namespace Protocol
 
     struct IicDecodeResult //: DecodeResult
     {
-        bool decode_result_need_update = false;
         bool decode_event_need_update = false;
-        IicDecodeResultCell* decode_result_cells_ptr; //解码数据结果指针
-        IicPacket* decode_events_ptr; //解码结果事件指针
-        uint64_t decode_result_count; //结果数量
         uint64_t decode_event_count; //事件数量
-        
-        IicDecodeResult() : decode_result_cells_ptr(nullptr), decode_events_ptr(nullptr), decode_result_count(0),
-                            decode_event_count(0) 
-                          
+        IicEvent* decode_events_ptr; //解码结果事件指针
+        intptr_t decoder_ptr;
+        IicDecodeResult() : decode_event_count(0),
+                            decode_events_ptr(nullptr),
+                            decoder_ptr(0)
         {
-            
         }
-        IicDecodeResult(const IicDecodeResult& result)  
+        IicDecodeResult(const IicDecodeResult& result)
         {
-            decode_result_need_update = result.decode_result_need_update;
             decode_event_need_update = result.decode_event_need_update;
-            decode_result_cells_ptr = result.decode_result_cells_ptr;
             decode_events_ptr = result.decode_events_ptr;
-
-            decode_result_count = result.decode_result_count;
+            decoder_ptr = 0;
             decode_event_count = result.decode_event_count;
         }
         ~IicDecodeResult() = default;
 
         IicDecodeResult(IicDecodeResult&& other) noexcept
-            : decode_result_need_update(other.decode_result_need_update),
-              decode_event_need_update(other.decode_event_need_update),
-              decode_result_cells_ptr(other.decode_result_cells_ptr),
+            : decode_event_need_update(other.decode_event_need_update),
+              decode_event_count(other.decode_event_count),
               decode_events_ptr(other.decode_events_ptr),
-              decode_result_count(other.decode_result_count),
-              decode_event_count(other.decode_event_count) 
+              decoder_ptr(other.decoder_ptr)
+
 
         {
         }
         IicDecodeResult& operator=(const IicDecodeResult& other)
         {
             if (this == &other) return *this;
-            decode_result_need_update = other.decode_result_need_update;
             decode_event_need_update = other.decode_event_need_update;
-            decode_result_cells_ptr = other.decode_result_cells_ptr;
             decode_events_ptr = other.decode_events_ptr;
-            decode_result_count = other.decode_result_count;
             decode_event_count = other.decode_event_count;
             return *this;
         }
         IicDecodeResult& operator=(IicDecodeResult&& other) noexcept
         {
             if (this == &other) return *this;
-            decode_result_need_update = other.decode_result_need_update;
             decode_event_need_update = other.decode_event_need_update;
-            decode_result_cells_ptr = other.decode_result_cells_ptr;
             decode_events_ptr = other.decode_events_ptr;
-            decode_result_count = other.decode_result_count;
             decode_event_count = other.decode_event_count;
             return *this;
         }
-   
+
     };
 
 }

+ 448 - 396
ProtocolIIC/iic_decoder.cc

@@ -4,400 +4,452 @@
 
 namespace Protocol
 {
-    bool IicDecoder::DecodeIic(const IicDecodeOptions& option, const EdgePulseDataTwoLevels& edge_pulses_clk,
-                               const EdgePulseDataTwoLevels& edge_pulses_data, IicDecodeResult& result)
-    {
-        result = {};
-        is_cancel_ptr_ = option.is_cancel;
-        wave_data_len_ = static_cast<int32_t>(
-            std::min(edge_pulses_clk.waveform_data_count, edge_pulses_data.waveform_data_count));
-        bit_width_ = option.data_bit_width;
-        const int32_t addr_len = bit_width_ == IicEnums::DataBitWidth::DATA_BIT_WIDTH_7_BIT ? 7 : 10;
-        int32_t start_index = 0;
-
-        packet_infos_.clear();
-        TwoLevelEdgePulse* edge_pulses_data_ptr = edge_pulses_data.GetDataAddrPtr();
-        TwoLevelEdgePulse* edge_pulses_clk_ptr = edge_pulses_clk.GetDataAddrPtr();
-
-        while (true)
-        {
-            // 检查是否取消
-            if (is_cancel_ptr_ != nullptr && *is_cancel_ptr_)
-            {
-                WriteLog(LogLevel::Level2, "AsyncFunction canceled");
-                return false;
-            }
-            // find start index
-            if (!FindStartNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr))
-            {
-                break;
-            }
-            int32_t data_len = wave_data_len_;
-            IicPacket info = {};
-            info.has_start = true;
-            info.start_index = start_index;
-            info.start_len = 1;
-            packet_infos_.push_back(info);
-            start_index = edge_pulses_clk_ptr++->start_index;
-            if (start_index <= 0) break;
-            start_index = edge_pulses_clk_ptr++->start_index;
-            if (start_index <= 0) break;
-
-            info.addr_index = start_index;
-            for (int index = 0; index < addr_len; ++index)
-            {
-                if (start_index >= data_len)
-                {
-                    start_index = data_len;
-                    break;
-                }
-                if (!CommonHelper::GetNodeByIndex(index, edge_pulses_data_ptr))
-                {
-                    start_index = data_len;
-                    break;
-                }
-                const bool bit = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
-                info.addr_data = static_cast<uint16_t>(info.addr_data << 1 | (bit ? 1 : 0));
-                if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-                {
-                    start_index = data_len;
-                    break;
-                }
-                if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-                {
-                    start_index = data_len;
-                    break;
-                }
-                start_index = edge_pulses_clk_ptr->start_index;
-            }
-            info.addr_len = start_index - info.addr_index;
-
-            // 解析RW标识位
-            info.rw_index = start_index;
-            if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
-            {
-                start_index = data_len;
-                break;
-            }
-            info.rw = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
-            start_index = edge_pulses_data_ptr->start_index;
-            if (start_index <= 0 || *is_cancel_ptr_)
-            {
-                start_index = data_len;
-                break;
-            }
-
-            if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-            {
-                start_index = data_len;
-                break;
-            }
-            start_index = edge_pulses_clk_ptr->start_index;
-
-            info.rw_len = start_index - info.rw_index;
-            packet_infos_.push_back(info);
-
-            //解析Addr后的ACK信号
-            info.addr_ack_index = start_index;
-            info.success_addr_ack = false;
-            if (start_index >= data_len) break;
-            if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
-            {
-                start_index = data_len;
-                break;
-            }
-            info.addr_ack = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;  //get level
-            if (!CommonHelper::FindNextNode(start_index, edge_pulses_data_ptr))
-            {
-                start_index = data_len;
-                break;
-            }
-
-            info.addr_ack_len = start_index - info.addr_ack_index;
-            packet_infos_.push_back(info);
-
-            if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-            {
-                start_index = data_len;
-                break;
-            }
-            start_index = edge_pulses_clk_ptr->start_index;
-
-            if (start_index <= 0 || *is_cancel_ptr_)
-            {
-                start_index = data_len;
-                break;
-            }
-
-            std::vector<IicDataInfo> temp_infos;
-            //开始解析数据
-            while (true)
-            {
-                if (!FindStopNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr))
-                {
-                    break;
-                }
-                int stop_index = edge_pulses_data_ptr->start_index;
-                if (stop_index > 0)
-                {
-                    info.has_end = true;
-                    info.end_index = stop_index;
-                    info.end_len = 1;
-                    if (start_index >= stop_index)
-                    {
-                        start_index = stop_index + 1;
-                        break;
-                    }
-                }
-                //下一帧 查找下一段启动信号
-
-                if (!FindStartNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr) || *is_cancel_ptr_)
-                {
-                    break;
-                }
-                auto next_start_index = edge_pulses_clk_ptr->start_index;
-                if (start_index >= next_start_index && next_start_index != -1)
-                {
-                    start_index = next_start_index - 1;
-                    break;
-                }
-                IicDataInfo data_info = {};
-                data_info.index = start_index;
-
-                //解析数据
-                for (int32_t index = 0; index < 8; index++)
-                {
-                    if (start_index >= data_len)
-                    {
-                        start_index = data_len;
-                        break;
-                    }
-                    if (start_index >= stop_index && stop_index != -1)
-                    {
-                        start_index = stop_index + 1;
-                        break;
-                    }
-                    if (start_index >= next_start_index && next_start_index != -1)
-                    {
-                        start_index = next_start_index - 1;
-                        break;
-                    }
-                    data_info.data <<= 1;
-                    if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
-                    {
-                        start_index = data_len;
-                        break;
-                    }
-                    bool bit = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
-                    data_info.data |= static_cast<uint8_t>(bit ? 1 : 0);
-
-                    if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-                    {
-                        start_index = data_len;
-                        break;
-                    }
-                    start_index = edge_pulses_clk_ptr->start_index;
-                    if (start_index <= 0 || *is_cancel_ptr_)
-                    {
-                        start_index = data_len;
-                        break;
-                    }
-
-                    if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-                    {
-                        start_index = data_len;
-                        break;
-                    }
-                    start_index = edge_pulses_clk_ptr->start_index;
-                    if (start_index <= 0 || *is_cancel_ptr_)
-                    {
-                        start_index = data_len;
-                        break;
-                    }
-                }
-
-                if (start_index >= data_len) break;
-                if (start_index >= stop_index && stop_index != -1)
-                {
-                    start_index = stop_index + 1;
-                    break;
-                }
-                if (start_index >= next_start_index && next_start_index != -1)
-                {
-                    start_index = next_start_index - 1;
-                    break;
-                }
-                //计算长度
-                data_info.length = stop_index - start_index;
-                //解析数据包后的ACK信号
-                data_info.ack_index = start_index;
-                if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
-                {
-                    start_index = data_len;
-                    break;
-                }
-                data_info.ack = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
-
-                if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-                {
-                    start_index = data_len;
-                    break;
-                }
-                start_index = edge_pulses_clk_ptr->start_index;
-
-                if (start_index <= 0 /*|| token.IsCancellationRequested || needclear*/)
-                {
-                    start_index = data_len;
-                    break;
-                }
-                data_info.ack_length = start_index - data_info.ack_index;
-                data_info.success_ack = false;
-                if (start_index >= stop_index && stop_index != -1)
-                {
-                    start_index = stop_index + 1;
-                    break;
-                }
-                if (start_index >= next_start_index && next_start_index != -1)
-                {
-                    start_index = next_start_index - 1;
-                    break;
-                }
-                temp_infos.push_back(data_info);
-
-                if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
-                {
-                    start_index = data_len;
-                    break;
-                }
-                start_index = edge_pulses_clk_ptr->start_index;
-
-                if (start_index <= 0 || *is_cancel_ptr_)
-                {
-                    start_index = data_len;
-                    break;
-                }
-            }
-            info.data_infos = temp_infos;
-            //最后一个数据包的ACK信号为NACK
-            if (!temp_infos.empty())
-            {
-                info.data_infos = std::move(temp_infos);
-                info.has_end = true;
-                info.end_index = start_index;
-                info.end_len = 1;
-                packet_infos_.push_back(info);
-            }
-        }
-        if (!packet_infos_.empty())
-        {
-            result.decode_event_need_update = true;
-        }
-        result.decode_event_count = packet_infos_.size();
-        result.decode_events_ptr = packet_infos_.data();
-
-        return true;
-    }
-    bool IicDecoder::FindStopNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
-                                  TwoLevelEdgePulse* node_scl)
-    {
-        if (start_index < 0)
-        {
-            return false;
-        }
-        const auto start_time = std::chrono::steady_clock::now();
-        while (CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
-        {
-            // 找到SDA上升沿
-            // 寻找SCL高
-            if (node_sda->start_index > start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::High)
-            {
-                node_sda++;
-                continue;
-            }
-
-            while (node_scl->start_index > start_index)
-            {
-                node_scl++;
-            }
-            if (node_sda->start_index > node_scl->start_index)
-            {
-                node_scl++;
-            }
-            if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
-            {
-                return false;
-            }
-
-            // 确认SCL为高电平
-            if (node_scl->current_level == TwoLevelEdgePulseStatusType::High)
-            {
-                if (node_sda->start_index <= node_scl->start_index)
-                {
-                    // 找到目标停止条件
-                    return true;
-                }
-            }
-            start_index = node_scl->end_index + 1;
-            if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::nanoseconds>(
-                    end_time - start_time).count() >
-                DECODE_OUT_TIME_BY_SEC)
-            {
-                WriteLog(LogLevel::Level2, "IIC FindStopNode TimeOut");
-                return false;
-            }
-        }
-        return CommonHelper::CheckTwoLevelEdgePulseValid(node_sda);
-    }
-
-    bool IicDecoder::FindStartNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
-                                   TwoLevelEdgePulse* node_scl)
-    {
-        if (start_index < 0)
-        {
-            return false;
-        }
-        const auto start_time = std::chrono::steady_clock::now();
-        while (CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
-        {
-            // 找到SDA下降沿
-            // 寻找SCL高
-            if (node_sda->start_index > start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::Low)
-            {
-                node_sda += 1;
-                continue;
-            }
-
-            while (node_scl->start_index > start_index)
-            {
-                node_scl++;
-            }
-            if (node_sda->start_index > node_scl->start_index)
-            {
-                node_scl += 1;
-            }
-            if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
-            {
-                return false;
-            }
-
-            //找到SCL下降沿
-            if (node_scl->current_level == TwoLevelEdgePulseStatusType::Low)
-            {
-                if (node_sda->start_index <= node_scl->start_index)
-                {
-                    //找到目标头
-                    return true;
-                }
-            }
-            start_index = node_scl->end_index + 1;
-            if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::nanoseconds>(
-                    end_time - start_time).count() >
-                DECODE_OUT_TIME_BY_SEC)
-            {
-                WriteLog(LogLevel::Level2, "IIC FindStartNode TimeOut");
-                return false;
-            }
-        }
-        return CommonHelper::CheckTwoLevelEdgePulseValid(node_sda);
-    }
+	bool IicDecoder::DecodeIic(const IicDecodeOptions& option, const EdgePulseDataTwoLevels& edge_pulses_clk,
+		const EdgePulseDataTwoLevels& edge_pulses_data, IicDecodeResult& result)
+	{
+		result = {};
+		result.decoder_ptr = reinterpret_cast<intptr_t>(this);
+		is_cancel_ptr_ = option.is_cancel;
+		wave_data_len_ = static_cast<int32_t>(
+			std::min(edge_pulses_clk.waveform_data_count, edge_pulses_data.waveform_data_count));
+		bit_width_ = option.data_bit_width;
+		const int32_t addr_len = bit_width_ == IicEnums::DataBitWidth::DATA_BIT_WIDTH_7_BIT ? 7 : 10;
+		int32_t start_index = 0;
+
+		packet_infos_.clear();
+		packet_info_datas_.clear();
+		TwoLevelEdgePulse* edge_pulses_data_ptr = edge_pulses_data.GetDataAddrPtr();
+		TwoLevelEdgePulse* edge_pulses_clk_ptr = edge_pulses_clk.GetDataAddrPtr();
+
+		while (true)
+		{
+			// 检查是否取消
+			if (is_cancel_ptr_ != nullptr && *is_cancel_ptr_)
+			{
+				WriteLog(LogLevel::Level2, "AsyncFunction canceled");
+				return false;
+			}
+			// find start index
+			if (!FindStartNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr))
+			{
+				break;
+			}
+			//start_index = edge_pulses_data_ptr->start_index;
+			int32_t data_len = wave_data_len_;
+			IicEvent info = {};
+			info.has_start = true;
+			info.start_index = start_index;
+			info.start_len = 1;
+
+			//start_index = edge_pulses_clk_ptr++->start_index;
+			if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			start_index = edge_pulses_clk_ptr->start_index;
+			//if (start_index <= 0) break;
+			//start_index = edge_pulses_clk_ptr++->start_index;
+			if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			start_index = edge_pulses_clk_ptr->start_index;
+			//if (start_index <= 0) break;
+
+			info.addr_index = start_index;
+			for (int index = 0; index < addr_len; ++index)
+			{
+				if (start_index >= data_len)
+				{
+					start_index = data_len;
+					break;
+				}
+				if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
+				{
+					start_index = data_len;
+					break;
+				}
+				const bool bit = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
+				info.addr_data = static_cast<uint16_t>(info.addr_data << 1 | (bit ? 1 : 0));
+				if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+				{
+					start_index = data_len;
+					break;
+				}
+				start_index = edge_pulses_clk_ptr->start_index;
+				if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+				{
+					start_index = data_len;
+					break;
+				}
+				start_index = edge_pulses_clk_ptr->start_index;
+			}
+			info.addr_len = start_index - info.addr_index;
+
+			// 解析RW标识位
+			info.rw_index = start_index;
+			if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			info.rw = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High ? 1 : 0;//get level
+			start_index = edge_pulses_clk_ptr->start_index;
+			if (start_index <= 0 || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+
+			if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			start_index = edge_pulses_clk_ptr->start_index;
+			if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			start_index = edge_pulses_clk_ptr->start_index;
+
+			info.rw_len = start_index - info.rw_index;
+
+			//解析Addr后的ACK信号
+			info.addr_ack_index = start_index;
+			info.success_addr_ack = false;
+			if (start_index >= data_len) {
+				packet_infos_.push_back(info);
+				break;
+			}
+			if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			info.addr_ack = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;  //get level
+			if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			start_index = edge_pulses_clk_ptr->start_index;
+
+			info.addr_ack_len = start_index - info.addr_ack_index;
+
+
+			if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+			start_index = edge_pulses_clk_ptr->start_index;
+
+			if (start_index <= 0 || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+			{
+				start_index = data_len;
+				packet_infos_.push_back(info);
+				break;
+			}
+
+			std::vector<IicDataInfo> temp_infos;
+			//开始解析数据
+			while (true)
+			{
+				int stop_index = 0;
+				if (FindStopNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr))
+				{
+					stop_index = edge_pulses_data_ptr->start_index;
+					if (stop_index > 0)
+					{
+						info.has_end = true;
+						info.end_index = stop_index;
+						info.end_len = 1;
+						if (start_index >= stop_index)
+						{
+							start_index = stop_index + 1;
+							break;
+						}
+					}
+					break;
+				}
+
+				//下一帧 查找下一段启动信号
+				if (start_index > edge_pulses_clk_ptr->start_index)
+				{
+					start_index = edge_pulses_clk_ptr->start_index;
+				}
+				int32_t next_start_index = -1;
+				if (FindStartNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr) || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+				{
+					next_start_index = edge_pulses_data_ptr->start_index;
+				}
+				
+				if (start_index >= next_start_index && next_start_index != -1)
+				{
+					start_index = next_start_index - 1;
+					break;
+				}
+				IicDataInfo data_info = {};
+				data_info.index = start_index;
+
+				//解析数据
+				for (int32_t index = 0; index < 8; index++)
+				{
+					if (start_index >= data_len)
+					{
+						start_index = data_len;
+						break;
+					}
+					if (start_index >= stop_index && stop_index != -1)
+					{
+						start_index = stop_index + 1;
+						break;
+					}
+					if (start_index >= next_start_index && next_start_index != -1)
+					{
+						start_index = next_start_index - 1;
+						break;
+					}
+					data_info.data <<= 1;
+					if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
+					{
+						start_index = data_len;
+						break;
+					}
+					bool bit = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
+					data_info.data |= static_cast<uint8_t>(bit ? 1 : 0);
+
+					if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+					{
+						start_index = data_len;
+						break;
+					}
+					start_index = edge_pulses_clk_ptr->start_index;
+					if (start_index <= 0 || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+					{
+						start_index = data_len;
+						break;
+					}
+
+					if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+					{
+						start_index = data_len;
+						break;
+					}
+					start_index = edge_pulses_clk_ptr->start_index;
+					if (start_index <= 0 || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+					{
+						start_index = data_len;
+						break;
+					}
+				}
+
+				if (start_index >= data_len) break;
+				if (start_index >= stop_index && stop_index != -1)
+				{
+					start_index = stop_index + 1;
+					break;
+				}
+				if (start_index >= next_start_index && next_start_index != -1)
+				{
+					start_index = next_start_index - 1;
+					break;
+				}
+				//计算长度
+				data_info.length = stop_index - start_index;
+				//解析数据包后的ACK信号
+				data_info.ack_index = start_index;
+				if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
+				{
+					start_index = data_len;
+					break;
+				}
+				data_info.ack = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
+
+				if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+				{
+					start_index = data_len;
+					break;
+				}
+				start_index = edge_pulses_clk_ptr->start_index;
+
+				if (start_index <= 0 /*|| token.IsCancellationRequested || needclear*/)
+				{
+					start_index = data_len;
+					break;
+				}
+				data_info.ack_length = start_index - data_info.ack_index;
+				data_info.success_ack = false;
+				if (start_index >= stop_index && stop_index != -1)
+				{
+					start_index = stop_index + 1;
+					break;
+				}
+				if (start_index >= next_start_index && next_start_index != -1)
+				{
+					start_index = next_start_index - 1;
+					break;
+				}
+				temp_infos.push_back(data_info);
+
+				if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
+				{
+					start_index = data_len;
+					break;
+				}
+				start_index = edge_pulses_clk_ptr->start_index;
+
+				if (start_index <= 0 || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+				{
+					start_index = data_len;
+					break;
+				}
+			}
+			//info.data_infos = temp_infos;
+			//最后一个数据包的ACK信号为NACK
+			if (!temp_infos.empty())
+			{
+				int32_t old_data_len = static_cast<int32_t>(packet_info_datas_.size());
+				info.data_infos_count = static_cast<int32_t>(temp_infos.size());
+				// 使用std::move将temp_infos的内容移动到packet_info_datas_的末尾
+				packet_info_datas_.insert(packet_info_datas_.end(),
+					std::make_move_iterator(temp_infos.begin()), std::make_move_iterator(temp_infos.end()));
+				info.data_infos_ptr = reinterpret_cast<intptr_t> (packet_info_datas_.data() + old_data_len - 1);
+
+				info.has_end = true;
+				info.end_index = start_index;
+				info.end_len = 1;
+
+				info.success_addr_ack = true;
+			}
+
+			packet_infos_.push_back(info);
+		}
+		if (!packet_infos_.empty())
+		{
+			result.decode_event_need_update = true;
+		}
+		result.decode_event_count = packet_infos_.size();
+		result.decode_events_ptr = packet_infos_.data();
+
+		return true;
+	}
+	bool IicDecoder::FindStopNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
+		TwoLevelEdgePulse* node_scl)
+	{
+		if (start_index < 0)
+		{
+			return false;
+		}
+		const auto start_time = std::chrono::steady_clock::now();
+		while (CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
+		{
+			// 找到SDA上升沿
+			// 寻找SCL高
+			if (node_sda->start_index > start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::High)
+			{
+				node_sda++;
+				continue;
+			}
+
+			if (!CommonHelper::FindNextNode(node_sda->start_index, node_scl))
+			{
+				return false;
+			}
+
+			if (node_sda->start_index > node_scl->start_index)
+			{
+				node_scl++;
+			}
+			if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
+			{
+				return false;
+			}
+
+			// 确认下一个SCL为低电平
+			if (node_scl->current_level == TwoLevelEdgePulseStatusType::Low)
+			{
+				if (node_sda->start_index < node_scl->start_index)
+				{
+					// 找到目标停止条件
+					return true;
+				}
+			}
+			start_index = node_scl->end_index + 1;
+			if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::nanoseconds>(
+				end_time - start_time).count() >
+				DECODE_OUT_TIME_BY_SEC)
+			{
+				WriteLog(LogLevel::Level2, "IIC FindStopNode TimeOut");
+				return false;
+			}
+		}
+		return CommonHelper::CheckTwoLevelEdgePulseValid(node_sda);
+	}
+
+	bool IicDecoder::FindStartNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
+		TwoLevelEdgePulse* node_scl)
+	{
+		if (start_index < 0)
+		{
+			return false;
+		}
+		const auto start_time = std::chrono::steady_clock::now();
+		while (CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
+		{
+			// 找到SDA下降沿
+			// 寻找SCL高
+			if (node_sda->start_index < start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::Low)
+			{
+				node_sda += 1;
+				continue;
+			}
+
+			while (node_scl->start_index < start_index)
+			{
+				node_scl++;
+			}
+			if (node_sda->start_index > node_scl->start_index)
+			{
+				node_scl += 1;
+			}
+			if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
+			{
+				return false;
+			}
+
+			//找到SCL下降沿
+			if (node_scl->current_level == TwoLevelEdgePulseStatusType::Low)
+			{
+				if (node_sda->start_index < node_scl->start_index)
+				{
+					//找到目标头
+					start_index = node_sda->start_index;
+					return true;
+				}
+			}
+			start_index = node_scl->end_index + 1;
+			if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::nanoseconds>(
+				end_time - start_time).count() >
+				DECODE_OUT_TIME_BY_SEC)
+			{
+				WriteLog(LogLevel::Level2, "IIC FindStartNode TimeOut");
+				return false;
+			}
+		}
+		return CommonHelper::CheckTwoLevelEdgePulseValid(node_sda);
+	}
 }

+ 2 - 1
ProtocolIIC/iic_decoder.h

@@ -35,7 +35,8 @@ namespace Protocol
     private:
         bool* is_cancel_ptr_ = nullptr;
         int32_t wave_data_len_ = 0;
-        std::vector<IicPacket> packet_infos_;
+        std::vector<IicEvent> packet_infos_;
+        std::vector<IicDataInfo> packet_info_datas_;
         IicDecodeResult result_data_;
 
         IicEnums::DataBitWidth bit_width_ = IicEnums::DataBitWidth::DATA_BIT_WIDTH_7_BIT;

+ 50 - 2
ProtocolIIC/iic_packet.h

@@ -16,7 +16,8 @@
 
 namespace Protocol
 {
-    struct IicDataInfo {
+    struct IicDataInfo
+    {
         int32_t index;
         int32_t length;
         uint8_t data;
@@ -26,8 +27,9 @@ namespace Protocol
         bool success_ack;
         bool has_ack;
     };
+
     //内部使用数据结构 数据帧
-    struct IicPacket 
+   /* struct IicPacket
     {
         bool has_start;
         int32_t start_index;
@@ -47,5 +49,51 @@ namespace Protocol
         bool has_end;
         int32_t end_index;
         int32_t end_len;
+    };*/
+
+    struct IicEvent
+    {
+        bool has_start;
+        int32_t start_index;
+        int32_t start_len;
+        int32_t addr_index;
+        int32_t addr_len;
+        uint16_t addr_data;
+        //bool rw; bug
+        uint16_t rw; 
+
+        int32_t rw_index;
+        int32_t rw_len;
+        bool addr_ack;
+        bool success_addr_ack;
+        int32_t addr_ack_index;
+        int32_t addr_ack_len;
+        bool has_addr_ack;
+        intptr_t data_infos_ptr; // ptr to std::vector<IicDataInfo> packet_infos_
+        int32_t data_infos_count;
+        bool has_end;
+        int32_t end_index;
+        int32_t end_len;
+       /* IicEvent(IicPacket packet)
+        {
+            has_start = packet.has_start;
+            start_index = packet.start_index;
+            start_len = packet.start_len;
+            addr_index = packet.addr_index;
+            addr_len = packet.addr_len;
+            addr_data = packet.addr_data;
+            rw = packet.rw;
+            rw_index = packet.rw_index;
+            rw_len = packet.rw_len;
+            addr_ack = packet.addr_ack;
+            success_addr_ack = packet.success_addr_ack;
+            addr_ack_index = packet.addr_ack_index;
+            addr_ack_len = packet.addr_ack_len;
+            has_addr_ack = packet.has_addr_ack;
+            data_infos_ptr = reinterpret_cast<intptr_t>(packet.data_infos.data());
+            has_end = packet.has_end;
+            end_index = packet.end_index;
+            end_len = packet.end_len;
+        }*/
     };
 }

BIN
x64/Debug/ProtocolDecoder.dll


BIN
x64/Debug/ProtocolDecoder.pdb