Sfoglia il codice sorgente

update the read method of lux value

Jack Shao 11 anni fa
parent
commit
2972633217

+ 102 - 90
Digital_Light_TSL2561.cpp

@@ -28,113 +28,125 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+
 #include <Digital_Light_TSL2561.h>
 #include <Arduino.h>
 #include <Wire.h>
 int TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
 {
+
     int value;
-    Wire.beginTransmission(deviceAddress);
-    Wire.write(address);                // register to read
-    Wire.endTransmission();
-    Wire.requestFrom(deviceAddress, 1); // read a byte
-    while(!Wire.available());
-    value = Wire.read();
-    delay(100);
-    return value;
+     Wire.beginTransmission(deviceAddress);
+     Wire.write(address);                // register to read
+     Wire.endTransmission();
+     Wire.requestFrom(deviceAddress, 1); // read a byte
+     while(!Wire.available());
+     value = Wire.read();
+     //delay(100);
+     return value;
 }
 
 void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, int val)
-{
-    Wire.beginTransmission(deviceAddress);  // start transmission to device
-    Wire.write(address);                    // send register address
-    Wire.write(val);                        // send value to write
-    Wire.endTransmission();                 // end transmission
-    delay(100);
-}
-void TSL2561_CalculateLux::getLux(void)
-{
-    CH0_LOW=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL0L);
-    CH0_HIGH=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL0H);
+ {
+     Wire.beginTransmission(deviceAddress);  // start transmission to device
+     Wire.write(address);                    // send register address
+     Wire.write(val);                        // send value to write
+     Wire.endTransmission();                 // end transmission
+     //delay(100);
+ }
+ void TSL2561_CalculateLux::getLux(void)
+ {
+    CH0_LOW=readRegister(TSL2561_Address,TSL2561_Channal0L);
+    CH0_HIGH=readRegister(TSL2561_Address,TSL2561_Channal0H);
     //read two bytes from registers 0x0E and 0x0F
-    CH1_LOW=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL1L);
-    CH1_HIGH=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL1H);
+    CH1_LOW=readRegister(TSL2561_Address,TSL2561_Channal1L);
+    CH1_HIGH=readRegister(TSL2561_Address,TSL2561_Channal1H);
 
     channel0=CH0_HIGH*256+CH0_LOW;
     channel1=CH1_HIGH*256+CH1_LOW;
