TSL2561

Dependents:   Hexi_TSL2561 HexiHeart_Main

Fork of TSL2561 by Kenji Arai

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Sat Mar 07 23:42:22 2015 +0000
Parent:
0:eec7bcd27c52
Child:
2:17591031447b
Commit message:
Changed lux calculation way

Changed in this revision

TSL2561.cpp Show annotated file Show diff for this revision Revisions of this file
TSL2561.h Show annotated file Show diff for this revision Revisions of this file
--- a/TSL2561.cpp	Sun Feb 22 01:19:56 2015 +0000
+++ b/TSL2561.cpp	Sat Mar 07 23:42:22 2015 +0000
@@ -7,7 +7,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: Feburary  21st, 2015
- *      Revised: Feburary  22nd, 2015
+ *      Revised: March      8th, 2015
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -64,9 +64,17 @@
     _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
     _i2c.read(TSL2561_addr, (char *)dt, 2, false);
     ch1 = dt[1] << 8 | dt[0];
-    lux0 = ch0;
-    lux1 = ch1;
+    if (ch0 == 0xFFFF) {
+        return 2500.0;
+    }
+    lux0 = (double)ch0;
+    lux1 = (double)ch1;
     ratio = lux1 / lux0;
+    read_timing_reg();
+    lux0 *= (402.0/integ_time);
+    lux1 *= (402.0/integ_time);
+    lux0 /= gain;
+    lux1 /= gain;
     if (ratio <= 0.5) {
         dlux = 0.03040 * lux0 - 0.06200 * lux0 * pow(ratio,1.4);
     } else if (ratio <= 0.61) {
@@ -86,7 +94,49 @@
 {
     _i2c.frequency(100000);
     power_up();
-    read_ID();
+    set_timing_reg(TIMING_DEFAULT);
+}
+
+/////////////// Timing Register ///////////////////////////
+uint8_t TSL2561::set_timing_reg(uint8_t parameter)
+{
+    dt[0] = CMD_SINGLE + TSL2561_TIMING;
+    dt[1] = parameter;
+    _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);
+    dt[0] = CMD_SINGLE + TSL2561_TIMING;
+    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
+    _i2c.read(TSL2561_addr, (char *)dt, 1, false);
+    return dt[0];
+}
+
+uint8_t TSL2561::read_timing_reg(void)
+{
+    uint8_t i;
+
+    dt[0] = CMD_SINGLE + TSL2561_TIMING;
+    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
+    _i2c.read(TSL2561_addr, (char *)dt, 1, false);
+    if (dt[0] & TIMING_GAIN_16){
+        gain = 16;
+    } else {
+        gain = 1;
+    }
+    i = dt[0] & 0x3;
+    switch (i) {
+        case 0:
+            integ_time = 13.7;
+            break;
+        case 1:
+            integ_time = 101.0;
+            break;
+        case 2:
+            integ_time = 402.0;
+            break;
+        default:
+            integ_time = 0;
+            break;
+    }
+    return dt[0];
 }
 
 /////////////// ID ////////////////////////////////////////
--- a/TSL2561.h	Sun Feb 22 01:19:56 2015 +0000
+++ b/TSL2561.h	Sat Mar 07 23:42:22 2015 +0000
@@ -7,7 +7,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: Feburary  21st, 2015
- *      Revised: Feburary  21st, 2015
+ *      Revised: March      8th, 2015
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -48,6 +48,15 @@
 #define TSL2561_DATA1LOW            0x0E
 #define TSL2561_DATA1HIGH           0x0F
 
+////////////// TIMING PARAMETER ///////////////////////////
+#define TIMING_GAIN_1               (0UL << 4)
+#define TIMING_GAIN_16              (1UL << 4)
+#define TIMING_TIME_13R7            (0x0)
+#define TIMING_TIME_101             (0x1)
+#define TIMING_TIME_402             (0x2)
+#define TIMING_TIME_MANU            (0x3)
+#define TIMING_DEFAULT              (TIMING_GAIN_1 + TIMING_TIME_402)
+
 ////////////// ID /////////////////////////////////////////
 #define I_AM_TSL2561                0x50
 #define REG_NO_MASK                 0x0F
@@ -69,7 +78,7 @@
  *  TSL2561      lum(dp5,dp27);    // TSL2561 SDA, SCL
  * // If you connected I2C line not only this device but also other devices,
  * //     you need to declare following method.
- *  I2C          i2c(dp5,dp27);     // SDA, SCL
+ *  I2C          i2c(dp5,dp27);    // SDA, SCL
  *  TSL2561      lum(i2c);         // TSL2561 SDA, SCL (Data available every 400mSec)
  *
  * int main() {;
@@ -103,11 +112,17 @@
       */
     float lux(void);
 
-    /** Set config register
-      * @param config parameter
-      * @return config read data
+    /** Set timing register
+      * @param timing parameter
+      * @return timing read data
       */
-    uint16_t set_config(uint16_t cfg);
+    uint8_t set_timing_reg(uint8_t parameter);
+
+    /** Read timing register
+      * @param timing parameter
+      * @return timing read data
+      */
+    uint8_t read_timing_reg(void);
 
     /** Set I2C clock frequency
       * @param freq.
@@ -142,9 +157,11 @@
 private:
     uint8_t  TSL2561_addr;
     uint8_t  dt[4];
-    uint16_t ch0;
-    uint16_t ch1;
-    uint8_t id_number;
+    uint32_t ch0;
+    uint32_t ch1;
+    int8_t   gain;
+    uint8_t  id_number;
+    double   integ_time;
 };
 
 #endif      // TSL2561_H