Apollo 10.0
自动驾驶开放平台
byte.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <algorithm>
20#include <bitset>
21
23
24namespace apollo {
25namespace drivers {
26namespace canbus {
27namespace {
28
29constexpr int32_t BYTE_LENGTH = static_cast<int32_t>(sizeof(int8_t) * 8);
30
31const uint8_t RANG_MASK_1_L[] = {0x01, 0x03, 0x07, 0x0F,
32 0x1F, 0x3F, 0x7F, 0xFF};
33const uint8_t RANG_MASK_0_L[] = {0xFE, 0XFC, 0xF8, 0xF0,
34 0xE0, 0xC0, 0x80, 0x00};
35
36} // namespace
37
38Byte::Byte(const uint8_t *value) : value_(const_cast<uint8_t *>(value)) {}
39
40Byte::Byte(const Byte &value) : value_(value.value_) {}
41
42std::string Byte::byte_to_hex(const uint8_t value) {
43 static const char HEX[] = "0123456789ABCDEF";
44 uint8_t high = static_cast<uint8_t>(value >> 4);
45 uint8_t low = static_cast<uint8_t>(value & 0x0F);
46 return {HEX[high], HEX[low]};
47}
48
49std::string Byte::byte_to_hex(const uint32_t value) {
50 uint8_t high;
51 uint8_t low;
52 std::string result = "";
53 if (value > 0xFF) {
54 high = static_cast<uint8_t>((value >> 24) & 0xFF);
55 low = static_cast<uint8_t>((value >> 16) & 0xFF);
56 result += byte_to_hex(high);
57 result += byte_to_hex(low);
58 }
59 high = static_cast<uint8_t>((value >> 8) & 0xFF);
60 low = static_cast<uint8_t>(value & 0xFF);
61 result += byte_to_hex(high);
62 result += byte_to_hex(low);
63 return result;
64}
65
66std::string Byte::byte_to_binary(const uint8_t value) {
67 return std::bitset<8 * sizeof(uint8_t)>(value).to_string();
68}
69
70void Byte::set_bit_1(const int32_t pos) {
71 static const uint8_t BIT_MASK_1[] = {0x01, 0x02, 0x04, 0x08,
72 0x10, 0x20, 0x40, 0x80};
73 if (pos >= 0 && pos < BYTE_LENGTH) {
74 *value_ |= BIT_MASK_1[pos];
75 }
76}
77
78void Byte::set_bit_0(const int32_t pos) {
79 static const uint8_t BIT_MASK_0[] = {0xFE, 0xFD, 0xFB, 0xF7,
80 0xEF, 0xDF, 0xBF, 0x7F};
81 if (pos >= 0 && pos < BYTE_LENGTH) {
82 *value_ &= BIT_MASK_0[pos];
83 }
84}
85
86bool Byte::is_bit_1(const int32_t pos) const {
87 return pos >= 0 && pos < BYTE_LENGTH && ((*value_ >> pos) % 2 == 1);
88}
89
90void Byte::set_value(const uint8_t value) {
91 if (value_ != nullptr) {
92 *value_ = value;
93 }
94}
95
96void Byte::set_value_high_4_bits(const uint8_t value) {
97 set_value(value, 4, 4);
98}
99
100void Byte::set_value_low_4_bits(const uint8_t value) { set_value(value, 0, 4); }
101
102void Byte::set_value(const uint8_t value, const int32_t start_pos,
103 const int32_t length) {
104 if (start_pos > BYTE_LENGTH - 1 || start_pos < 0 || length < 1) {
105 return;
106 }
107 int32_t end_pos = std::min(start_pos + length - 1, BYTE_LENGTH - 1);
108 int32_t real_len = end_pos + 1 - start_pos;
109 uint8_t current_value_low = 0x00;
110 if (start_pos > 0) {
111 current_value_low = *value_ & RANG_MASK_1_L[start_pos - 1];
112 }
113 uint8_t current_value_high = *value_ & RANG_MASK_0_L[end_pos];
114 uint8_t middle_value = value & RANG_MASK_1_L[real_len - 1];
115 middle_value = static_cast<uint8_t>(middle_value << start_pos);
116 *value_ = static_cast<uint8_t>(current_value_high + middle_value +
117 current_value_low);
118}
119
120uint8_t Byte::get_byte() const { return *value_; }
121
122uint8_t Byte::get_byte_high_4_bits() const { return get_byte(4, 4); }
123
124uint8_t Byte::get_byte_low_4_bits() const { return get_byte(0, 4); }
125
126uint8_t Byte::get_byte(const int32_t start_pos, const int32_t length) const {
127 if (start_pos > BYTE_LENGTH - 1 || start_pos < 0 || length < 1) {
128 return 0x00;
129 }
130 int32_t end_pos = std::min(start_pos + length - 1, BYTE_LENGTH - 1);
131 int32_t real_len = end_pos + 1 - start_pos;
132 uint8_t result = static_cast<uint8_t>(*value_ >> start_pos);
133 result &= RANG_MASK_1_L[real_len - 1];
134 return result;
135}
136
137std::string Byte::to_hex_string() const { return byte_to_hex(*value_); }
138
139std::string Byte::to_binary_string() const { return byte_to_binary(*value_); }
140
141} // namespace canbus
142} // namespace drivers
143} // namespace apollo
Defines the Byte class.
The class of one byte, which is 8 bits.
Definition byte.h:39
void set_bit_1(const int32_t pos)
Set the bit on a specified position to one.
Definition byte.cc:70
static std::string byte_to_binary(const uint8_t value)
Transform an integer with the size of one byte to its binary represented by a string.
Definition byte.cc:66
uint8_t get_byte_low_4_bits() const
Get a one-byte unsigned integer representing the lower 4 bits.
Definition byte.cc:124
std::string to_hex_string() const
Transform to its hexadecimal represented by a string.
Definition byte.cc:137
void set_value_low_4_bits(const uint8_t value)
Reset the lower 4 bits as the lower 4 bits of a specified one-byte unsigned integer.
Definition byte.cc:100
bool is_bit_1(const int32_t pos) const
Check if the bit on a specified position is one.
Definition byte.cc:86
void set_bit_0(const int32_t pos)
Set the bit on a specified position to zero.
Definition byte.cc:78
uint8_t get_byte_high_4_bits() const
Get a one-byte unsigned integer representing the higher 4 bits.
Definition byte.cc:122
void set_value_high_4_bits(const uint8_t value)
Reset the higher 4 bits as the higher 4 bits of a specified one-byte unsigned integer.
Definition byte.cc:96
std::string to_binary_string() const
Transform to its binary represented by a string.
Definition byte.cc:139
void set_value(const uint8_t value)
Reset this Byte by a specified one-byte unsigned integer.
Definition byte.cc:90
Byte(const uint8_t *value)
Constructor which takes a pointer to a one-byte unsigned integer.
Definition byte.cc:38
static std::string byte_to_hex(const uint8_t value)
Transform an integer with the size of one byte to its hexadecimal represented by a string.
Definition byte.cc:42
uint8_t get_byte() const
Get the one-byte unsigned integer.
Definition byte.cc:120
class register implement
Definition arena_queue.h:37