From 8a9a690a2a33d0292ec5d490d867c1153a8bef44 Mon Sep 17 00:00:00 2001 From: walchko Date: Wed, 22 Nov 2023 14:55:05 -0700 Subject: [PATCH] update --- cpp/examples/crc.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++ cpp/src/crc.hpp | 40 ++++++++++++++++++++++++++ cpp/src/yivopack.hpp | 16 +++++++++++ readme.md | 3 ++ 4 files changed, 126 insertions(+) create mode 100644 cpp/examples/crc.cpp create mode 100644 cpp/src/crc.hpp diff --git a/cpp/examples/crc.cpp b/cpp/examples/crc.cpp new file mode 100644 index 0000000..6641907 --- /dev/null +++ b/cpp/examples/crc.cpp @@ -0,0 +1,67 @@ +#include +#include + +#define CRC8_POLYNOMIAL 0x07 + +// Function to generate CRC-8 lookup table +void generate8(uint8_t table[256]) { + for (uint16_t i = 0; i < 256; ++i) { + uint8_t crc = i; + for (size_t j = 1; j < 8; j++) { + crc = (crc & 0x80) ? (crc << 1) ^ CRC8_POLYNOMIAL : crc << 1; + } + table[i] = crc; + } +} + +#define CRC16_POLYNOMIAL 0x8005 + +// Function to generate CRC-16 lookup table +void generate16(uint16_t table[256]) { + for (uint16_t i = 0; i < 256; ++i) { + uint16_t crc = i; + for (int j = 0; j < 8; ++j) { + crc = (crc & 1) ? (crc >> 1) ^ CRC16_POLYNOMIAL : (crc >> 1); + } + table[i] = crc; + } +} + +int main() { + // Example data + // uint8_t data[] = "Hello, CRC!"; + uint16_t table[256]; + uint8_t table8[256]; + + generate8(table8); + + // Print the result + for (size_t i=0; i<16; ++i) { + for (size_t j=0; j<16; ++j) { + // printf("0x%02X,", table8[i*16+j]); + printf("%u,", table8[i*16+j]); + } + printf("\n"); + } + + printf("--------------------------\n"); + + generate16(table); + + // Print the result + for (size_t i=0; i<16; ++i) { + for (size_t j=0; j<16; ++j) { + printf("0x%04X,", table[i*16+j]); + } + printf("\n"); + } + + /* + crc = 0 + for byte in message: + crcByte = byte^crc + crc = table[crcByte] + */ + + return 0; +} \ No newline at end of file diff --git a/cpp/src/crc.hpp b/cpp/src/crc.hpp new file mode 100644 index 0000000..b0f21ec --- /dev/null +++ b/cpp/src/crc.hpp @@ -0,0 +1,40 @@ + +#pragma once + +#include + +// // CRC-8 polynomial (0x07) +// #define CRC8_POLYNOMIAL 0x07 + +// // Function to calculate CRC-8 checksum +// uint8_t crc8(const uint8_t *data, size_t length) { +// uint8_t crc = 0; + +// for (size_t i = 0; i < length; ++i) { +// crc ^= data[i]; + +// for (int j = 0; j < 8; ++j) { +// crc = (crc & 0x80) ? (crc << 1) ^ CRC8_POLYNOMIAL : (crc << 1); +// } +// } + +// return crc; +// } + +// // CRC-16 polynomial (0x8005) +// #define CRC16_POLYNOMIAL 0x8005 + +// // Function to calculate CRC-16 checksum +// uint16_t crc16(const uint8_t *data, size_t length) { +// uint16_t crc = 0xFFFF; + +// for (size_t i = 0; i < length; ++i) { +// crc ^= (uint16_t)data[i]; + +// for (int j = 0; j < 8; ++j) { +// crc = (crc & 0x0001) ? (crc >> 1) ^ CRC16_POLYNOMIAL : (crc >> 1); +// } +// } + +// return crc; +// } \ No newline at end of file diff --git a/cpp/src/yivopack.hpp b/cpp/src/yivopack.hpp index 85a47f4..3c60d66 100644 --- a/cpp/src/yivopack.hpp +++ b/cpp/src/yivopack.hpp @@ -39,9 +39,25 @@ class YivoPack_t: public std::vector { uint8_t msgid() { return (*this)[4]; } uint16_t playload_size() { return ((*this)[3] << 8) | (*this)[2];} uint8_t checksum() { return (*this)[size()-1]; } + void set_checksum() { (*this)[size()-1]; } // uint16_t playload_size() { return size() - 6;} + uint8_t calc_checksum() { + uint8_t cs = 0; + uint16_t payload_size = this->payload_size(); + uint8_t *data = this->data(); + for (uint16_t i = 0; i < payload_size; ++i) { + cs ^= data[2+i]; + } + return cs; + } + bool has_valide_chksum() { + return this->calc_checksum() == this->checksum(); + } }; +typedef YivoPack_t ypkt_t; // rename to this? + + namespace yivo { static std::string to_string(const YivoPack_t& msg) { diff --git a/readme.md b/readme.md index e6170cc..86bb562 100644 --- a/readme.md +++ b/readme.md @@ -30,6 +30,9 @@ Trying to standardize the way I access sensors. not dependant on the message format other than, it is a `struct` - [x] Make a header only library - [ ] Generate C and Python messages from a template instead of writing independently +- [ ] rename `YivoPack_t` to something else like `ypkt_t` + - [ ] Add method `bool has_valid_chksum()`? Class could calculate checksum and then compare + to its checksum ## Python