Przeglądaj źródła

Proper handling of negative LM75 values

Victor 3 lat temu
rodzic
commit
8b3134e7ea
1 zmienionych plików z 6 dodań i 2 usunięć
  1. 6 2
      sensors/LM75.c

+ 6 - 2
sensors/LM75.c

@@ -54,8 +54,12 @@ UnitempStatus unitemp_LM75_update(void* s) {
 
     uint8_t buff[2];
     if(!readRegArray(i2c_sensor, LM75_REG_TEMP, 2, buff)) return UT_TIMEOUT;
-    uint16_t raw =
-        ((((uint16_t)buff[0] << 8) | buff[1]) >> 7) * ((buff[1] & 0b10000000) ? -1.0f : 1.0f);
+    int16_t raw = ((((uint16_t)buff[0] << 8) | buff[1]) >> 7);
+
+    if(FURI_BIT(raw, 8)) {
+        FURI_BIT_CLEAR(raw, 8);
+        raw = (int8_t)raw;
+    }
     sensor->temp = (float)raw / 2.0f;
 
     FURI_LOG_D(APP_NAME, "Sensor %s updated %f", sensor->name, (double)raw / (double)2.0);