diff --git a/src/SensirionI2CTxFrame.cpp b/src/SensirionI2CTxFrame.cpp index a803ee2..b55242c 100644 --- a/src/SensirionI2CTxFrame.cpp +++ b/src/SensirionI2CTxFrame.cpp @@ -37,8 +37,9 @@ #include "SensirionErrors.h" SensirionI2CTxFrame::SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize, - size_t index) - : _buffer(buffer), _bufferSize(bufferSize), _index(index) { + size_t numCommandBytes) + : _buffer(buffer), _bufferSize(bufferSize), _index(numCommandBytes), + _numCommandBytes(numCommandBytes) { } SensirionI2CTxFrame::SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize) @@ -57,7 +58,8 @@ SensirionI2CTxFrame SensirionI2CTxFrame::createWithUInt16Command(uint16_t command, uint8_t buffer[], size_t bufferSize) { SensirionI2CTxFrame instance = SensirionI2CTxFrame(buffer, bufferSize, 2); - instance.addCommand(command); + instance._buffer[0] = static_cast((command & 0xFF00) >> 8); + instance._buffer[1] = static_cast((command & 0x00FF) >> 0); return instance; } @@ -144,7 +146,7 @@ uint16_t SensirionI2CTxFrame::_addByte(uint8_t data) { return TxFrameError | BufferSizeError; } _buffer[_index++] = data; - if (_index % 2 == 0) { + if ((_index - _numCommandBytes) % 3 == 2) { if (_bufferSize <= _index) { return TxFrameError | BufferSizeError; } diff --git a/src/SensirionI2CTxFrame.h b/src/SensirionI2CTxFrame.h index 2343804..27660c5 100644 --- a/src/SensirionI2CTxFrame.h +++ b/src/SensirionI2CTxFrame.h @@ -175,7 +175,8 @@ class SensirionI2CTxFrame { uint16_t addBytes(const uint8_t data[], size_t dataLength); private: - SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize, size_t index); + SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize, + size_t numCommandBytes); static uint8_t _generateCRC(const uint8_t* data, size_t count); @@ -184,6 +185,7 @@ class SensirionI2CTxFrame { uint8_t* _buffer; size_t _bufferSize; size_t _index; + size_t _numCommandBytes; }; #endif /* SENSIRION_I2C_TX_FRAME_H_ */