Skip to content

Commit

Permalink
feat: add automatic detection of system endianess
Browse files Browse the repository at this point in the history
  • Loading branch information
vtx22 committed Mar 9, 2025
1 parent 20d467c commit 51deba7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/sparq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ int SPARQ::init()
{
std::cout << "\n=== SPARQ " << SPARQ_VERSION << " ===\n\n";
std::cout << "Initializing ...\n\n";
std::cout << "System Endianess: " << (sparq_is_little_endian() ? "Little Endian" : "Big Endian") << "\n";

object_init();

Expand Down
4 changes: 1 addition & 3 deletions src/sparq_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@

#define SPARQ_RECEIVE_LOOP_DELAY 1ms
#define SPARQ_NOTIFY_DURATION_OK 3000
#define SPARQ_NOTIFY_DURATION_ERR 5000

constexpr bool SPARQ_PLATFORM_LITTLE_ENDIAN = true;
#define SPARQ_NOTIFY_DURATION_ERR 5000
8 changes: 7 additions & 1 deletion src/sparq_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#define SPARQ_MAX_MESSAGE_LENGTH (SPARQ_MESSAGE_HEADER_LENGTH + SPARQ_MAX_PAYLOAD_LENGTH + SPARQ_CHECKSUM_LENGTH)
#define SPARQ_DEFAULT_SIGNATURE 0xFF

constexpr bool sparq_is_little_endian()
{
constexpr uint32_t value = 0x01020304;
return reinterpret_cast<const uint8_t *>(&value)[0] == 0x04;
}

enum class sparq_header_control_t : uint8_t
{
LSB_FIRST = (1 << 7),
Expand Down Expand Up @@ -121,7 +127,7 @@ struct sparq_message_t
uint32_t value32 = 0;

bool lsb_first = header.control & (uint8_t)sparq_header_control_t::LSB_FIRST;
if (lsb_first == SPARQ_PLATFORM_LITTLE_ENDIAN)
if (lsb_first == sparq_is_little_endian())
{
value32 = (v3 << 24) + (v2 << 16) + (v1 << 8) + v0;
}
Expand Down

0 comments on commit 51deba7

Please sign in to comment.