Adafruit TSL2591 sensor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
12104404
Date:
Mon Apr 04 08:49:41 2016 +0000
Parent:
2:dd10c541a3dc
Child:
4:66ce66d4c07c
Commit message:
BME280, TSL2591;

Changed in this revision

BME280.cpp Show annotated file Show diff for this revision Revisions of this file
BME280.h Show annotated file Show diff for this revision Revisions of this file
TSL2591.cpp Show annotated file Show diff for this revision Revisions of this file
TSL2591.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BME280.cpp	Mon Apr 04 08:49:41 2016 +0000
@@ -0,0 +1,153 @@
+#include "BME280.h"
+
+BME280::BME280(I2C& bme280_i2c, uint8_t bme280_addr):
+    _i2c(bme280_i2c), _addr(bme280_addr<<1)
+{
+    _init=false;
+}
+
+bool BME280::init(void)
+{
+    char cmd[18];
+ 
+    cmd[0] = BME280_CTRL_HUM;
+    cmd[1] = BME280_SAMPLING_X1;
+    if(_i2c.write(_addr, cmd, 2)!=0)
+        return false;
+    
+    cmd[0] = BME280_CTRL_MEAS; // ctrl_meas
+    cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
+    _i2c.write(_addr, cmd, 2);
+ 
+    cmd[0] = 0xf5; // config
+    cmd[1] = 0xa0; // Standby 1000ms, Filter off
+    _i2c.write(_addr, cmd, 2);
+ 
+    cmd[0] = 0x88; // read dig_T regs
+    _i2c.write(_addr, cmd, 1);
+    _i2c.read(_addr, cmd, 6);
+ 
+    dig_T1 = (cmd[1] << 8) | cmd[0];
+    dig_T2 = (cmd[3] << 8) | cmd[2];
+    dig_T3 = (cmd[5] << 8) | cmd[4];
+ 
+    cmd[0] = 0x8E; // read dig_P regs
+    _i2c.write(_addr, cmd, 1);
+    _i2c.read(_addr, cmd, 18);
+ 
+    dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
+    dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
+    dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
+    dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
+    dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
+    dig_P6 = (cmd[11] << 8) | cmd[10];
+    dig_P7 = (cmd[13] << 8) | cmd[12];
+    dig_P8 = (cmd[15] << 8) | cmd[14];
+    dig_P9 = (cmd[17] << 8) | cmd[16];
+    
+    cmd[0] = 0xA1; // read dig_H regs
+    _i2c.write(_addr, cmd, 1);
+    _i2c.read(_addr, cmd, 1);
+     cmd[1] = 0xE1; // read dig_H regs
+    _i2c.write(_addr, &cmd[1], 1);
+    _i2c.read(_addr, &cmd[1], 7);
+
+    dig_H1 = cmd[0];
+    dig_H2 = (cmd[2] << 8) | cmd[1];
+    dig_H3 = cmd[3];
+    dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
+    dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
+    dig_H6 = cmd[7];
+    return true;
+}
+
+float BME280::getTemperature(void)
+{
+    uint32_t temp_raw;
+    float tempf;
+    char cmd[4];
+ 
+    cmd[0] = 0xfa; // temp_msb
+    _i2c.write(_addr, cmd, 1);
+    _i2c.read(_addr, &cmd[1], 3);
+ 
+    temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
+ 
+    int32_t temp;
+ 
+    temp =
+        (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
+        ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
+ 
+    t_fine = temp;
+    temp = (temp * 5 + 128) >> 8;
+    tempf = (float)temp;
+ 
+    return (tempf/100.0f);
+}
+ 
+float BME280::getPressure(void)
+{
+    uint32_t press_raw;
+    float pressf;
+    char cmd[4];
+ 
+    cmd[0] = 0xf7; // press_msb
+    _i2c.write(_addr, cmd, 1);
+    _i2c.read(_addr, &cmd[1], 3);
+ 
+    press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
+ 
+    int32_t var1, var2;
+    uint32_t press;
+ 
+    var1 = (t_fine >> 1) - 64000;
+    var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
+    var2 = var2 + ((var1 * dig_P5) << 1);
+    var2 = (var2 >> 2) + (dig_P4 << 16);
+    var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
+    var1 = ((32768 + var1) * dig_P1) >> 15;
+    if (var1 == 0) {
+        return 0;
+    }
+    press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
+    if(press < 0x80000000) {
+        press = (press << 1) / var1;
+    } else {
+        press = (press / var1) * 2;
+    }
+    var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
+    var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
+    press = (press + ((var1 + var2 + dig_P7) >> 4));
+ 
+    pressf = (float)press;
+    return (pressf/100.0f);
+}
+ 
+float BME280::getHumidity(void)
+{
+    uint32_t hum_raw;
+    float humf;
+    char cmd[4];
+ 
+    cmd[0] = 0xfd; // hum_msb
+    _i2c.write(_addr, cmd, 1);
+    _i2c.read(_addr, &cmd[1], 2);
+ 
+    hum_raw = (cmd[1] << 8) | cmd[2];
+ 
+    int32_t v_x1;
+ 
+    v_x1 = t_fine - 76800;
+    v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
+               ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
+                                            (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
+                                            (int32_t)dig_H2 + 8192) >> 14));
+    v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
+    v_x1 = (v_x1 < 0 ? 0 : v_x1);
+    v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
+ 
+    humf = (float)(v_x1 >> 12);
+ 
+    return (humf/1024.0f);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BME280.h	Mon Apr 04 08:49:41 2016 +0000
@@ -0,0 +1,50 @@
+#ifndef BME280_H
+#define BME280_H
+
+#include "mbed.h"
+
+#define BME280_ADDR_H       (0x77)
+#define BME280_ADDR_L       (0x76)
+
+#define BME280_CTRL_HUM     (0xF2)
+#define BME280_CTRL_MEAS    (0xF4)
+
+typedef enum {
+    BME280_SAMPLING_X1    = 0x01,
+    BME280_SAMPLING_X2    = 0x02,
+    BME280_SAMPLING_X4    = 0x03,
+    BME280_SAMPLING_X8    = 0x04,
+    BME280_SAMPLING_X16   = 0x05,
+} bme280_sampling_t;
+
+typedef enum{
+    BME280_MODE_SLEEP   = 0x00,
+    BME280_MODE_FORCED1 = 0x01,
+    BME280_MODE_FORCED2 = 0x02,
+    BME280_MODE_NORMAL  = 0x03,
+} bme280_mode_t;
+
+class BME280
+{
+public:
+    BME280(I2C& bme280_i2c, uint8_t bme280_addr=BME280_ADDR_H);
+    //virtual ~BME280();
+    bool init(void);
+    float getTemperature(void);
+    float getPressure(void);
+    float getHumidity(void);
+
+protected:
+    I2C         _i2c;
+    uint8_t     _addr;
+    bool        _init;
+    uint16_t    dig_T1;
+    int16_t     dig_T2, dig_T3;
+    uint16_t    dig_P1;
+    int16_t     dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
+    uint16_t    dig_H1, dig_H3;
+    int16_t     dig_H2, dig_H4, dig_H5, dig_H6;
+    int32_t     t_fine;
+};
+
+#endif
\ No newline at end of file
--- a/TSL2591.cpp	Mon Mar 14 03:11:03 2016 +0000
+++ b/TSL2591.cpp	Mon Apr 04 08:49:41 2016 +0000
@@ -8,8 +8,8 @@
     _gain = TSL2591_GAIN_LOW;
 }
 /*
- * Initialize TSL2591
- * Checks ID and sets gain and integration time
+ *  Initialize TSL2591
+ *  Checks ID and sets gain and integration time
  */
 bool TSL2591::init(void)
 {
@@ -27,19 +27,26 @@
     }
     return false;
 }
