17 static constexpr uint8_t REDUX_CAN_ID = 14;
25 return (fullId >> 24) & 0x1f;
36 return (fullId >> 14) & 0x3;
47 return (fullId >> 6) & 0xff;
70 constexpr bool idMatches(uint32_t idToCompare, uint8_t deviceType, uint8_t devId) {
71 return (idToCompare & 0x1f00003fu) == (((uint32_t) deviceType << 24u) | devId);
83 return (deviceType << 24) | (REDUX_CAN_ID << 16) | (msgId << 6) | (devId);
94 constexpr std::chrono::duration<double, std::ratio<1LL, 1LL>>
toChronoSeconds(units::second_t seconds) {
95 return seconds.to<
double>() * std::chrono::seconds(1);
105 constexpr void memcpyLE(
void* dst,
void* src,
size_t len) {
107 uint8_t* dst8 = (uint8_t*) dst;
108 uint8_t* src8 = (uint8_t*) src;
110 if constexpr (std::endian::native == std::endian::little) {
112 memcpy(dst, src, len);
114 for (
size_t i = 0; i < len; i++) {
120 for (
size_t i = 0; i < len; i++) {
121 dst8[len - i - 1] = src8[i];
133 constexpr uint8_t
extractU8(uint64_t data, uint8_t width, uint8_t offset) {
134 uint64_t mask = ((1ull) << width) - 1;
135 return static_cast<uint8_t
>((data >> offset) & mask);
145 constexpr uint16_t
extractU16(uint64_t data, uint8_t width, uint8_t offset) {
146 uint64_t mask = ((1ull) << width) - 1;
147 return static_cast<uint16_t
>((data >> offset) & mask);
157 constexpr uint32_t
extractU32(uint64_t data, uint8_t width, uint8_t offset) {
158 uint64_t mask = ((1ull) << width) - 1;
159 return static_cast<uint32_t
>((data >> offset) & mask);
169 constexpr uint64_t
extractU64(uint64_t data, uint8_t width, uint8_t offset) {
170 uint64_t mask = ((1ull) << width) - 1;
171 return static_cast<uint64_t
>((data >> offset) & mask);
182 constexpr int8_t
extractI8(uint64_t data, uint8_t width, uint8_t offset) {
183 const size_t BIT_WIDTH = (
sizeof(int8_t) << 3);
184 int8_t result =
static_cast<int8_t
>(
extractU8(data, width, offset));
185 if (width >= BIT_WIDTH) {
return result; }
186 uint8_t shift = (BIT_WIDTH - width);
187 return (result << shift) >> shift;
197 constexpr int16_t
extractI16(uint64_t data, uint8_t width, uint8_t offset) {
198 const size_t BIT_WIDTH = (
sizeof(int16_t) << 3);
199 int16_t result =
static_cast<int16_t
>(
extractU16(data, width, offset));
200 if (width >= BIT_WIDTH) {
return result; }
201 uint8_t shift = (BIT_WIDTH - width);
202 return (result << shift) >> shift;
212 constexpr int32_t
extractI32(uint64_t data, uint8_t width, uint8_t offset) {
213 const size_t BIT_WIDTH = (
sizeof(int32_t) << 3);
214 int32_t result =
static_cast<int32_t
>(
extractU32(data, width, offset));
215 if (width >= BIT_WIDTH) {
return result; }
216 uint8_t shift = (BIT_WIDTH - width);
217 return (result << shift) >> shift;
227 constexpr int64_t
extractI64(uint64_t data, uint8_t width, uint8_t offset) {
228 const size_t BIT_WIDTH = (
sizeof(int64_t) << 3);
229 int64_t result =
static_cast<int64_t
>(
extractU64(data, width, offset));
230 if (width >= BIT_WIDTH) {
return result; }
231 uint8_t shift = (BIT_WIDTH - width);
232 return (result << shift) >> shift;
246 uint32_t uvalue =
static_cast<uint32_t
>((data >> offset) & 0xffffff) << 8;
261 uint32_t uvalue =
static_cast<uint32_t
>(data >> offset);
262 memcpyLE(&fvalue, &uvalue,
sizeof(
float));
274 memcpyLE(&dvalue, &data,
sizeof(
double));
285 return ((data >> offset) & 1) != 0;
295 constexpr uint64_t
packUInt(uint64_t data, uint8_t width, uint8_t offset) {
296 uint64_t mask = ((1ull) << width) - 1;
297 return (data & mask) << offset;
307 constexpr uint64_t
packInt(int64_t data, uint8_t width, uint8_t offset) {
308 uint64_t mask = ((1ull) << width) - 1;
309 return (
static_cast<uint64_t
>(data) & mask) << offset;
319 constexpr uint64_t
packF24(
float data, uint8_t offset) {
321 memcpyLE(&udata, &data,
sizeof(
float));
323 return static_cast<uint64_t
>(udata) << offset;
332 constexpr uint64_t
packF32(
float data, uint8_t offset) {
334 memcpyLE(&udata, &data,
sizeof(
float));
335 return static_cast<uint64_t
>(udata) << offset;
344 constexpr uint64_t
packF64(
double data, uint8_t offset) {
346 memcpyLE(&udata, &data,
sizeof(
double));
347 return udata << offset;
356 constexpr uint64_t
packBool(
bool data, uint8_t offset) {
357 return static_cast<uint64_t
>(data) << offset;
365 template <
typename E>
366 constexpr typename std::underlying_type<E>::type
to_underlying(E e)
noexcept {
367 return static_cast<typename std::underlying_type<E>::type
>(e);
Definition: CanandUtils.h:15
constexpr uint8_t getDeviceType(uint32_t fullId)
Definition: CanandUtils.h:24
constexpr uint64_t packInt(int64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:307
constexpr float extractF32(uint64_t data, uint8_t offset)
Definition: CanandUtils.h:259
constexpr uint8_t extractU8(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:133
constexpr uint8_t getDeviceId(uint32_t fullId)
Definition: CanandUtils.h:57
constexpr int32_t extractI32(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:212
constexpr double extractF64(uint64_t data)
Definition: CanandUtils.h:272
constexpr bool idMatches(uint32_t idToCompare, uint8_t deviceType, uint8_t devId)
Definition: CanandUtils.h:70
constexpr bool extractBool(uint64_t data, uint8_t offset)
Definition: CanandUtils.h:284
constexpr uint64_t extractU64(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:169
constexpr int16_t extractI16(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:197
constexpr std::chrono::duration< double, std::ratio< 1LL, 1LL > > toChronoSeconds(units::second_t seconds)
Definition: CanandUtils.h:94
constexpr uint32_t constructMessageId(uint8_t deviceType, uint16_t devId, uint8_t msgId)
Definition: CanandUtils.h:82
constexpr uint64_t packF32(float data, uint8_t offset)
Definition: CanandUtils.h:332
constexpr uint64_t packF64(double data, uint8_t offset)
Definition: CanandUtils.h:344
constexpr void memcpyLE(void *dst, void *src, size_t len)
Definition: CanandUtils.h:105
constexpr uint8_t getApiIndex(uint32_t fullId)
Definition: CanandUtils.h:46
constexpr uint8_t getApiPage(uint32_t fullId)
Definition: CanandUtils.h:35
constexpr uint16_t extractU16(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:145
constexpr std::underlying_type< E >::type to_underlying(E e) noexcept
Definition: CanandUtils.h:366
constexpr uint64_t packUInt(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:295
constexpr int8_t extractI8(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:182
constexpr uint32_t extractU32(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:157
constexpr float extractF24(uint64_t data, uint8_t offset)
Definition: CanandUtils.h:244
constexpr int64_t extractI64(uint64_t data, uint8_t width, uint8_t offset)
Definition: CanandUtils.h:227
constexpr uint64_t packBool(bool data, uint8_t offset)
Definition: CanandUtils.h:356
constexpr uint64_t packF24(float data, uint8_t offset)
Definition: CanandUtils.h:319