Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
walchko committed Nov 22, 2023
1 parent 9774891 commit 8a9a690
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
67 changes: 67 additions & 0 deletions cpp/examples/crc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdint.h>

#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;
}
40 changes: 40 additions & 0 deletions cpp/src/crc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#pragma once

#include <cstdint>

// // 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;
// }
16 changes: 16 additions & 0 deletions cpp/src/yivopack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,25 @@ class YivoPack_t: public std::vector<uint8_t> {
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) {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8a9a690

Please sign in to comment.