Skip to content

Commit

Permalink
Merge pull request #22 from pilotak/master
Browse files Browse the repository at this point in the history
return NAN if device unavailable
  • Loading branch information
hoffmannjan committed May 15, 2019
2 parents 5c2efb5 + 9c17b29 commit f962296
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Adafruit_MCP9808.cpp
Expand Up @@ -112,12 +112,15 @@ bool Adafruit_MCP9808::init() {
* @return Temperature in Centigrade.
*/
float Adafruit_MCP9808::readTempC() {
float temp = NAN;
uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);

float temp = t & 0x0FFF;
temp /= 16.0;
if (t & 0x1000)
temp -= 256;
if (t != 0xFFFF) {
temp = t & 0x0FFF;
temp /= 16.0;
if (t & 0x1000)
temp -= 256;
}

return temp;
}
Expand All @@ -128,14 +131,17 @@ float Adafruit_MCP9808::readTempC() {
* @return Temperature in Fahrenheit.
*/
float Adafruit_MCP9808::readTempF() {
float temp = NAN;
uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);

float temp = t & 0x0FFF;
temp /= 16.0;
if (t & 0x1000)
temp -= 256;
if (t != 0xFFFF) {
temp = t & 0x0FFF;
temp /= 16.0;
if (t & 0x1000)
temp -= 256;

temp = temp * 9.0 / 5.0 + 32;
temp = temp * 9.0 / 5.0 + 32;
}

return temp;
}
Expand Down

0 comments on commit f962296

Please sign in to comment.