-
+/*
+ *  Power On TSL2591
+ */
 void TSL2591::enable(void)
 {
     char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_PON|TSL2591_EN_AEN|TSL2591_EN_AIEN|TSL2591_EN_NPIEN)};
     _i2c.write(_addr<<1, write, 2, 0);
 }
-
+/*
+ *  Power Off TSL2591
+ */
 void TSL2591::disable(void)
 {
     char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_POFF)};
     _i2c.write(_addr<<1, write, 2, 0);
 }
-
+/*
+ *  Set Gain and Write
+ *  Set gain and write time and gain
+ */
 void TSL2591::setGain(tsl2591Gain_t gain)
 {
     enable();
@@ -48,7 +55,10 @@
     _i2c.write(_addr<<1, write, 2, 0);
     disable();
 }
-
+/*
+ *  Set Integration Time and Write
+ *  Set gain and write time and gain
+ */
 void TSL2591::setTime(tsl2591IntegrationTime_t integ)
 {
     enable();
@@ -57,7 +67,10 @@
     _i2c.write(_addr<<1, write, 2, 0);
     disable();
 }
-
+/*
+ *  Read ALS
+ *  Read full spectrum, infrared, and visible
+ */
 void TSL2591::getALS(void)
 {
     enable();
@@ -72,13 +85,15 @@
     _i2c.write(_addr<<1, write2, 1, 0);
     char read2[2];
     _i2c.read(_addr<<1, read2, 2, 0);
-    _rawALS = (((read1[1]<<8)|read1[0])<<16)|((read2[1]<<8)|read2[0]);
+    rawALS = (((read1[1]<<8)|read1[0])<<16)|((read2[1]<<8)|read2[0]);
     disable();
-    full = _rawALS & 0xFFFF;
-    ir = _rawALS >> 16;
+    full = rawALS & 0xFFFF;
+    ir = rawALS >> 16;
     visible = full - ir;
 }
