CAN CoreX 2.3.1
Table-driven CAN, CAN FD, ISO-TP, network simulation, and bus monitoring library
Loading...
Searching...
No Matches
can_corex_utils.h File Reference

Optional utility helpers for CAN CoreX applications. More...

#include "can_corex.h"
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define CCX_SLCAN_CLASSIC_MAX_LINE_LEN   28U
 Maximum NUL-terminated classic SLCAN data-frame line length.

Enumerations

enum  CCX_UTILS_Status_t {
  CCX_UTILS_OK = 0 , CCX_UTILS_NULL_PTR , CCX_UTILS_WRONG_ARG , CCX_UTILS_BUFFER_TOO_SMALL ,
  CCX_UTILS_UNSUPPORTED
}
 Status values returned by CAN CoreX utility helpers. More...

Functions

CCX_UTILS_Status_t CCX_SLCAN_Parse (const char *line, CCX_message_t *msg)
 Parse a classic SLCAN data-frame line into a CAN CoreX message.
CCX_UTILS_Status_t CCX_SLCAN_Format (const CCX_message_t *msg, char *out, size_t out_size)
 Format a CAN CoreX classic CAN message as an SLCAN data-frame line.
CCX_UTILS_Status_t CCX_BytesToHex (const uint8_t *bytes, size_t byte_count, char *out, size_t out_size)
 Convert bytes to an uppercase hex string.
CCX_UTILS_Status_t CCX_HexToBytes (const char *hex, size_t hex_len, uint8_t *out, size_t out_size, size_t *bytes_written)
 Convert a hex string to bytes.
Double-based linear signal helpers

These helpers implement the common CAN signal scaling model:

  • decode: physical = raw * factor + offset
  • encode: raw = round((physical - offset) / factor)

Encode/decode preserves the physical value exactly when the input physical value lies on the representable signal grid and the floating-point type can represent the intermediate values exactly enough. Off-grid physical values are quantized to the nearest raw integer first; decoding returns that nearest representable physical value, not necessarily the original input.

The 64-bit helpers are useful for large counters and scaled values, but double cannot represent every 64-bit integer exactly. Exact round-trips are guaranteed only within the precision limits of the floating-point type and the selected factor/offset.

If factor is zero, encode returns the minimum value for the target type.

uint8_t CCX_EncodeLinearU8_Clamped (double physical, double factor, double offset)
uint16_t CCX_EncodeLinearU16_Clamped (double physical, double factor, double offset)
uint32_t CCX_EncodeLinearU32_Clamped (double physical, double factor, double offset)
uint64_t CCX_EncodeLinearU64_Clamped (double physical, double factor, double offset)
int8_t CCX_EncodeLinearS8_Clamped (double physical, double factor, double offset)
int16_t CCX_EncodeLinearS16_Clamped (double physical, double factor, double offset)
int32_t CCX_EncodeLinearS32_Clamped (double physical, double factor, double offset)
int64_t CCX_EncodeLinearS64_Clamped (double physical, double factor, double offset)
double CCX_DecodeLinearU8 (uint8_t raw, double factor, double offset)
double CCX_DecodeLinearU16 (uint16_t raw, double factor, double offset)
double CCX_DecodeLinearU32 (uint32_t raw, double factor, double offset)
double CCX_DecodeLinearU64 (uint64_t raw, double factor, double offset)
double CCX_DecodeLinearS8 (int8_t raw, double factor, double offset)
double CCX_DecodeLinearS16 (int16_t raw, double factor, double offset)
double CCX_DecodeLinearS32 (int32_t raw, double factor, double offset)
double CCX_DecodeLinearS64 (int64_t raw, double factor, double offset)
Float-based linear signal helpers

Same behavior as the double-based family, but all physical values, factors, offsets, and decoded values use float.

This is useful on MCUs where float is the preferred application type. Precision is lower than the double family: not all 32-bit or 64-bit raw values can be represented exactly as float.

If factor is zero, encode returns the minimum value for the target type.

uint8_t CCX_EncodeLinearU8F_Clamped (float physical, float factor, float offset)
uint16_t CCX_EncodeLinearU16F_Clamped (float physical, float factor, float offset)
uint32_t CCX_EncodeLinearU32F_Clamped (float physical, float factor, float offset)
uint64_t CCX_EncodeLinearU64F_Clamped (float physical, float factor, float offset)
int8_t CCX_EncodeLinearS8F_Clamped (float physical, float factor, float offset)
int16_t CCX_EncodeLinearS16F_Clamped (float physical, float factor, float offset)
int32_t CCX_EncodeLinearS32F_Clamped (float physical, float factor, float offset)
int64_t CCX_EncodeLinearS64F_Clamped (float physical, float factor, float offset)
float CCX_DecodeLinearU8F (uint8_t raw, float factor, float offset)
float CCX_DecodeLinearU16F (uint16_t raw, float factor, float offset)
float CCX_DecodeLinearU32F (uint32_t raw, float factor, float offset)
float CCX_DecodeLinearU64F (uint64_t raw, float factor, float offset)
float CCX_DecodeLinearS8F (int8_t raw, float factor, float offset)
float CCX_DecodeLinearS16F (int16_t raw, float factor, float offset)
float CCX_DecodeLinearS32F (int32_t raw, float factor, float offset)
float CCX_DecodeLinearS64F (int64_t raw, float factor, float offset)

Detailed Description

Optional utility helpers for CAN CoreX applications.