[riot-notifications] [RIOT-OS/RIOT] drivers/lpsxxx: refactor lps331ap and add support for lps25hb + lps22hb (#10695)

Kaspar Schleiser notifications at github.com
Tue Feb 19 15:17:48 CET 2019


kaspar030 commented on this pull request.



> +
+    if (i2c_read_reg(DEV_I2C, DEV_ADDR, LPSXXX_REG_TEMP_OUT_H, &tmp, 0) < 0) {
+        i2c_release(DEV_I2C);
+        DEBUG("[lpsxxx] read_temp: cannot read TEMP_OUT_H register\n");
+        return -LPSXXX_ERR_I2C;
+    }
+    i2c_release(DEV_I2C);
+    val |= ((uint16_t)tmp << 8);
+
+    DEBUG("[lpsxxx] read_temp: raw data %08" PRIx32 "\n", (uint32_t)val);
+
+    /* compute actual temperature value in °C */
+    res += ((float)val) / TEMP_DIVIDER;
+
+    /* return temperature in c°C */
+    *temp = (int16_t)(res * 100);

I think you can get rid of the float conversion & division:
```
(TEMP_BASE + (val / TEMP_DIVIDER)) * 100 == (TEMP_BASE * 100) + (val * 100 / TEMP_DIVIDER)
```

So this should be equivalent:

```
#define TEMP_BASE=4250U;
#define TEMP_DIVIDER=480U;

uint32_t res = TEMP_BASE;
uint32_t val = 0;
// ...
res += (val * 100) / TEMP_DIVIDER;
*temp = (int16_t)res;
```

(maybe use signed int).

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/10695#pullrequestreview-205234463
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190219/d45353ec/attachment.html>


More information about the notifications mailing list