forked from joaobarroca93/MLX90615
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crc8.cpp
39 lines (33 loc) · 1.16 KB
/
Crc8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "Crc8.h"
/**************************************************************************************************/
/* CRC8 helper class functions. */
/**************************************************************************************************/
/**
* \brief CRC8 class constructor.
* \param [in] poly 8 bit CRC polynomial to use.
*/
CRC8::CRC8(uint8_t poly) {crc8Start(poly);}
/**
* \brief Return the current value of the CRC.
* \return 8 bit CRC current value.
*/
uint8_t CRC8::crc8(void) {return _crc;}
/**
* \brief Update the current value of the CRC.
* \param [in] data New 8 bit data to be added to the CRC.
* \return 8 bit CRC current value.
*/
uint8_t CRC8::crc8(uint8_t data) {
uint8_t i = 8;
_crc ^= data;
while(i--) _crc = _crc & 0x80 ? (_crc << 1) ^ _poly : _crc << 1;
return _crc;
}
/**
* \brief Initialize the CRC8 object.
* \param [in] poly 8 bit CRC polynomial to use.
*/
void CRC8::crc8Start(uint8_t poly) {
_poly = poly;
_crc = 0;
}