AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
SimpleBuffer.h
Go to the documentation of this file.
1 
16 #ifndef SimpleBuffer_h
17 #define SimpleBuffer_h
18 
19 // #define DEBUG // uncomment for debug output to Serial (which has to be begun() in the main sketch)
20 
21 #include <Arduino.h>
22 
23 #ifdef DEBUG
24 #define log(...) Serial.print(__VA_ARGS__)
25 #else
26 #define log(...)
27 #endif // DEBUG
28 
29 
31 {
32 public:
38  void init(uint8_t buflen);
39 
45  template <typename T> void write(const T& value);
46 
54  template <typename T> void read(T& value);
55 
61  void reset();
62 
63 
68  void setCRC8();
69 
75  bool checkCRC8();
76 
77 
78  // I guess a proper class would make the next three private or safeguard them
79  // with access methods. Let's keep 'em public for quick'n'dirty direct access.
80 
84  uint8_t* buffer;
85 
90  uint8_t idx; // next position to write to / read from
91 
96  uint8_t maxLen;
97 
98 private:
99  uint8_t calculateCRC8 ();
100 };
101 
102 
103 template <typename T> void SimpleBuffer::write(const T& value)
104 {
105  if (idx + sizeof (value) <= maxLen) { // enough space to write to?
106  memcpy(&buffer[idx], &value, sizeof (value));
107  idx += sizeof (value);
108  }
109 }
110 
111 // T should be initiated before calling this, as it might return unchanged due to if() statement
112 template <typename T> void SimpleBuffer::read(T& value)
113 {
114  if (idx + sizeof (value) <= maxLen) { // enough space to read from?
115  memcpy(&value, &buffer[idx], sizeof (value));
116  idx += sizeof (value);
117  }
118 }
119 
120 
121 #endif
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
SimpleBuffer
Definition: SimpleBuffer.h:30
SimpleBuffer::read
void read(T &value)
Read any basic data type from the buffer from the current position and increment the position pointer...
Definition: SimpleBuffer.h:112
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
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::buffer
uint8_t * buffer
The allocated buffer.
Definition: SimpleBuffer.h:84
SimpleBuffer::write
void write(const T &value)
Write any basic data type to the buffer at the current position and increment the position pointer ac...
Definition: SimpleBuffer.h:103