-}
-void TSL2561_CalculateLux::init()
+ }
+ void TSL2561_CalculateLux::init()
+ {
+   writeRegister(TSL2561_Address,TSL2561_Control,0x03);  // POWER UP
+   writeRegister(TSL2561_Address,TSL2561_Timing,0x11);  //High Gain (16x), integration time of 101ms
+   writeRegister(TSL2561_Address,TSL2561_Interrupt,0x00);
+   writeRegister(TSL2561_Address,TSL2561_Control,0x00);  // POWER Down
+ }
+ unsigned long TSL2561_CalculateLux::readVisibleLux()
+ {
+   writeRegister(TSL2561_Address,TSL2561_Control,0x03);  // POWER UP
+   delay(102);
+   getLux();
+   writeRegister(TSL2561_Address,TSL2561_Control,0x00);  // POWER Down
+   return calculateLux(1, 1, 1);
+ }
+ unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt,int iType)
 {
-    writeRegister(TSL2561_ADDRESS,TSL2561_CONTROL,0x03);  // POWER UP
-    writeRegister(TSL2561_ADDRESS,TSL2561_TIMING,0x11);  //High Gain (16x), integration time of 101ms
-    writeRegister(TSL2561_ADDRESS,TSL2561_INTERRUPT,0x00);
+ switch (tInt)
+ {
+  case 0:  // 13.7 msec
+  chScale = CHSCALE_TINT0;
+  break;
+  case 1: // 101 msec
+  chScale = CHSCALE_TINT1;
+  break;
+  default: // assume no scaling
+  chScale = (1 << CH_SCALE);
+  break;
 }
-unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt,int iType)
-{
-    switch (tInt)
-    {
-        case 0:  // 13.7 msec
-        chScale = CHSCALE_TINT0;
-        break;
-        case 1: // 101 msec
-        chScale = CHSCALE_TINT1;
-        break;
-        default: // assume no scaling
-        chScale = (1 << CH_SCALE);
-        break;
-    }
-    if (!iGain)  chScale = chScale << 4; // scale 1X to 16X
-    // scale the channel values
-    channel0 = (channel0 * chScale) >> CH_SCALE;
-    channel1 = (channel1 * chScale) >> CH_SCALE;
+if (!iGain)  chScale = chScale << 4; // scale 1X to 16X
+// scale the channel values
+channel0 = (channel0 * chScale) >> CH_SCALE;
+channel1 = (channel1 * chScale) >> CH_SCALE;
 
-    ratio1 = 0;
-    if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
-    // round the ratio value
-     unsigned long ratio = (ratio1 + 1) >> 1;
+  ratio1 = 0;
+ if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
+// round the ratio value
+ unsigned long ratio = (ratio1 + 1) >> 1;
 
-    switch (iType)
-    {
-        case 0: // T package
-            if ((ratio >= 0) && (ratio <= K1T))
-            {b=B1T; m=M1T;}
-            else if (ratio <= K2T)
-            {b=B2T; m=M2T;}
-            else if (ratio <= K3T)
-            {b=B3T; m=M3T;}
-            else if (ratio <= K4T)
-            {b=B4T; m=M4T;}
-            else if (ratio <= K5T)
-            {b=B5T; m=M5T;}
-            else if (ratio <= K6T)
-            {b=B6T; m=M6T;}
-            else if (ratio <= K7T)
-            {b=B7T; m=M7T;}
-            else if (ratio > K8T)
-            {b=B8T; m=M8T;}
-            break;
-        case 1:// CS package
-            if ((ratio >= 0) && (ratio <= K1C))
-            {b=B1C; m=M1C;}
-            else if (ratio <= K2C)
-            {b=B2C; m=M2C;}
-            else if (ratio <= K3C)
-            {b=B3C; m=M3C;}
-            else if (ratio <= K4C)
-            {b=B4C; m=M4C;}
-            else if (ratio <= K5C)
-            {b=B5C; m=M5C;}
-            else if (ratio <= K6C)
-            {b=B6C; m=M6C;}
-            else if (ratio <= K7C)
-            {b=B7C; m=M7C;}
-    }
-    temp=((channel0*b)-(channel1*m));
-    if(temp<0) temp=0;
-    temp+=(1<<LUX_SCALE-1);
-    // strip off fractional portion
-    lux=temp>>LUX_SCALE;
-    return (lux);
+ switch (iType)
+ {
+ case 0: // T package
+   if ((ratio >= 0) && (ratio <= K1T))
+    {b=B1T; m=M1T;}
+   else if (ratio <= K2T)
+    {b=B2T; m=M2T;}
+   else if (ratio <= K3T)
+    {b=B3T; m=M3T;}
+   else if (ratio <= K4T)
+    {b=B4T; m=M4T;}
+   else if (ratio <= K5T)
+    {b=B5T; m=M5T;}
+   else if (ratio <= K6T)
+    {b=B6T; m=M6T;}
+   else if (ratio <= K7T)
+    {b=B7T; m=M7T;}
+   else if (ratio > K8T)
+    {b=B8T; m=M8T;}
+ break;
+  case 1:// CS package
+   if ((ratio >= 0) && (ratio <= K1C))
+    {b=B1C; m=M1C;}
+   else if (ratio <= K2C)
+    {b=B2C; m=M2C;}
+  else if (ratio <= K3C)
+   {b=B3C; m=M3C;}
+  else if (ratio <= K4C)
+   {b=B4C; m=M4C;}
+  else if (ratio <= K5C)
+   {b=B5C; m=M5C;}
+  else if (ratio <= K6C)
+   {b=B6C; m=M6C;}
+  else if (ratio <= K7C)
+    {b=B7C; m=M7C;}
+ }
+  temp=((channel0*b)-(channel1*m));
+  if(temp<0) temp=0;
+  temp+=(1<<LUX_SCALE-1);
+  // strip off fractional portion
+  lux=temp>>LUX_SCALE;
+  return (lux);
  }
  TSL2561_CalculateLux TSL2561;
 
