-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce7282c
commit f2f30b8
Showing
3 changed files
with
118 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
|
||
#include "lib/CRC8.h" | ||
|
||
|
||
CRC8::CRC8(void) { | ||
|
||
|
||
} | ||
|
||
void CRC8::begin(void) { | ||
crc remainder; | ||
|
||
for (int dividend = 0; dividend < 256; ++dividend) | ||
{ | ||
remainder = dividend << (WIDTH - 8); | ||
|
||
|
||
for (uint8_t bit = 8; bit > 0; --bit) | ||
{ | ||
if (remainder & TOPBIT) | ||
{ | ||
remainder = (remainder << 1) ^ POLYNOMIAL; | ||
} | ||
else | ||
{ | ||
remainder = (remainder << 1); | ||
} | ||
} | ||
|
||
crcTable[dividend] = remainder; | ||
} | ||
} | ||
|
||
|
||
crc CRC8::get_crc8(uint8_t const message[], int nBytes) { | ||
uint8_t data; | ||
crc remainder = 0; | ||
|
||
|
||
for (int byte = 0; byte < nBytes; ++byte) | ||
{ | ||
data = message[byte] ^ (remainder >> (WIDTH - 8)); | ||
remainder = crcTable[data] ^ (remainder << 8); | ||
} | ||
|
||
|
||
return (remainder); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* CRC8 library | ||
*/ | ||
#ifndef CRC8_H | ||
#define CRC8_H | ||
|
||
#if ARDUINO >= 100 | ||
#include "Arduino.h" | ||
#else | ||
#include "WProgram.h" | ||
#endif | ||
|
||
typedef uint8_t crc; | ||
#define POLYNOMIAL 0xd5 /* CRC8_CCITT -- this polynomial needs to match choice on javascript end */ | ||
#define WIDTH (8 * sizeof(crc)) | ||
#define TOPBIT (1 << (WIDTH - 1)) | ||
|
||
class CRC8 { | ||
public: | ||
CRC8(); | ||
void begin(); | ||
crc get_crc8(uint8_t const message[], int nBytes); | ||
|
||
private: | ||
uint8_t crcTable[256]; | ||
|
||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters