common_helper.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // // ******************************************************************
  2. // // /\ /| @File commonhelper.cc
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-05-31
  6. // // / \\ @Modified 2024-06-24
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #include "common_helper.h"
  10. namespace Protocol
  11. {
  12. // 移除数组中第一个元素,如果它与其他元素的差异最大
  13. std::vector<int32_t> CommonHelper::RemoveFirstIfMostDifferent(const std::vector<int32_t>& array)
  14. {
  15. if (array.size() < 2)
  16. {
  17. throw std::invalid_argument("Array must contain at least two elements.");
  18. }
  19. const int32_t first_value = array[0];
  20. int32_t max_difference = 0;
  21. int32_t most_different_index = 0;
  22. // 找到与第一个元素差异最大的元素
  23. for (int32_t i = 1; i < static_cast<int32_t>(array.size()); ++i)
  24. {
  25. int32_t difference = std::abs(array[i] - first_value);
  26. if (difference > max_difference)
  27. {
  28. max_difference = difference;
  29. most_different_index = i;
  30. }
  31. }
  32. // 如果第一个元素与其他元素差异最大,则移除第一个元素
  33. if (most_different_index == 0)
  34. {
  35. return std::vector<int32_t>(array.begin() + 1, array.end());
  36. }
  37. return array; // 如果第一个元素不是差异最大的,则返回原数组
  38. }
  39. bool CommonHelper::CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node)
  40. {
  41. return node != nullptr && node->start_index >= 0 && node->end_index > 0
  42. && node->LevelCount() > 0 && node->current_level != TwoLevelEdgePulseStatusType::None;
  43. }
  44. uint8_t CommonHelper::SwapHighLowBits(const uint8_t value)
  45. {
  46. // 获取低4位
  47. const uint8_t lower_nibble = value & 0x0F;
  48. // 获取高4位
  49. const uint8_t higher_nibble = (value >> 4) & 0x0F;
  50. // 交换高低4位
  51. return static_cast<uint8_t>(higher_nibble | lower_nibble << 4);
  52. }
  53. uint8_t CommonHelper::ReverseOrderBits(uint8_t value)
  54. {
  55. value = SwapHighLowBits(value); //高低互换
  56. value = static_cast<uint8_t>((value & 0xCC) >> 2 | (value & 0x33) << 2); //交换每对相邻的位
  57. value = static_cast<uint8_t>((value & 0xAA) >> 1 | (value & 0x55) << 1); //交换每对相邻的位
  58. return value;
  59. }
  60. bool CommonHelper::GetNodeByIndex(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s)
  61. {
  62. if (index < 0 || node == nullptr) return false;
  63. if (index >= node->start_index && index <= node->end_index) return true;
  64. TwoLevelEdgePulse* temp_node = node;
  65. if (index > temp_node->end_index)
  66. {
  67. const auto start_time = std::chrono::steady_clock::now();
  68. while (CheckTwoLevelEdgePulseValid(temp_node))
  69. {
  70. if (index >= temp_node->start_index && index <= temp_node->end_index)
  71. {
  72. node = temp_node;
  73. return true;
  74. }
  75. temp_node++;
  76. if (auto end_time = std::chrono::steady_clock::now();
  77. std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() > time_out_by_s)
  78. {
  79. return false;
  80. }
  81. }
  82. }
  83. else if (index < temp_node->start_index)
  84. {
  85. const auto start_time = std::chrono::steady_clock::now();
  86. while (CheckTwoLevelEdgePulseValid(temp_node))
  87. {
  88. if (index >= temp_node->start_index && index <= temp_node->end_index)
  89. {
  90. node = temp_node;
  91. return true;
  92. }
  93. temp_node--;
  94. if (auto end_time = std::chrono::steady_clock::now();
  95. std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() > time_out_by_s)
  96. {
  97. return false;
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. bool CommonHelper::FindNextNode(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s)
  104. {
  105. if (index < 0)
  106. {
  107. return false;
  108. }
  109. if (GetNodeByIndex(index, node))
  110. {
  111. node++;
  112. if (CheckTwoLevelEdgePulseValid(node))
  113. {
  114. return true;
  115. }
  116. }
  117. /*while (CheckTwoLevelEdgePulseValid(node) && index< node->start_index)
  118. {
  119. node++;
  120. if (node->start_index > index)
  121. {
  122. break;
  123. }
  124. }
  125. node++;
  126. if (CheckTwoLevelEdgePulseValid(node) && node->start_index > index)
  127. {
  128. return true;
  129. }*/
  130. return false;
  131. }
  132. }