AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
SimpleBuffer.cpp
Go to the documentation of this file.
1 
13 #include <SimpleBuffer.h>
14 
15 
16 void SimpleBuffer::init(uint8_t buflen)
17 {
18  buffer = new uint8_t [buflen];
19  maxLen = buflen;
20  idx = 1; // first usable position, [0] is for crc8
21 }
22 
24 {
25  idx = 1;
26 }
27 
28 // calculate 8-bit CRC (seems to be CRC-8/MAXIM)
29 // adapted from Nick Gammon's page at http://www.gammon.com.au/i2c
30 uint8_t SimpleBuffer::calculateCRC8 ()
31 {
32  uint8_t* addr = &buffer[1];
33  uint8_t len = idx - 1;
34  uint8_t crc = 0;
35  while (len-- > 0) {
36  uint8_t inbyte = *addr++;
37  for (uint8_t i = 8; i; i--) {
38  uint8_t mix = (crc ^ inbyte) & 0x01;
39  crc >>= 1;
40  if (mix)
41  { crc ^= 0x8C; }
42  inbyte >>= 1;
43  } // end of for
44  } // end of while
45  log("[sb_CRC8="); log(crc); log("] ");
46  return crc;
47 } // end of crc8
48 
50 {
51  buffer[0] = calculateCRC8();
52 }
53 
55 {
56  return buffer[0] == calculateCRC8();
57 }
SimpleBuffer::setCRC8
void setCRC8()
Calculate CRC8 checksum for the currently used buffer ([1]...[idx-1]) and store it in the first byte ...
Definition: SimpleBuffer.cpp:49
SimpleBuffer::checkCRC8
bool checkCRC8()
Check for correct CRC8 checksum. First byte [0] holds the checksum, rest of the currently used buffer...
Definition: SimpleBuffer.cpp:54
addr
const uint8_t addr
Definition: Interrupt_Endstop.ino:28
SimpleBuffer::idx
uint8_t idx
The position pointer. Remember, [0] holds the CRC8 checksum, so for an empty buffer,...
Definition: SimpleBuffer.h:90
SimpleBuffer::init
void init(uint8_t buflen)
Allocate and reset buffer.
Definition: SimpleBuffer.cpp:16
SimpleBuffer::reset
void reset()
Reset the position pointer to the start of the buffer (which is[1] as [0] is the CRC8 chcksum) withou...
Definition: SimpleBuffer.cpp:23
log
#define log(...)
Definition: firmware.ino:94
SimpleBuffer::maxLen
uint8_t maxLen
Maximum length of buffer in bytes. Read and write operations use this to check for sufficient space (...
Definition: SimpleBuffer.h:96
SimpleBuffer.h
Simple and ugly serialization buffer for any data type. Template technique and CRC8 adapted from Nick...
SimpleBuffer::buffer
uint8_t * buffer
The allocated buffer.
Definition: SimpleBuffer.h:84