+

+ 30 - 26
Digital_Light_TSL2561.h

@@ -28,18 +28,19 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#ifndef __DIGITAL_LIGHT_TSL2561_H__
-#define __DIGITAL_LIGHT_TSL2561_H__
 
-#define  TSL2561_CONTROL  0x80
-#define  TSL2561_TIMING   0x81
-#define  TSL2561_INTERRUPT 0x86
-#define  TSL2561_CHANNAL0L 0x8C
-#define  TSL2561_CHANNAL0H 0x8D
-#define  TSL2561_CHANNAL1L 0x8E
-#define  TSL2561_CHANNAL1H 0x8F
+#ifndef Digital_Light_TSL2561_H
+#define Digital_Light_TSL2561_H
 
-#define TSL2561_ADDRESS  0x29       //device address
+#define  TSL2561_Control  0x80
+#define  TSL2561_Timing   0x81
+#define  TSL2561_Interrupt 0x86
+#define  TSL2561_Channal0L 0x8C
+#define  TSL2561_Channal0H 0x8D
+#define  TSL2561_Channal1L 0x8E
+#define  TSL2561_Channal1H 0x8F
+
+#define TSL2561_Address  0x29       //device address
 
 #define LUX_SCALE 14           // scale by 2^14
 #define RATIO_SCALE 9          // scale ratio by 2^9
@@ -100,23 +101,26 @@
 #define M8C 0x0000   // 0.000 * 2^LUX_SCALE
 class TSL2561_CalculateLux
 {
-public:
-    unsigned long calculateLux(unsigned int iGain, unsigned int tInt,int iType);
-    void getLux(void);
-    void init(void);
-    int readRegister(int deviceAddress, int address);
-    void writeRegister(int deviceAddress, int address, int val);
-private:
-    int CH0_LOW,CH0_HIGH,CH1_LOW,CH1_HIGH;
-    unsigned long chScale;
-    unsigned long channel1;
-    unsigned long channel0;
-    unsigned long  ratio1;
-    unsigned int b;
-    unsigned int m;
-    unsigned long temp;
-    unsigned long lux;
+ public:
+  unsigned long readVisibleLux();
+  unsigned long calculateLux(unsigned int iGain, unsigned int tInt,int iType);
+  void getLux(void);
+  void init(void);
+  int readRegister(int deviceAddress, int address);
+  void writeRegister(int deviceAddress, int address, int val);
+ private:
+  int CH0_LOW,CH0_HIGH,CH1_LOW,CH1_HIGH;
+  unsigned long chScale;
+  unsigned long channel1;
+  unsigned long channel0;
+  unsigned long  ratio1;
+  unsigned int b;
+  unsigned int m;
+  unsigned long temp;
+  unsigned long lux;
+
  };
 extern TSL2561_CalculateLux  TSL2561;
 #endif
 
+

+ 2 - 2
README.md

@@ -15,9 +15,9 @@ This is an Arduino Library of Grove - Digital Light Sensor.
 
     TSL2561.init(); 
 
-### Get Data
+### Get the Lux reading of visible light
 
-    unsigned long calculateLux(unsigned int iGain, unsigned int tInt,int iType);
+    unsigned long readVisibleLux();
 
 
 <br>

+ 8 - 9
examples/Digital_Light_Sensor/Digital_Light_Sensor.ino

@@ -28,22 +28,21 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+
 #include <Wire.h>
 #include <Digital_Light_TSL2561.h>
 void setup()
 {
-    Wire.begin();
-    Serial.begin(9600);
-    TSL2561.init();
+  Wire.begin();
+  Serial.begin(9600);
+  TSL2561.init();
 }
 
 void loop()
 {
-    unsigned long  Lux;
+  Serial.print("The Light value is: ");
+  Serial.println(TSL2561.readVisibleLux());
+  delay(1000);
+}
 
-    TSL2561.getLux();
 
-    Serial.print("The Light value is: ");
-    Serial.println(TSL2561.calculateLux(0,0,1));
-    delay(1000);
-}