Skip to content

Commit 85850aa

Browse files
authored
Merge pull request #13 from Sensirion/fix-compiler-warnings
Fix compiler warnings
2 parents 0b6e552 + 409c696 commit 85850aa

File tree

7 files changed

+69
-37
lines changed

7 files changed

+69
-37
lines changed

.github/workflows/arduino_checks.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Arduino Checks
1+
name: Arduino-Checks
22

33
on:
44
pull_request:
@@ -14,3 +14,4 @@ jobs:
1414
with:
1515
expect-arduino-examples: true
1616
lint-lib-manager-check: update
17+
dependency-list: "[{'source-path': './'}]"

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
88

99
`Unreleased`_
1010
-------------
11+
- Fix compiler warnings
1112
- update to SensirionShdlcTxFrame::begin so the buffer is always filled from position 0.
12-
. update to SensirionShdlcCommunication::receiveFrame - if an error frame is recieved, the data part of the frame should not be read even if the length is not 0.
13+
- update to SensirionShdlcCommunication::receiveFrame - if an error frame is recieved, the data part of the frame should not be read even if the length is not 0.
1314

1415
`0.7.1`_ 2024-04-30
1516
-------------------

src/SensirionI2CCommunication.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,26 @@ uint16_t SensirionI2CCommunication::sendFrame(uint8_t address,
5252
size_t writtenBytes = i2cBus.write(frame._buffer, frame._index);
5353
uint8_t i2c_error = i2cBus.endTransmission();
5454
if (writtenBytes != frame._index) {
55-
return WriteError | I2cOtherError;
55+
return static_cast<uint16_t>(WriteError) |
56+
static_cast<uint8_t>(I2cOtherError);
5657
}
5758
// translate Arduino errors, see
5859
// https://www.arduino.cc/en/Reference/WireEndTransmission
5960
switch (i2c_error) {
6061
case 0:
6162
return NoError;
6263
case 1:
63-
return WriteError | InternalBufferSizeError;
64+
return static_cast<uint16_t>(WriteError) |
65+
static_cast<uint8_t>(InternalBufferSizeError);
6466
case 2:
65-
return WriteError | I2cAddressNack;
67+
return static_cast<uint16_t>(WriteError) |
68+
static_cast<uint8_t>(I2cAddressNack);
6669
case 3:
67-
return WriteError | I2cDataNack;
70+
return static_cast<uint16_t>(WriteError) |
71+
static_cast<uint8_t>(I2cDataNack);
6872
default:
69-
return WriteError | I2cOtherError;
73+
return static_cast<uint16_t>(WriteError) |
74+
static_cast<uint8_t>(I2cOtherError);
7075
}
7176
}
7277

