Skip to content

Commit

Permalink
Fixed rounding errors going to/from ºC to RV-C temps
Browse files Browse the repository at this point in the history
  • Loading branch information
rubillos committed Mar 9, 2023
1 parent b259327 commit 395dc6e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,19 @@ uint32_t makeMsg(uint32_t dgn, uint8_t sourceID=0, uint8_t priority=6) {
return (priority<<26) | (dgn << 8) | sourceID;
}

//////////////////////////////////////////////

constexpr double tempCOffset = -273.0;
constexpr double tempCScale = 0.03125;
constexpr double tempCScaleInv = 1.0 / tempCScale;
constexpr double tempCRoundingOffset = 0.25;

double convToTempC(uint16_t value) {
return tempCOffset + value * tempCScale;
return tempCOffset + value * tempCScale + tempCRoundingOffset;
}

uint16_t convFromTempC(double tempC) {
return (tempC - tempCOffset) * tempCScaleInv;
return (tempC - tempCOffset) * tempCScaleInv + 0.5;
}

//////////////////////////////////////////////
Expand Down

0 comments on commit 395dc6e

Please sign in to comment.