Temp rounding

This commit is contained in:
Clément Grennerat 2025-07-15 01:42:39 +02:00
parent d6f911bf4f
commit 7c3849ee35

6
main.c
View File

@ -84,12 +84,14 @@ int fetchClimate(int8_t* humidity, int8_t* temperature) {
*humidity = (int8_t) (hum / 10); *humidity = (int8_t) (hum / 10);
uint16_t temp = (((uint16_t) (dhtBytes[2])) << 8) | ((uint16_t) dhtBytes[3]); uint16_t temp = (((uint16_t) (dhtBytes[2])) << 8) | ((uint16_t) dhtBytes[3]);
int8_t tempRound = (temp % 10) >= 5 ? 1 : 0;
if (temp & 0x8000) { // If high bit is 1, temperature is negative. if (temp & 0x8000) { // If high bit is 1, temperature is negative.
*temperature = - (int8_t) ((temp & 0x7FFF) / 10); *temperature = - (int8_t) ((temp & 0x7FFF) / 10) - tempRound;
}else { }else {
*temperature = (int8_t) (temp / 10); *temperature = (int8_t) (temp / 10) + tempRound;
} }
return 0; return 0;
} }