@@ -87,10 +92,12 @@ uint16_t SensirionI2CCommunication::receiveFrame(uint8_t address,
8792
#endif
8893

8994
if (numBytes % 3) {
90-
return ReadError | WrongNumberBytesError;
95+
return static_cast<uint16_t>(ReadError) |
96+
static_cast<uint8_t>(WrongNumberBytesError);
9197
}
9298
if ((numBytes / 3) * 2 > frame._bufferSize) {
93-
return ReadError | BufferSizeError;
99+
return static_cast<uint16_t>(ReadError) |
100+
static_cast<uint8_t>(BufferSizeError);
94101
}
95102
size_t i = 0;
96103
int16_t remaining = numBytes;
@@ -104,7 +111,8 @@ uint16_t SensirionI2CCommunication::receiveFrame(uint8_t address,
104111
uint8_t available = i2cBus.requestFrom(address, bytesToRead,
105112
static_cast<uint8_t>(stop));
106113
if (bytesToRead != available) {
107-
return ReadError | NotEnoughDataError;
114+
return static_cast<uint16_t>(ReadError) |
115+
static_cast<uint8_t>(NotEnoughDataError);
108116
}
109117
while (available > 0) {
110118
frame._buffer[i++] = i2cBus.read();
@@ -113,7 +121,8 @@ uint16_t SensirionI2CCommunication::receiveFrame(uint8_t address,
113121
uint8_t expectedCRC = generateCRC(&frame._buffer[i - 2], 2, poly);
114122
if (actualCRC != expectedCRC) {
115123
clearRxBuffer(i2cBus);
116-
return ReadError | CRCError;
124+
return static_cast<uint16_t>(ReadError) |
125+
static_cast<uint8_t>(CRCError);
117126
}
118127
available -= 3;
119128
}

src/SensirionI2CTxFrame.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ SensirionI2CTxFrame SensirionI2CTxFrame::createWithUInt16Command(
6868

6969
uint16_t SensirionI2CTxFrame::addCommand(uint16_t command) {
7070
if (_bufferSize < 2) {
71-
return TxFrameError | BufferSizeError;
71+
return static_cast<uint16_t>(TxFrameError) |
72+
static_cast<uint8_t>(BufferSizeError);
7273
}
7374
_buffer[0] = static_cast<uint8_t>((command & 0xFF00) >> 8);
7475
_buffer[1] = static_cast<uint8_t>((command & 0x00FF) >> 0);
@@ -130,12 +131,14 @@ uint16_t SensirionI2CTxFrame::addBytes(const uint8_t data[],
130131

131132
uint16_t SensirionI2CTxFrame::_addByte(uint8_t data) {
132133
if (_bufferSize <= _index) {
133-
return TxFrameError | BufferSizeError;
134+
return static_cast<uint16_t>(TxFrameError) |
135+
static_cast<uint8_t>(BufferSizeError);
134136
}
135137
_buffer[_index++] = data;
136138
if ((_index - _numCommandBytes) % 3 == 2) {
137139
if (_bufferSize <= _index) {
138-
return TxFrameError | BufferSizeError;
140+
return static_cast<uint16_t>(TxFrameError) |
141+
static_cast<uint8_t>(BufferSizeError);
139142
}
140143
uint8_t crc = generateCRC(&_buffer[_index - 2], 2, _polynomial_type);
141144
_buffer[_index++] = crc;

src/SensirionRxFrame.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ SensirionRxFrame::SensirionRxFrame(uint8_t buffer[], size_t bufferSize)
4242

4343
uint16_t SensirionRxFrame::getUInt32(uint32_t& data) {
4444
if (_numBytes < 4) {
45-
return RxFrameError | NoDataError;
45+
return static_cast<uint16_t>(RxFrameError) |
46+
static_cast<uint8_t>(NoDataError);
4647
}
4748
data = static_cast<uint32_t>(_buffer[_index++]) << 24;
4849
data |= static_cast<uint32_t>(_buffer[_index++]) << 16;
@@ -61,7 +62,8 @@ uint16_t SensirionRxFrame::getInt32(int32_t& data) {
6162

6263
uint16_t SensirionRxFrame::getUInt16(uint16_t& data) {
6364
if (_numBytes < 2) {
64-
return RxFrameError | NoDataError;
65+
return static_cast<uint16_t>(RxFrameError) |
66+
static_cast<uint8_t>(NoDataError);
6567
}
6668
data = static_cast<uint16_t>(_buffer[_index++]) << 8;
6769
data |= static_cast<uint16_t>(_buffer[_index++]);
@@ -78,7 +80,8 @@ uint16_t SensirionRxFrame::getInt16(int16_t& data) {
7880

7981
uint16_t SensirionRxFrame::getUInt8(uint8_t& data) {
8082
if (_numBytes < 1) {
81-
return RxFrameError | NoDataError;
83+
return static_cast<uint16_t>(RxFrameError) |
84+
static_cast<uint8_t>(NoDataError);
8285
}
8386
data = _buffer[_index++];
8487
_numBytes -= 1;
@@ -87,7 +90,8 @@ uint16_t SensirionRxFrame::getUInt8(uint8_t& data) {
8790

8891
uint16_t SensirionRxFrame::getInt8(int8_t& data) {
8992
if (_numBytes < 1) {
90-
return RxFrameError | NoDataError;
93+
return static_cast<uint16_t>(RxFrameError) |
94+
static_cast<uint8_t>(NoDataError);
9195
}
9296
data = static_cast<int8_t>(_buffer[_index++]);
9397
_numBytes -= 1;
@@ -96,7 +100,8 @@ uint16_t SensirionRxFrame::getInt8(int8_t& data) {
96100

97101
uint16_t SensirionRxFrame::getBool(bool& data) {
98102
if (_numBytes < 1) {
99-
return RxFrameError | NoDataError;
103+
return static_cast<uint16_t>(RxFrameError) |
104+
static_cast<uint8_t>(NoDataError);
100105
}
101106
data = static_cast<bool>(_buffer[_index++]);
102107
_numBytes -= 1;
@@ -115,7 +120,8 @@ uint16_t SensirionRxFrame::getFloat(float& data) {
115120

116121
uint16_t SensirionRxFrame::getBytes(uint8_t data[], size_t maxBytes) {
117122
if (_numBytes < 1) {
118-
return RxFrameError | NoDataError;
123+
return static_cast<uint16_t>(RxFrameError) |
124+
static_cast<uint8_t>(NoDataError);
119125
}
120126
size_t readAmount = maxBytes;
121127
if (_numBytes < maxBytes) {
@@ -132,7 +138,8 @@ uint16_t SensirionRxFrame::getInteger(uint8_t* destination, IntegerType type,
132138
uint8_t nrOfBytes) {
133139

134140
if (_numBytes < nrOfBytes) {
135-
return RxFrameError | NoDataError;
141+
return static_cast<uint16_t>(RxFrameError) |
142+
static_cast<uint8_t>(NoDataError);
136143
}
137144

138145
if (nrOfBytes > type) {

src/SensirionShdlcCommunication.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ static uint16_t readByte(uint8_t& data, Stream& serial, uint32_t startTime,
4242
uint32_t timeoutMicros) {
4343
do {
4444
if (micros() - startTime > timeoutMicros) {
45-
return ReadError | TimeoutError;
45+
return static_cast<uint16_t>(ReadError) |
46+
static_cast<uint8_t>(TimeoutError);
4647
}
4748
} while (!serial.available());
4849
data = serial.read();
@@ -69,7 +70,8 @@ uint16_t SensirionShdlcCommunication::sendFrame(SensirionShdlcTxFrame& frame,
6970
Stream& serial) {
7071
size_t writtenBytes = serial.write(&frame._buffer[0], frame._index);
7172
if (writtenBytes != frame._index) {
72-
return WriteError | SerialWriteError;
73+
return static_cast<uint16_t>(WriteError) |
74+
static_cast<uint8_t>(SerialWriteError);
7375
}
7476
return NoError;
7577
}
@@ -83,7 +85,8 @@ uint16_t SensirionShdlcCommunication::receiveFrame(SensirionShdlcRxFrame& frame,
8385
uint8_t current = 0;
8486

8587
if (frame._numBytes) {
86-
return ReadError | NonemptyFrameError;
88+
return static_cast<uint16_t>(ReadError) |
89+
static_cast<uint8_t>(NonemptyFrameError);
8790
}
8891

8992
// Wait for start byte and ignore all other bytes in case a partial frame
@@ -121,14 +124,16 @@ uint16_t SensirionShdlcCommunication::receiveFrame(SensirionShdlcRxFrame& frame,
121124
frame._address + frame._command + frame._state + dataLength;
122125

123126
if (dataLength > frame._bufferSize) {
124-
return RxFrameError | BufferSizeError;
127+
return static_cast<uint16_t>(RxFrameError) |
128+
static_cast<uint8_t>(BufferSizeError);
125129
}
126130

127-
// An error frame does not have any data eventhough the value of "dataLength" is not zero.
128-
// Therefore, only read data from the buffer if the frame state is 0, i.e, the frame is not of an error frame.
129-
130-
if(frame._state == 0 ){
131-
for(size_t i = 0; i < dataLength; i++){
131+
// An error frame does not have any data eventhough the value of
132+
// "dataLength" is not zero. Therefore, only read data from the buffer if
133+
// the frame state is 0, i.e, the frame is not of an error frame.
134+
135+
if (frame._state == 0) {
136+
for (size_t i = 0; i < dataLength; i++) {
132137
error = unstuffByte(current, serial, startTime, timeoutMicros);
133138
if (error) {
134139
return error;
@@ -145,7 +150,8 @@ uint16_t SensirionShdlcCommunication::receiveFrame(SensirionShdlcRxFrame& frame,
145150
return error;
146151
}
147152
if (expectedChecksum != actualChecksum) {
148-
return ReadError | ChecksumError;
153+
return static_cast<uint16_t>(ReadError) |
154+
static_cast<uint8_t>(ChecksumError);
149155
}
150156

151157
uint8_t stop;
@@ -154,7 +160,8 @@ uint16_t SensirionShdlcCommunication::receiveFrame(SensirionShdlcRxFrame& frame,
154160
return error;
155161
}
156162
if (stop != 0x7e) {
157-
return ReadError | StopByteError;
163+
return static_cast<uint16_t>(ReadError) |
164+
static_cast<uint8_t>(StopByteError);
158165
}
159166
if (frame._state & 0x7F) {
160167
return ExecutionError | frame._state;
@@ -178,10 +185,12 @@ uint16_t SensirionShdlcCommunication::sendAndReceiveFrame(
178185
return error;
179186
}
180187
if (rxFrame.getCommand() != txFrame.getCommand()) {
181-
return RxFrameError | RxCommandError;
188+
return static_cast<uint16_t>(RxFrameError) |
189+
static_cast<uint8_t>(RxCommandError);
182190
}
183191
if (rxFrame.getAddress() != txFrame.getAddress()) {
184-
return RxFrameError | RxAddressError;
192+
return static_cast<uint16_t>(RxFrameError) |
193+
static_cast<uint8_t>(RxAddressError);
185194
}
186195
return NoError;
187196
}

src/SensirionShdlcTxFrame.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
uint16_t SensirionShdlcTxFrame::begin(uint8_t command, uint8_t address,
4040
uint8_t dataLength) {
41-
_index=0; //sets the index to point towards the start of the buffer.
41+
_index = 0; // sets the index to point towards the start of the buffer.
4242
_buffer[_index] = 0x7e;
4343
uint16_t error = addUInt8(address);
4444
error |= addUInt8(command);
@@ -54,7 +54,8 @@ uint16_t SensirionShdlcTxFrame::finish(void) {
5454
return error;
5555
}
5656
if (_index + 1 > _bufferSize) {
57-
return TxFrameError | BufferSizeError;
57+
return static_cast<uint16_t>(TxFrameError) |
58+
static_cast<uint8_t>(BufferSizeError);
5859
}
5960
_buffer[_index++] = 0x7e;
6061
_isFinished = true;
@@ -85,7 +86,8 @@ uint16_t SensirionShdlcTxFrame::addInt16(int16_t data) {
8586

8687
uint16_t SensirionShdlcTxFrame::addUInt8(uint8_t data) {
8788
if (_index + 2 > _bufferSize) {
88-
return TxFrameError | BufferSizeError;
89+
return static_cast<uint16_t>(TxFrameError) |
90+
static_cast<uint8_t>(BufferSizeError);
8991
}
9092
switch (data) {
9193
case 0x11:

0 commit comments

Comments
 (0)