// // ****************************************************************** // // /\ /| @File commonhelper.cc // // \ V/ @Brief // // | "") @Author lijinwen, ghz005@uni-trend.com.cn // // / | @Creation 2024-05-31 // // / \\ @Modified 2024-06-24 // // *(__\_\ // // ****************************************************************** #include "common_helper.h" namespace Protocol { // 移除数组中第一个元素,如果它与其他元素的差异最大 std::vector CommonHelper::RemoveFirstIfMostDifferent(const std::vector& 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(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(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 && 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(higher_nibble | lower_nibble << 4); } uint8_t CommonHelper::ReverseOrderBits(uint8_t value) { value = SwapHighLowBits(value); //高低互换 value = static_cast((value & 0xCC) >> 2 | (value & 0x33) << 2); //交换每对相邻的位 value = static_cast((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 || node == nullptr) 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(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(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; } }