Skip to content

Commit bbe3e43

Browse files
committed
Fix for part of issue 27 - getTemperatureOffset now correctly returns negative numbers
1 parent 7fd4d73 commit bbe3e43

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/SparkFun_SCD30_Arduino_Library.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ bool SCD30::setForcedRecalibrationFactor(uint16_t concentration)
151151
float SCD30::getTemperatureOffset(void)
152152
{
153153
uint16_t response = readRegister(COMMAND_SET_TEMPERATURE_OFFSET);
154-
return (((float)response) / 100.0);
154+
155+
union
156+
{
157+
int16_t signed16;
158+
uint16_t unsigned16;
159+
} signedUnsigned; // Avoid any ambiguity casting int16_t to uint16_t
160+
signedUnsigned.signed16 = response;
161+
162+
return (((float)signedUnsigned.signed16) / 100.0);
155163
}
156164

157165
//Set the temperature offset. See 1.3.8.
@@ -163,6 +171,7 @@ bool SCD30::setTemperatureOffset(float tempOffset)
163171
uint16_t unsigned16;
164172
} signedUnsigned; // Avoid any ambiguity casting int16_t to uint16_t
165173
signedUnsigned.signed16 = tempOffset * 100;
174+
166175
return sendCommand(COMMAND_SET_TEMPERATURE_OFFSET, signedUnsigned.unsigned16);
167176
}
168177

0 commit comments

Comments
 (0)