Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
endianness_wrappers.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_ENDIANNESS_WRAPPERS_HPP_
25#define TAPROOT_ENDIANNESS_WRAPPERS_HPP_
26
27#include <cstdint>
28#include <cstring>
29
30#include "modm/architecture/detect.hpp"
31
32namespace tap
33{
34namespace arch
35{
47template <typename T>
48inline void dataToByteArray(T data, uint8_t *bytesOut, bool forward)
49{
50 int numBytes = sizeof(T);
51 for (int i = 0; i < numBytes; i++)
52 {
53 int index = forward ? i : numBytes - 1 - i;
54 uint8_t byte = (data >> (8 * index)) & 0xff;
55 bytesOut[i] = byte;
56 }
57}
58
70template <typename T>
71inline void byteArrayToData(T *data, const uint8_t *bytesIn, bool forward)
72{
73 int numBytes = sizeof(T);
74 for (int i = 0; i < numBytes; i++)
75 {
76 int index = forward ? i : numBytes - 1 - i;
77 uint8_t byte = (*(bytesIn + index));
78 *(reinterpret_cast<uint8_t *>(data) + i) = byte;
79 }
80}
81
94template <typename T>
95void convertToLittleEndian(T data, uint8_t *bytesOut)
96{
97#if MODM_IS_LITTLE_ENDIAN
98 memcpy(bytesOut, &data, sizeof(T));
99#else
100 dataToByteArray(data, bytesOut, false);
101#endif
102}
103
116template <typename T>
117void convertToBigEndian(T data, uint8_t *bytesOut)
118{
119#if MODM_IS_LITTLE_ENDIAN
120 dataToByteArray(data, bytesOut, false);
121#else
122 memcpy(bytesOut, &data, sizeof(T));
123#endif
124}
125
139template <typename T>
140void convertFromLittleEndian(T *data, const uint8_t *bytesIn)
141{
142#if MODM_IS_LITTLE_ENDIAN
143 *data = *reinterpret_cast<const T *>(bytesIn);
144#else
145 byteArrayToData(data, bytesIn, false);
146#endif
147}
148
162template <typename T>
163void convertFromBigEndian(T *data, const uint8_t *bytesIn)
164{
165#if MODM_IS_LITTLE_ENDIAN
166 byteArrayToData(data, bytesIn, false);
167#else
168 *data = *reinterpret_cast<const T *>(bytesIn);
169#endif
170}
171
178inline float bigEndianInt16ToFloat(const uint8_t *buff)
179{
180 return static_cast<float>(static_cast<int16_t>((*(buff)) | (*(buff + 1) << 8)));
181}
182
183} // namespace arch
184} // namespace tap
185
186#endif // TAPROOT_ENDIANNESS_WRAPPERS_HPP_
IUnit i
Definition dimensional_smooth_pid.hpp:2
void convertToBigEndian(T data, uint8_t *bytesOut)
Definition endianness_wrappers.hpp:117
void convertFromBigEndian(T *data, const uint8_t *bytesIn)
Definition endianness_wrappers.hpp:163
void byteArrayToData(T *data, const uint8_t *bytesIn, bool forward)
Definition endianness_wrappers.hpp:71
void dataToByteArray(T data, uint8_t *bytesOut, bool forward)
Definition endianness_wrappers.hpp:48
float bigEndianInt16ToFloat(const uint8_t *buff)
Definition endianness_wrappers.hpp:178
void convertFromLittleEndian(T *data, const uint8_t *bytesIn)
Definition endianness_wrappers.hpp:140
void convertToLittleEndian(T data, uint8_t *bytesOut)
Definition endianness_wrappers.hpp:95
Definition ballistics.cpp:29