1
1
//
2
2
// FILE: SHT2x.cpp
3
3
// AUTHOR: Rob Tillaart, Viktor Balint, JensB
4
- // VERSION: 0.5.0
4
+ // VERSION: 0.5.1
5
5
// DATE: 2023-11-25
6
6
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
7
7
// URL: https://github.com/RobTillaart/SHT2x
@@ -164,19 +164,23 @@ bool SHT2x::readTemperature()
164
164
if (crc8 (buffer, 2 ) != buffer[2 ])
165
165
{
166
166
_error = SHT2x_ERR_CRC_TEMP;
167
- // return false; // do not fail yet
167
+ // Allow reading the value even if CRC fails, as _error is flagged.
168
+ // The user should call getError() to check the status.
169
+ // return false;
168
170
}
169
171
_rawTemperature = buffer[0 ] << 8 ;
170
172
_rawTemperature += buffer[1 ];
171
- _rawTemperature &= 0xFFFC ;
173
+ _rawTemperature &= 0xFFFC ; // Clear status bits (last two bits)
172
174
173
- // clear requestType
175
+ // clear requestType, marking this async operation as complete
174
176
_requestType = SHT2x_REQ_NONE;
175
177
176
- _status = buffer[1 ] & 0x03 ;
177
- if (_status == 0xFF ) // TODO != 0x01 (need HW to test)
178
+ _status = buffer[1 ] & 0x03 ; // Extract status bits
179
+ // After a temperature read, the status bits should indicate "temperature reading" (0x01).
180
+ // If not, it implies a read error or unexpected sensor state.
181
+ if (_status != SHT2x_STATUS_TEMPERATURE)
178
182
{
179
- _error = SHT2x_ERR_READBYTES;
183
+ _error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
180
184
return false ;
181
185
}
182
186
return true ;
@@ -194,24 +198,28 @@ bool SHT2x::readHumidity()
194
198
if (crc8 (buffer, 2 ) != buffer[2 ])
195
199
{
196
200
_error = SHT2x_ERR_CRC_HUM;
197
- // return false; // do not fail yet
201
+ // Allow reading the value even if CRC fails, as _error is flagged.
202
+ // The user should call getError() to check the status.
203
+ // return false;
198
204
}
199
205
_rawHumidity = buffer[0 ] << 8 ;
200
206
_rawHumidity += buffer[1 ];
201
- _rawHumidity &= 0xFFFC ;
207
+ _rawHumidity &= 0xFFFC ; // Clear status bits (last two bits)
202
208
203
- // clear requestType
209
+ // clear requestType, marking this async operation as complete
204
210
_requestType = SHT2x_REQ_NONE;
205
211
206
- _status = buffer[1 ] & 0x03 ;
207
- if (_status == 0xFF ) // TODO != 0x02 (need HW to test)
212
+ _status = buffer[1 ] & 0x03 ; // Extract status bits
213
+ // After a humidity read, the status bits should indicate "humidity reading" (0x02).
214
+ // If not, it implies a read error or unexpected sensor state.
215
+ if (_status != SHT2x_STATUS_HUMIDITY)
208
216
{
209
- _error = SHT2x_ERR_READBYTES;
217
+ _error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
210
218
return false ;
211
219
}
212
220
213
- _error = SHT2x_OK;
214
- _lastRead = millis ();
221
+ _error = SHT2x_OK; // Mark as OK if all checks passed for this specific read
222
+ _lastRead = millis (); // Record time of successful synchronous style read completion
215
223
return true ;
216
224
}
217
225
@@ -412,7 +420,7 @@ bool SHT2x::setHeaterLevel(uint8_t level)
412
420
heaterReg |= level;
413
421
if (writeCmd (0x51 , heaterReg) == false )
414
422
{
415
- _error = - 1 ;
423
+ _error = SHT2x_ERR_WRITECMD; // Use defined error code for write command failure
416
424
return false ;
417
425
}
418
426
return true ;
@@ -563,18 +571,25 @@ bool SHT2x::batteryOK()
563
571
//
564
572
uint8_t SHT2x::crc8 (const uint8_t *data, uint8_t len)
565
573
{
566
- // CRC-8 formula from page 14 of SHT spec pdf
567
- // Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
568
- const uint8_t POLY = 0x31 ;
574
+ // CRC-8 formula from page 14 of SHT2x datasheet.
575
+ // Document: "Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf"
576
+ // Polynomial: x^8 + x^5 + x^4 + 1 (0x131 -> 0x31, MSB is implicit)
577
+ const uint8_t CRC_POLYNOMIAL = 0x31 ;
569
578
uint8_t crc = 0x00 ;
570
579
571
- for (uint8_t j = len; j; --j )
580
+ for (uint8_t byteIndex = 0 ; byteIndex < len; ++byteIndex )
572
581
{
573
- crc ^= *data++;
574
-
575
- for (uint8_t i = 8 ; i; --i)
582
+ crc ^= data[byteIndex]; // XOR byte into CRC
583
+ for (uint8_t bit = 8 ; bit > 0 ; --bit)
576
584
{
577
- crc = (crc & 0x80 ) ? (crc << 1 ) ^ POLY : (crc << 1 );
585
+ if (crc & 0x80 ) // If MSB is set
586
+ {
587
+ crc = (crc << 1 ) ^ CRC_POLYNOMIAL;
588
+ }
589
+ else
590
+ {
591
+ crc = (crc << 1 );
592
+ }
578
593
}
579
594
}
580
595
return crc;
0 commit comments