[riot-notifications] [RIOT-OS/RIOT] drivers/opt3001: Initial support for the Opt3001 sensor (#12260)

benpicco notifications at github.com
Tue Sep 24 00:22:25 CEST 2019


benpicco commented on this pull request.



> @@ -165,7 +151,7 @@ int opt3001_read(const opt3001_t *dev, uint16_t *crf, uint16_t *rawl);
  * @param[in]  rawl         raw lux value
  * @param[out] convl        converted lux value

What is the unit of the converted lux value?

>  {
   uint16_t mantissa;
   uint8_t exponent;
 
   exponent = OPT3001_REGS_REG_EXPONENT(rawl);
   mantissa = OPT3001_REGS_REG_MANTISSA(rawl);
 
-  *convl = 0.01 * pow(2, exponent) * mantissa;
+  *convl = (1 << exponent) * mantissa;

If you want to return mLux, you have to multiply by 10 now.

> @@ -182,13 +184,6 @@ int opt3001_read_lux(const opt3001_t *dev, int16_t *convl)
 
     opt3001_convert(rawl, &convlux);
     *convl = (int16_t)(convlux*100);

This is not needed anymore.

> @@ -31,13 +31,9 @@
 
     res->val[1] = 0;
 
- #if OPT3001_USE_RAW_VALUES
-     res->unit = UNIT_NONE;
-     res->scale = 0;
- #else
-     res->unit = UNIT_LUX;
-     res->scale = -2;
- #endif
+    res->unit = UNIT_LUX;
+    res->scale = -2;

Is this still right?

> +    if (i2c_read_regs(DEV_I2C, DEV_ADDR, OPT3001_REGS_CONFIG, &reg, 2, 0) < 0) {
+        i2c_release(DEV_I2C);
+        LOG_ERROR("opt3001_init: Error reading BUS!\n");
+        return -OPT3001_ERROR_BUS;
+    }
+
+    *crf = htons(reg) & OPT3001_REGS_CONFIG_CRF;
+
+    if (!(*crf)) {
+        i2c_release(DEV_I2C);
+        LOG_DEBUG("opt3001_read: conversion in progress!\n");
+        return -OPT3001_ERROR;
+    }
+
+    /* wait for the conversion to finish */
+    if (OPT3001_CONVERSION_TIME) {

Why wait now?
We already know here that no conversion is in progress anymore.

> +    uint16_t reg;
+
+    i2c_acquire(DEV_I2C);
+
+    if (i2c_read_regs(DEV_I2C, DEV_ADDR, OPT3001_REGS_CONFIG, &reg, 2, 0) < 0) {
+        i2c_release(DEV_I2C);
+        LOG_ERROR("opt3001_init: Error reading BUS!\n");
+        return -OPT3001_ERROR_BUS;
+    }
+
+    *crf = htons(reg) & OPT3001_REGS_CONFIG_CRF;
+
+    if (!(*crf)) {
+        i2c_release(DEV_I2C);
+        LOG_DEBUG("opt3001_read: conversion in progress!\n");
+        return -OPT3001_ERROR;

Instead of returning error here, why not repeat reading `OPT3001_REGS_CONFIG` in a loop until `OPT3001_REGS_CONFIG_CRF` is set?
You can still call `xtimer_usleep()` in the loop, but only sleep 1ms at a time until you have reached `OPT3001_CONVERSION_TIME`.

Another option instead of polling would be to use the interrupt pin to get an interrupt when conversion is done.

> +
+ #define OPT3001_CONFIG_M_SHUTDOWN     (0x0) /**< Shutdown mode */
+ #define OPT3001_CONFIG_M_SINGLE       (0x1) /**< Single-shot mode */
+ #define OPT3001_CONFIG_M_CONTINUOUS   (0x2) /**< Continuous mode (also 0x3) */
+ #define OPT3001_CONFIG_MOC_SHIFT      (9U)  /**< Mode of conversion shift */
+ #define OPT3001_CONFIG_MOC_MASK       (0x0600) /**< Mode of conversion mask */
+ #define OPT3001_REGS_CONFIG_MOC(x)    (((uint16_t)(((uint16_t)(x)) \
+                                       << OPT3001_CONFIG_MOC_SHIFT)) \
+                                       & OPT3001_CONFIG_MOC_MASK) /**< Mode of conversion field */
+
+ #define OPT3001_REGS_CONFIG_OVF (1 << 8)  /**< Overflow flag field */
+ #define OPT3001_REGS_CONFIG_CRF (1 << 7)  /**< Conversion ready field */
+ #define OPT3001_REGS_CONFIG_FH	 (1 << 6)  /**< Flag high field */
+ #define OPT3001_REGS_CONFIG_FL	 (1 << 5)  /**< Flag low field */
+ #define OPT3001_REGS_CONFIG_L	 (1 << 4)  /**< Latch field */
+ #define OPT3001_REGS_CONFIG_POL (1 << 3)  /**< Polarity field */

Indentation

> + #define OPT3001_REGS_CONFIG_CT_MASK   (0x0800) /**< Conversion time mask */
+ #define OPT3001_REGS_CONFIG_CT(x)     (((uint16_t)(((uint16_t)(x)) \
+                                       << OPT3001_REGS_CONFIG_CT_SHIFT)) \
+                                       & OPT3001_REGS_CONFIG_CT_MASK) /**< Conversion time field */
+
+ #define OPT3001_CONFIG_M_SHUTDOWN     (0x0) /**< Shutdown mode */
+ #define OPT3001_CONFIG_M_SINGLE       (0x1) /**< Single-shot mode */
+ #define OPT3001_CONFIG_M_CONTINUOUS   (0x2) /**< Continuous mode (also 0x3) */
+ #define OPT3001_CONFIG_MOC_SHIFT      (9U)  /**< Mode of conversion shift */
+ #define OPT3001_CONFIG_MOC_MASK       (0x0600) /**< Mode of conversion mask */
+ #define OPT3001_REGS_CONFIG_MOC(x)    (((uint16_t)(((uint16_t)(x)) \
+                                       << OPT3001_CONFIG_MOC_SHIFT)) \
+                                       & OPT3001_CONFIG_MOC_MASK) /**< Mode of conversion field */
+
+ #define OPT3001_REGS_CONFIG_OVF (1 << 8)  /**< Overflow flag field */
+ #define OPT3001_REGS_CONFIG_CRF (1 << 7)  /**< Conversion ready field */

Indentation

> +    if (i2c_read_regs(DEV_I2C, DEV_ADDR, OPT3001_REGS_RESULT, &reg, 2, 0) < 0) {
+        i2c_release(DEV_I2C);
+        LOG_ERROR("opt3001_init: Error reading BUS!\n");
+        return -OPT3001_ERROR_BUS;
+    }
+
+    *rawl = htons(reg);
+
+    i2c_release(DEV_I2C);
+    return OPT3001_OK;
+}
+
+void opt3001_convert(int16_t rawl, uint16_t *convl)
+{
+  uint16_t mantissa;
+  uint8_t exponent;

Indentation should always be 4 spaces.

-- 
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/12260#pullrequestreview-292073964
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190923/9457dcfd/attachment.htm>


More information about the notifications mailing list