-
+/*
+ *  Calculate Lux
+ */
 void TSL2591::calcLux(void)
 {
     float atime, again, cpl, lux1, lux2, lux3;
@@ -131,12 +146,4 @@
     lux2 = (( TSL2591_LUX_COEFC * (float)full ) - ( TSL2591_LUX_COEFD * (float)ir)) / cpl;
     lux3 = lux1 > lux2 ? lux1 : lux2;
     lux = (uint32_t)lux3;
-}
-
-/*
-uint16_t TSL2591::getLumin(uint8_t channel)
-{
-    uint32_t x =
-}
-*/
-//uint32_t TSL2591::calcLux()
\ No newline at end of file
+}
\ No newline at end of file
--- a/TSL2591.h	Mon Mar 14 03:11:03 2016 +0000
+++ b/TSL2591.h	Mon Apr 04 08:49:41 2016 +0000
@@ -79,7 +79,7 @@
 class TSL2591
 {
     public:
-    TSL2591(I2C& tsl2591_i2c, uint8_t tsl2591_addr);
+    TSL2591(I2C& tsl2591_i2c, uint8_t tsl2591_addr=TSL2591_ADDR);
     bool init(void);
     void enable(void);
     void disable(void);
@@ -87,9 +87,10 @@
     void setTime(tsl2591IntegrationTime_t integ);
     void getALS(void);
     void calcLux(void);
-    volatile uint16_t           visible;
+    volatile uint32_t           rawALS;
     volatile uint16_t           ir;
     volatile uint16_t           full;
+    volatile uint16_t           visible;
     volatile uint32_t           lux;
     
     protected:
@@ -98,7 +99,6 @@
     bool                        _init;
     tsl2591Gain_t               _gain;
     tsl2591IntegrationTime_t    _integ;
-    uint32_t                    _rawALS;
 };
 
 #endif
\ No newline at end of file
--- a/main.cpp	Mon Mar 14 03:11:03 2016 +0000
+++ b/main.cpp	Mon Apr 04 08:49:41 2016 +0000
@@ -1,18 +1,19 @@
 #include "mbed.h"
 #include "TSL2591.h"
+#include "BME280.h"
 
-I2C i2c1(PB_9, PB_8);
-TSL2591 sensor(i2c1, TSL2591_ADDR);
-
-DigitalOut myled(LED1);
+I2C i2c1(I2C_SDA, I2C_SCL);
+BME280 sensor(i2c1, BME280_ADDR_L);
+TSL2591 sensor1(i2c1, TSL2591_ADDR);
 
 int main()
 {
     sensor.init();
     while(1) {
-        sensor.getALS();
-        sensor.calcLux();
-        printf("%d \t %d \t %d \t %d\n",sensor.full,sensor.ir,sensor.visible,sensor.lux);
-        wait(0.5);
+        sensor1.getALS();
+        sensor1.calcLux();
+        printf("%d \t %d \t %d \t %d\n",sensor1.full,sensor1.ir,(int16_t)sensor1.visible,sensor1.lux);
+        printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", sensor.getTemperature(), sensor.getPressure(), sensor.getHumidity());
+        wait(1);
     }
 }
\ No newline at end of file